private boolean createFunction(TaskMonitor monitor, String funcName, Namespace nameSpace,
			Address entry, AddressSetView body, SourceType nameSource)
			throws InvalidInputException, OverlappingFunctionException, CancelledException {

		FunctionManager functionMgr = program.getFunctionManager();

		// if the entry point is unknown, and just have a location in the body
		//    try to find the entry point (must wander into an existing function
		if (this.findEntryPoint) {
			// if we are already in a function, then no need to find the entry
			Function functionContaining = functionMgr.getFunctionContaining(entry);
			if (functionContaining != null) {
				// if we are not recreating the function,
				//  then don't continue because there is already a function here.
				if (!recreateFunction) {
					entry = functionContaining.getEntryPoint(); // preserve entry
					long bodySize = functionContaining.getBody().getNumAddresses();
					if (bodySize != 1) {
						return true; // function already created
					}
				}

				// if the function containing this entry does not start at this address
				if (!functionContaining.getEntryPoint().equals(entry)) {
					entry = findFunctionEntry(entry);
				}
			}
			if (entry == null) {
				return false;
			}
			if (origBody != null && !origBody.isEmpty()) {
				Function func = program.getFunctionManager().getFunctionContaining(entry);
				if (func == null) {
					return false;
				}
				try {
					func.setBody(origBody); // trigger analysis
					return true;
				}
				catch (OverlappingFunctionException e) {
					// don't care about overlapping functions
				}
			}
			if (fixupFunctionBody(program, program.getListing().getInstructionAt(entry), monitor)) {
				return true;
			}
		}

		Function existingFunction = functionMgr.getFunctionAt(entry);

		// if there is an existing function handle necessary changes
		if (existingFunction != null) {
			return handleExistingFunction(monitor, entry, existingFunction);
		}

		// if the body is undefined, figure it out
		// get the function body JUST by following flow
		body = (body == null ? getFunctionBody(program, entry, false, monitor) : body);

		Map<Function, AddressSetView> bodyChangeMap = new HashMap<>();
		body = subtractBodyFromExisting(program, entry, body, bodyChangeMap, monitor);

		return createFunction(nameSpace, funcName, entry, body, nameSource, bodyChangeMap, monitor);
	}
Creates a function in the program.

@param entry
           The address of the entry point for the new function
@param body
           The address set containing all the addresses to be included in
           the body of the new function.
@param nameSource
           the source of this function's name
           
@return true if a function was created, or already exists

@throws OverlappingFunctionException if new function overlaps with existing and couldn't fix
@throws InvalidInputException bad characters in function name
Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateFunctionCmd.java