#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ _setpidfile(char const *);
int /*<<< orphan*/ fork();
int getppid();
int setsid();
int daemonize(const char *pidfile) {
  pid_t pid;
  if (getppid() == 1) {
    return -1;
  }
  if ((pid = fork()) > 0) {
    ;
    return -1;
  }
  int sid = setsid();
  ;
  if (pidfile != NULL) {
    _setpidfile(pidfile);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
cmp    $0x1,%eax
jne    1168 <daemonize+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <daemonize+0x6c>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1187 <daemonize+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <daemonize+0x6c>
mov    $0x0,%al
call   1030 <setsid@plt>
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a5 <daemonize+0x65>
mov    -0x10(%rbp),%rdi
call   1040 <_setpidfile@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <getppid@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
je     1178 <daemonize+0x38>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
jg     1178 <daemonize+0x38>
xor    %eax,%eax
call   1030 <setsid@plt>
test   %rbx,%rbx
je     1176 <daemonize+0x36>
mov    %rbx,%rdi
call   1040 <_setpidfile@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @daemonize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 (...) @getppid()
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %21
9: ; preds = %1
%10 = call i32 (...) @fork()
store i32 %10, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 -1, i32* %2
br label %21
13: ; preds = %9
%14 = call i32 (...) @setsid()
store i32 %14, i32* %5
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i32 @_setpidfile(i8* %18)
br label %20
20: ; preds = %17, %13
store i32 0, i32* %2
br label %21
21: ; preds = %20, %12, %8
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
define i32 @daemonize(i8* %0) {
%2 = tail call i32 (...) @getppid()
%3 = icmp eq i32 %2, 1
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = tail call i32 (...) @fork()
%6 = icmp sgt i32 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = tail call i32 (...) @setsid()
%9 = icmp eq i8* %0, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @_setpidfile(i8* %0)
br label %12
12: ; preds = %7, %10, %4, %1
%13 = phi i32 [ -1, %1 ], [ -1, %4 ], [ 0, %10 ], [ 0, %7 ]
ret i32 %13
}