#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 */
struct rlimit {
  int rlim_max;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int adc_server_daemonize(void) {
  int i, fdin, fdout, fderr;
  pid_t ret_pid;
  struct rlimit rl;
  umask(0);
  if ((ret_pid = fork()) < 0) {
    do {
    } while (0);
    return -1;
  } else if (ret_pid > 0) {
    exit(0);
  }
  setsid();
  signal(SIGHUP, SIG_IGN);
  if ((ret_pid = fork()) < 0) {
    do {
    } while (0);
    return -1;
  } else if (ret_pid > 0) {
    exit(0);
  }
  if (chdir("/") < 0) {
    do {
    } while (0);
    return -1;
  }
  if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
    do {
    } while (0);
    return -1;
  }
  if (rl.rlim_max == RLIM_INFINITY)
    rl.rlim_max = 512;
  for (i = 0; i < rl.rlim_max; i++)
    close(i);
  fdin = open("/dev/null", O_RDWR);
  fdout = dup(0);
  fderr = dup(0);
  if (fdin != 0 || fdout != 1 || fderr != 2) {
    syslog(LOG_ERR, "Redicet fd failed...");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<adc_server_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %edi,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    11ea <adc_server_daemonize+0x3a>
jmp    11d9 <adc_server_daemonize+0x29>
jmp    11de <adc_server_daemonize+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <adc_server_daemonize+0x1c3>
cmpq   $0x0,-0x20(%rbp)
jle    11fc <adc_server_daemonize+0x4c>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1201 <adc_server_daemonize+0x51>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    124a <adc_server_daemonize+0x9a>
jmp    1239 <adc_server_daemonize+0x89>
jmp    123e <adc_server_daemonize+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <adc_server_daemonize+0x1c3>
cmpq   $0x0,-0x20(%rbp)
jle    125c <adc_server_daemonize+0xac>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1261 <adc_server_daemonize+0xb1>
lea    0xd98(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    128d <adc_server_daemonize+0xdd>
jmp    127c <adc_server_daemonize+0xcc>
jmp    1281 <adc_server_daemonize+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <adc_server_daemonize+0x1c3>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   10c0 <getrlimit@plt>
cmp    $0x0,%rax
jge    12bf <adc_server_daemonize+0x10f>
jmp    12ae <adc_server_daemonize+0xfe>
jmp    12b3 <adc_server_daemonize+0x103>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <adc_server_daemonize+0x1c3>
mov    -0x28(%rbp),%eax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%eax
jne    12d8 <adc_server_daemonize+0x128>
movl   $0x200,-0x28(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1301 <adc_server_daemonize+0x151>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12df <adc_server_daemonize+0x12f>
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
lea    0xcf1(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    134b <adc_server_daemonize+0x19b>
cmpl   $0x1,-0x10(%rbp)
jne    134b <adc_server_daemonize+0x19b>
cmpl   $0x2,-0x14(%rbp)
je     136c <adc_server_daemonize+0x1bc>
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
lea    0xcb1(%rip),%rsi
call   1090 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <adc_server_daemonize+0x1c3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adc_server_daemonize>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     12c3 <adc_server_daemonize+0x113>
jne    12ce <adc_server_daemonize+0x11e>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12c3 <adc_server_daemonize+0x113>
jne    12ce <adc_server_daemonize+0x11e>
lea    0xdeb(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     12c3 <adc_server_daemonize+0x113>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   10c0 <getrlimit@plt>
test   %rax,%rax
js     12c3 <adc_server_daemonize+0x113>
mov    0x8(%rsp),%eax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%eax
jne    1258 <adc_server_daemonize+0xa8>
movl   $0x200,0x8(%rsp)
jmp    125c <adc_server_daemonize+0xac>
test   %eax,%eax
jle    1270 <adc_server_daemonize+0xc0>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1260 <adc_server_daemonize+0xb0>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    0xd82(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%r14d
xor    %edi,%edi
call   1040 <dup@plt>
test   %ebp,%ebp
jne    12a9 <adc_server_daemonize+0xf9>
cmp    $0x1,%r14d
jne    12a9 <adc_server_daemonize+0xf9>
cmp    $0x2,%eax
je     12c3 <adc_server_daemonize+0x113>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
call   1090 <syslog@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [2 x i8] c"/\00"
@RLIMIT_NOFILE = global i32 0
@RLIM_INFINITY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@LOG_ERR = global i32 0
@.str.2 = [21 x i8] c"Redicet fd failed...\00"
define i32 @adc_server_daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.rlimit
%8 = call i32 @umask(i32 0)
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
br label %12
12: ; preds = %11
br label %13
13: ; preds = %12
store i32 -1, i32* %1
br label %85
14: ; preds = %0
%15 = load i64, i64* %6
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %14
br label %20
20: ; preds = %19
%21 = call i32 (...) @setsid()
%22 = load i32, i32* @SIGHUP
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = call i64 (...) @fork()
store i64 %25, i64* %6
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
br label %28
28: ; preds = %27
br label %29
29: ; preds = %28
store i32 -1, i32* %1
br label %85
30: ; preds = %20
%31 = load i64, i64* %6
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %30
br label %36
36: ; preds = %35
%37 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
br label %40
40: ; preds = %39
br label %41
41: ; preds = %40
store i32 -1, i32* %1
br label %85
42: ; preds = %36
%43 = load i32, i32* @RLIMIT_NOFILE
%44 = call i64 @getrlimit(i32 %43, %struct.rlimit* %7)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
br label %47
47: ; preds = %46
br label %48
48: ; preds = %47
store i32 -1, i32* %1
br label %85
49: ; preds = %42
%50 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i32, i32* @RLIM_INFINITY
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i32 512, i32* %55
br label %56
56: ; preds = %54, %49
store i32 0, i32* %2
br label %57
57: ; preds = %65, %56
%58 = load i32, i32* %2
%59 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%60 = load i32, i32* %59
%61 = icmp slt i32 %58, %60
br i1 %61, label %62, label %68
62: ; preds = %57
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62
%66 = load i32, i32* %2
%67 = add nsw i32 %66, 1
store i32 %67, i32* %2
br label %57
68: ; preds = %57
%69 = load i32, i32* @O_RDWR
%70 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %69)
store i32 %70, i32* %3
%71 = call i32 @dup(i32 0)
store i32 %71, i32* %4
%72 = call i32 @dup(i32 0)
store i32 %72, i32* %5
%73 = load i32, i32* %3
%74 = icmp ne i32 %73, 0
br i1 %74, label %81, label %75
75: ; preds = %68
%76 = load i32, i32* %4
%77 = icmp ne i32 %76, 1
br i1 %77, label %81, label %78
78: ; preds = %75
%79 = load i32, i32* %5
%80 = icmp ne i32 %79, 2
br i1 %80, label %81, label %84
81: ; preds = %78, %75, %68
%82 = load i32, i32* @LOG_ERR
%83 = call i32 @syslog(i32 %82, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %85
84: ; preds = %78
store i32 0, i32* %1
br label %85
85: ; preds = %84, %81, %48, %41, %29, %13
%86 = load i32, i32* %1
ret i32 %86
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [2 x i8] c"/\00"
@RLIMIT_NOFILE = global i32 0
@RLIM_INFINITY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@LOG_ERR = global i32 0
@.str.2 = [21 x i8] c"Redicet fd failed...\00"
define i32 @adc_server_daemonize() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = tail call i32 @umask(i32 0)
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %56, label %6
6: ; preds = %0
%7 = icmp eq i64 %4, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i32 (...) @setsid()
%12 = load i32, i32* @SIGHUP
%13 = load i32, i32* @SIG_IGN
%14 = tail call i32 @signal(i32 %12, i32 %13)
%15 = tail call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %56, label %17
17: ; preds = %10
%18 = icmp eq i64 %15, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i32 @exit(i32 0)
unreachable
21: ; preds = %17
%22 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp slt i64 %22, 0
br i1 %23, label %56, label %24
24: ; preds = %21
%25 = load i32, i32* @RLIMIT_NOFILE
%26 = call i64 @getrlimit(i32 %25, %struct.rlimit* %1)
%27 = icmp slt i64 %26, 0
br i1 %27, label %56, label %28
28: ; preds = %24
%29 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @RLIM_INFINITY
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 512, i32* %29
br label %36
34: ; preds = %28
%35 = icmp sgt i32 %30, 0
br i1 %35, label %36, label %43
36: ; preds = %33, %34
br label %37
37: ; preds = %36, %37
%38 = phi i32 [ %40, %37 ], [ 0, %36 ]
%39 = call i32 @close(i32 %38)
%40 = add nuw nsw i32 %38, 1
%41 = load i32, i32* %29
%42 = icmp slt i32 %40, %41
br i1 %42, label %37, label %43
43: ; preds = %37, %34
%44 = load i32, i32* @O_RDWR
%45 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = call i32 @dup(i32 0)
%47 = call i32 @dup(i32 0)
%48 = icmp ne i32 %45, 0
%49 = icmp ne i32 %46, 1
%50 = select i1 %48, i1 true, i1 %49
%51 = icmp ne i32 %47, 2
%52 = select i1 %50, i1 true, i1 %51
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = load i32, i32* @LOG_ERR
%55 = call i32 @syslog(i32 %54, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %43, %24, %21, %10, %0, %53
%57 = phi i32 [ -1, %53 ], [ -1, %0 ], [ -1, %10 ], [ -1, %21 ], [ -1, %24 ], [ 0, %43 ]
ret i32 %57
}