#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int InitDaemon(int iNum) {
  pid_t pid;
  if ((pid = fork()) != 0) {
    exit(0);
  }
  setpgid(0, 0);
  signal(SIGINT, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  signal(SIGQUIT, SIG_IGN);
  signal(SIGPIPE, SIG_IGN);
  signal(SIGTTOU, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
  signal(SIGCHLD, SIG_IGN);
  if ((pid = fork()) != 0) {
    exit(0);
  }
  umask(0);
  if (iNum <= 1) {
    return -1;
  }
  pid = 0;
  for (int i = 0; i < iNum; i++) {
    pid = fork();
    if (pid == 0 || pid == -1) {
      break;
    }
  }
  if (pid == -1) {
    perror("fail to fork!\n");
    exit(1);
  } else if (pid == 0) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<InitDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     1185 <InitDaemon+0x25>
xor    %edi,%edi
call   1070 <exit@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1060 <setpgid@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     1249 <InitDaemon+0xe9>
xor    %edi,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1040 <umask@plt>
cmpl   $0x1,-0x8(%rbp)
jg     1266 <InitDaemon+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <InitDaemon+0x193>
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12b6 <InitDaemon+0x156>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     129e <InitDaemon+0x13e>
cmpl   $0xffffffff,-0xc(%rbp)
jne    12a3 <InitDaemon+0x143>
jmp    12b6 <InitDaemon+0x156>
jmp    12a8 <InitDaemon+0x148>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1274 <InitDaemon+0x114>
cmpl   $0xffffffff,-0xc(%rbp)
jne    12d6 <InitDaemon+0x176>
lea    0xd39(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    12ec <InitDaemon+0x18c>
movl   $0x1,-0x4(%rbp)
jmp    12f3 <InitDaemon+0x193>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InitDaemon>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    124f <InitDaemon+0xef>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <setpgid@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%r14
mov    (%r14),%esi
call   1030 <signal@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1030 <signal@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1030 <signal@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1030 <signal@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1030 <signal@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1030 <signal@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1030 <signal@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    124f <InitDaemon+0xef>
xor    %edi,%edi
call   1040 <umask@plt>
mov    $0xffffffff,%ecx
cmp    $0x2,%ebp
jl     1248 <InitDaemon+0xe8>
mov    $0x1,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1080 <fork@plt>
lea    -0x1(%rax),%ecx
cmp    $0xfffffffd,%ecx
ja     1238 <InitDaemon+0xd8>
lea    0x1(%rbx),%ecx
cmp    %ebp,%ebx
mov    %ecx,%ebx
jl     1220 <InitDaemon+0xc0>
mov    $0x1,%ecx
test   %eax,%eax
je     1248 <InitDaemon+0xe8>
cmp    $0xffffffff,%eax
je     1256 <InitDaemon+0xf6>
xor    %ecx,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xda3(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@SIGQUIT = global i32 0
@SIGPIPE = global i32 0
@SIGTTOU = global i32 0
@SIGTTIN = global i32 0
@SIGCHLD = global i32 0
@.str = [15 x i8] c"fail to fork!\0A\00"
define i32 @InitDaemon(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i32 (...) @fork()
store i32 %6, i32* %4
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %1
%11 = call i32 @setpgid(i32 0, i32 0)
%12 = load i32, i32* @SIGINT
%13 = load i32, i32* @SIG_IGN
%14 = call i32 @signal(i32 %12, i32 %13)
%15 = load i32, i32* @SIGHUP
%16 = load i32, i32* @SIG_IGN
%17 = call i32 @signal(i32 %15, i32 %16)
%18 = load i32, i32* @SIGQUIT
%19 = load i32, i32* @SIG_IGN
%20 = call i32 @signal(i32 %18, i32 %19)
%21 = load i32, i32* @SIGPIPE
%22 = load i32, i32* @SIG_IGN
%23 = call i32 @signal(i32 %21, i32 %22)
%24 = load i32, i32* @SIGTTOU
%25 = load i32, i32* @SIG_IGN
%26 = call i32 @signal(i32 %24, i32 %25)
%27 = load i32, i32* @SIGTTIN
%28 = load i32, i32* @SIG_IGN
%29 = call i32 @signal(i32 %27, i32 %28)
%30 = load i32, i32* @SIGCHLD
%31 = load i32, i32* @SIG_IGN
%32 = call i32 @signal(i32 %30, i32 %31)
%33 = call i32 (...) @fork()
store i32 %33, i32* %4
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %10
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %10
%38 = call i32 @umask(i32 0)
%39 = load i32, i32* %3
%40 = icmp sle i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 -1, i32* %2
br label %70
42: ; preds = %37
store i32 0, i32* %4
store i32 0, i32* %5
br label %43
43: ; preds = %56, %42
%44 = load i32, i32* %5
%45 = load i32, i32* %3
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = call i32 (...) @fork()
store i32 %48, i32* %4
%49 = load i32, i32* %4
%50 = icmp eq i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %47
%52 = load i32, i32* %4
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %51, %47
br label %59
55: ; preds = %51
br label %56
56: ; preds = %55
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %43
59: ; preds = %54, %43
%60 = load i32, i32* %4
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %59
%66 = load i32, i32* %4
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %65
store i32 1, i32* %2
br label %70
69: ; preds = %65
store i32 0, i32* %2
br label %70
70: ; preds = %69, %68, %41
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@SIGQUIT = global i32 0
@SIGPIPE = global i32 0
@SIGTTOU = global i32 0
@SIGTTIN = global i32 0
@SIGCHLD = global i32 0
@.str = [15 x i8] c"fail to fork!\0A\00"
define i32 @InitDaemon(i32 %0) {
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %1
%7 = tail call i32 @setpgid(i32 0, i32 0)
%8 = load i32, i32* @SIGINT
%9 = load i32, i32* @SIG_IGN
%10 = tail call i32 @signal(i32 %8, i32 %9)
%11 = load i32, i32* @SIGHUP
%12 = load i32, i32* @SIG_IGN
%13 = tail call i32 @signal(i32 %11, i32 %12)
%14 = load i32, i32* @SIGQUIT
%15 = load i32, i32* @SIG_IGN
%16 = tail call i32 @signal(i32 %14, i32 %15)
%17 = load i32, i32* @SIGPIPE
%18 = load i32, i32* @SIG_IGN
%19 = tail call i32 @signal(i32 %17, i32 %18)
%20 = load i32, i32* @SIGTTOU
%21 = load i32, i32* @SIG_IGN
%22 = tail call i32 @signal(i32 %20, i32 %21)
%23 = load i32, i32* @SIGTTIN
%24 = load i32, i32* @SIG_IGN
%25 = tail call i32 @signal(i32 %23, i32 %24)
%26 = load i32, i32* @SIGCHLD
%27 = load i32, i32* @SIG_IGN
%28 = tail call i32 @signal(i32 %26, i32 %27)
%29 = tail call i32 (...) @fork()
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %6
%32 = tail call i32 @exit(i32 0)
unreachable
33: ; preds = %6
%34 = tail call i32 @umask(i32 0)
%35 = icmp slt i32 %0, 2
br i1 %35, label %49, label %36
36: ; preds = %33, %36
%37 = phi i32 [ %41, %36 ], [ 0, %33 ]
%38 = tail call i32 (...) @fork()
%39 = add i32 %38, -1
%40 = icmp ult i32 %39, -2
%41 = add nuw nsw i32 %37, 1
%42 = icmp slt i32 %41, %0
%43 = select i1 %40, i1 %42, i1 false
br i1 %43, label %36, label %44
44: ; preds = %36
switch i32 %38, label %48 [
i32 -1, label %45
i32 0, label %49
]
45: ; preds = %44
%46 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %44
br label %49
49: ; preds = %44, %33, %48
%50 = phi i32 [ 0, %48 ], [ -1, %33 ], [ 1, %44 ]
ret i32 %50
}