// File: /workspace/github_c_makefile_star2_O3/Tarsnap[P]kivaloo/liball/../libcperciva/util/daemonize.c
// Function: daemonize
int
daemonize(const char * spid)
{
	struct ipc_sync * IS;

	/*
	 * Create a pipe for the child to notify the parent when it has
	 * finished daemonizing.
	 */
	if ((IS = ipc_sync_init()) == NULL)
		goto err0;

	/*
	 * Fork into the parent process (which waits for a poke and exits)
	 * and the child process (which keeps going).
	 */
	switch (fork()) {
	case -1:
		/* Fork failed. */
		warnp("fork");
		goto err1;
	case 0:
		/* In child process. */
		break;
	default:
		/* In parent process; wait for the child to start. */
		if (ipc_sync_wait(IS))
			goto err1;

		/*
		 * Free resources associated with ${IS}.  These would be
		 * released by the following _exit(), but we're explicitly
		 * releasing them for the benefit of leak checkers.
		 */
		if (ipc_sync_done(IS))
			goto err0;

		/* We have been poked by the child.  Exit. */
		_exit(0);
	}

	/* Set ourselves to be a session leader. */
	if (setsid() == -1) {
		warnp("setsid");
		goto die;
	}

	/* Write out our pid file. */
	if (writepid(spid))
		goto die;

	/* Tell the parent that we've started. */
	if (ipc_sync_signal(IS))
		goto die;

	/* Clean up. */
	if (ipc_sync_done(IS))
		goto die;

	/* Success! */
	return (0);

err1:
	ipc_sync_done(IS);
err0:
	/* Failure! */
	return (-1);

die:
	/*
	 * We're in the child and something bad happened; the parent will be
	 * notified when we die thanks to the pipe being closed.
	 */
	_exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %r15
callq 0xe91c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0xdeed
movq %rax, %rbx
callq 0x23e0
testl %eax, %eax
je 0xde3d
cmpl $-0x1, %eax
jne 0xdeba
callq 0x20a0
cmpl $0x0, (%rax)
je 0xded7
movq %rax, %r14
leaq 0x80b5(%rip), %rdi      # 0x15edf
xorl %eax, %eax
callq 0xffd0
movl $0x0, (%r14)
jmp 0xdee5
callq 0x2220
cmpl $-0x1, %eax
je 0xdefa
leaq 0x6248(%rip), %rsi      # 0x1409a
movq %r15, %rdi
callq 0x2330
testq %rax, %rax
je 0xdf10
movq %rax, %r14
callq 0x2150
movslq %eax, %rdx
leaq 0x8080(%rip), %rsi      # 0x15ef5
movq %r14, %rdi
xorl %eax, %eax
callq 0x2270
testl %eax, %eax
js 0xdf30
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
jne 0xdf4d
movq %rbx, %rdi
callq 0xead5
testl %eax, %eax
jne 0xdfc7
movq %rbx, %rdi
callq 0xeb5a
xorl %ebp, %ebp
testl %eax, %eax
je 0xdeed
jmp 0xdfc7
movq %rbx, %rdi
callq 0xe9bd
testl %eax, %eax
jne 0xdee5
movq %rbx, %rdi
callq 0xeb5a
testl %eax, %eax
jne 0xdeed
jmp 0xdfc7
leaq 0x8001(%rip), %rdi      # 0x15edf
xorl %eax, %eax
callq 0x100ef
movq %rbx, %rdi
callq 0xeb5a
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x20a0
cmpl $0x0, (%rax)
jne 0xdf5c
leaq 0x7fd9(%rip), %rdi      # 0x15ee4
jmp 0xdfaa
callq 0x20a0
cmpl $0x0, (%rax)
jne 0xdf68
leaq 0x7fca(%rip), %rdi      # 0x15eeb
movq %r15, %rsi
xorl %eax, %eax
callq 0x100ef
jmp 0xdfc7
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
jne 0xdf7e
leaq 0x7fb5(%rip), %rdi      # 0x15ef9
xorl %eax, %eax
callq 0x100ef
jmp 0xdf92
callq 0x20a0
cmpl $0x0, (%rax)
je 0xdfa3
movq %rax, %rbx
jmp 0xdfb3
movq %rax, %rbx
leaq 0x7f7e(%rip), %rdi      # 0x15ee4
jmp 0xdfba
movq %rax, %rbx
leaq 0x7f79(%rip), %rdi      # 0x15eeb
movq %r15, %rsi
xorl %eax, %eax
callq 0xffd0
jmp 0xdfc1
leaq 0x7f74(%rip), %rdi      # 0x15ef9
xorl %eax, %eax
callq 0xffd0
movl $0x0, (%rbx)
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
je 0xdfc7
cmpl $0x0, (%rbx)
jne 0xdfb3
leaq 0x7f57(%rip), %rdi      # 0x15f01
xorl %eax, %eax
callq 0x100ef
jmp 0xdfc7
leaq 0x7f47(%rip), %rdi      # 0x15f01
xorl %eax, %eax
callq 0xffd0
movl $0x0, (%rbx)
xorl %edi, %edi
callq 0x20c0
nop