// File: /workspace/github_c_makefile_star2_O2/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 0xcb34
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0xbf40
movq %rax, %rbx
callq 0x23e0
testl %eax, %eax
je 0xbe90
cmpl $-0x1, %eax
jne 0xbf0d
callq 0x20a0
cmpl $0x0, (%rax)
je 0xbf2a
movq %rax, %r14
leaq 0x708f(%rip), %rdi      # 0x12f0f
xorl %eax, %eax
callq 0xdf55
andl $0x0, (%r14)
jmp 0xbf38
callq 0x2220
cmpl $-0x1, %eax
je 0xbf4d
leaq 0x51f5(%rip), %rsi      # 0x1109a
movq %r15, %rdi
callq 0x2330
testq %rax, %rax
je 0xbf63
movq %rax, %r14
callq 0x2150
movslq %eax, %rdx
leaq 0x705d(%rip), %rsi      # 0x12f25
movq %r14, %rdi
xorl %eax, %eax
callq 0x2270
testl %eax, %eax
js 0xbf83
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
jne 0xbfa0
movq %rbx, %rdi
callq 0xcc9f
testl %eax, %eax
jne 0xc014
movq %rbx, %rdi
callq 0xcd1b
xorl %ebp, %ebp
testl %eax, %eax
je 0xbf40
jmp 0xc014
movq %rbx, %rdi
callq 0xcbcd
testl %eax, %eax
jne 0xbf38
movq %rbx, %rdi
callq 0xcd1b
testl %eax, %eax
jne 0xbf40
jmp 0xc014
leaq 0x6fde(%rip), %rdi      # 0x12f0f
xorl %eax, %eax
callq 0xe0e4
movq %rbx, %rdi
callq 0xcd1b
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x20a0
cmpl $0x0, (%rax)
jne 0xbfaf
leaq 0x6fb6(%rip), %rdi      # 0x12f14
jmp 0xbffa
callq 0x20a0
cmpl $0x0, (%rax)
jne 0xbfbb
leaq 0x6fa7(%rip), %rdi      # 0x12f1b
movq %r15, %rsi
xorl %eax, %eax
callq 0xe0e4
jmp 0xc014
callq 0x20a0
movq %rax, %rbx
cmpl $0x0, (%rax)
jne 0xbfd1
leaq 0x6f92(%rip), %rdi      # 0x12f29
xorl %eax, %eax
callq 0xe0e4
jmp 0xbfe2
callq 0x20a0
cmpl $0x0, (%rax)
je 0xbff3
movq %rax, %rbx
jmp 0xc003
movq %rax, %rbx
leaq 0x6f5b(%rip), %rdi      # 0x12f14
jmp 0xc00a
movq %rax, %rbx
leaq 0x6f56(%rip), %rdi      # 0x12f1b
movq %r15, %rsi
xorl %eax, %eax
callq 0xdf55
jmp 0xc011
leaq 0x6f51(%rip), %rdi      # 0x12f29
xorl %eax, %eax
callq 0xdf55
andl $0x0, (%rbx)
movq %r14, %rdi
callq 0x2160
testl %eax, %eax
je 0xc014
cmpl $0x0, (%rbx)
jne 0xc003
leaq 0x6f37(%rip), %rdi      # 0x12f31
xorl %eax, %eax
callq 0xe0e4
jmp 0xc014
leaq 0x6f27(%rip), %rdi      # 0x12f31
xorl %eax, %eax
callq 0xdf55
andl $0x0, (%rbx)
xorl %edi, %edi
callq 0x20c0
nop