#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 */

/* Variables and functions */
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int server_main(int, char **);
int /*<<< orphan*/ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int);
int daemonize(int argc, char *argv[]) {
  int rc;
  int pid;
  int fd;
  int maxfd;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "fork failed\n");
    return -1;
  }
  if (pid > 0)
    return 0;
  setpgid(0, 0);
  setsid();
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "fork failed\n");
    return -1;
  }
  if (pid > 0)
    return 0;
  maxfd = sysconf(_SC_OPEN_MAX);
  for (fd = 0; fd < maxfd; fd++)
    close(fd);
  umask(022);
  chdir("/tmp");
  rc = server_main(argc, argv);
  return rc;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11d4 <daemonize+0x44>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <daemonize+0x11a>
cmpl   $0x0,-0x18(%rbp)
jle    11ea <daemonize+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    12aa <daemonize+0x11a>
xor    %esi,%esi
mov    %esi,%edi
call   10a0 <setpgid@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    122f <daemonize+0x9f>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <daemonize+0x11a>
cmpl   $0x0,-0x18(%rbp)
jle    1245 <daemonize+0xb5>
movl   $0x0,-0x4(%rbp)
jmp    12aa <daemonize+0x11a>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    127f <daemonize+0xef>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    125d <daemonize+0xcd>
mov    $0x12,%edi
call   1070 <umask@plt>
lea    0xd7d(%rip),%rdi
call   1030 <chdir@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <server_main@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1229 <daemonize+0x99>
jne    1243 <daemonize+0xb3>
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
call   10a0 <setpgid@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1229 <daemonize+0x99>
jne    1243 <daemonize+0xb3>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
test   %eax,%eax
jle    11fe <daemonize+0x6e>
mov    %eax,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    11f0 <daemonize+0x60>
mov    $0x12,%edi
call   1070 <umask@plt>
lea    0xdfe(%rip),%rdi
call   1030 <chdir@plt>
mov    %r15d,%edi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <server_main@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"fork failed\0A\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [5 x i8] c"/tmp\00"
define i32 @daemonize(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @fork()
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
16: ; preds = %2
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 0, i32* %3
br label %53
20: ; preds = %16
%21 = call i32 @setpgid(i32 0, i32 0)
%22 = call i32 (...) @setsid()
%23 = call i32 (...) @fork()
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
29: ; preds = %20
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 0, i32* %3
br label %53
33: ; preds = %29
%34 = load i32, i32* @_SC_OPEN_MAX
%35 = call i32 @sysconf(i32 %34)
store i32 %35, i32* %9
store i32 0, i32* %8
br label %36
36: ; preds = %43, %33
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %36
46: ; preds = %36
%47 = call i32 @umask(i32 18)
%48 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* %4
%50 = load i8**, i8*** %5
%51 = call i32 @server_main(i32 %49, i8** %50)
store i32 %51, i32* %6
%52 = load i32, i32* %6
store i32 %52, i32* %3
br label %53
53: ; preds = %46, %32, %26, %19, %13
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"fork failed\0A\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [5 x i8] c"/tmp\00"
define i32 @daemonize(i32 %0, i8** %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %33
8: ; preds = %2
%9 = icmp eq i32 %3, 0
br i1 %9, label %10, label %33
10: ; preds = %8
%11 = tail call i32 @setpgid(i32 0, i32 0)
%12 = tail call i32 (...) @setsid()
%13 = tail call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %33
18: ; preds = %10
%19 = icmp eq i32 %13, 0
br i1 %19, label %20, label %33
20: ; preds = %18
%21 = load i32, i32* @_SC_OPEN_MAX
%22 = tail call i32 @sysconf(i32 %21)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20, %24
%25 = phi i32 [ %27, %24 ], [ 0, %20 ]
%26 = tail call i32 @close(i32 %25)
%27 = add nuw nsw i32 %25, 1
%28 = icmp eq i32 %27, %22
br i1 %28, label %29, label %24
29: ; preds = %24, %20
%30 = tail call i32 @umask(i32 18)
%31 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = tail call i32 @server_main(i32 %0, i8** %1)
br label %33
33: ; preds = %18, %8, %29, %15, %5
%34 = phi i32 [ -1, %5 ], [ -1, %15 ], [ %32, %29 ], [ 0, %8 ], [ 0, %18 ]
ret i32 %34
}