// File: /workspace/github_c_makefile_star2_O1/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 0xe938
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0xdfdd
movq %rax, %rbx
callq 0x23e0
testl %eax, %eax
je 0xdf2d
cmpl $-0x1, %eax
jne 0xdfaa
callq 0x20a0
cmpl $0x0, (%rax)
je 0xdfc7
movq %rax, %r14
leaq 0x7fed(%rip), %rdi      # 0x15f07
xorl %eax, %eax
callq 0x10048
movl $0x0, (%r14)
jmp 0xdfd5
callq 0x2220
cmpl $-0x1, %eax
je 0xdfea
leaq 0x6158(%rip), %rsi      # 0x1409a
movq %r15, %rdi
callq 0x2330
testq %rax, %rax
je 0xe000
movq %rax, %r14
callq 0x2150
movslq %eax, %rdx
leaq 0x7fb8(%rip), %rsi      # 0x15f1d
movq %r14, %rdi
xorl %eax, %eax
callq 0x2270
testl %eax, %eax
js 0xe020
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
jne 0xe03d
movq %rbx, %rdi
callq 0xeb06
testl %eax, %eax
jne 0xe0b7
movq %rbx, %rdi
callq 0xeb8b
xorl %ebp, %ebp
testl %eax, %eax
je 0xdfdd
jmp 0xe0b7
movq %rbx, %rdi
callq 0xe9d9
testl %eax, %eax
jne 0xdfd5
movq %rbx, %rdi
callq 0xeb8b
testl %eax, %eax
jne 0xdfdd
jmp 0xe0b7
leaq 0x7f39(%rip), %rdi      # 0x15f07
xorl %eax, %eax
callq 0x10167
movq %rbx, %rdi
callq 0xeb8b
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x20a0
cmpl $0x0, (%rax)
jne 0xe04c
leaq 0x7f11(%rip), %rdi      # 0x15f0c
jmp 0xe09a
callq 0x20a0
cmpl $0x0, (%rax)
jne 0xe058
leaq 0x7f02(%rip), %rdi      # 0x15f13
movq %r15, %rsi
xorl %eax, %eax
callq 0x10167
jmp 0xe0b7
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
jne 0xe06e
leaq 0x7eed(%rip), %rdi      # 0x15f21
xorl %eax, %eax
callq 0x10167
jmp 0xe082
callq 0x20a0
cmpl $0x0, (%rax)
je 0xe093
movq %rax, %rbx
jmp 0xe0a3
movq %rax, %rbx
leaq 0x7eb6(%rip), %rdi      # 0x15f0c
jmp 0xe0aa
movq %rax, %rbx
leaq 0x7eb1(%rip), %rdi      # 0x15f13
movq %r15, %rsi
xorl %eax, %eax
callq 0x10048
jmp 0xe0b1
leaq 0x7eac(%rip), %rdi      # 0x15f21
xorl %eax, %eax
callq 0x10048
movl $0x0, (%rbx)
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
je 0xe0b7
cmpl $0x0, (%rbx)
jne 0xe0a3
leaq 0x7e8f(%rip), %rdi      # 0x15f29
xorl %eax, %eax
callq 0x10167
jmp 0xe0b7
leaq 0x7e7f(%rip), %rdi      # 0x15f29
xorl %eax, %eax
callq 0x10048
movl $0x0, (%rbx)
xorl %edi, %edi
callq 0x20c0
nop