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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int run(int, char **);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stdout;
int daemonize(int argc, char *argv[]) {
  int ret = 0;
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("fork");
    exit(1);
  } else if (pid != 0) {
    fprintf(stdout, "parent exited!!\n");
    exit(ret);
  }
  setsid();
  if (chdir("/") < 0) {
    perror("chdir");
    exit(1);
  }
  close(0);
  open("/dev/null", O_RDWR);
  dup2(0, 1);
  dup2(0, 2);
  ret = run(argc, argv);
  return ret;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    11e1 <daemonize+0x41>
lea    0xe2e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
je     1209 <daemonize+0x69>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   10b0 <exit@plt>
jmp    120e <daemonize+0x6e>
mov    $0x0,%al
call   1070 <setsid@plt>
lea    0xdfa(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1241 <daemonize+0xa1>
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    0xdc6(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <run@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1212 <daemonize+0x72>
jne    121b <daemonize+0x7b>
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xe52(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1237 <daemonize+0x97>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <run@plt>
lea    0xde7(%rip),%rdi
jmp    123e <daemonize+0x9e>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xdda(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@stdout = global i32 0
@.str.1 = [17 x i8] c"parent exited!!\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemonize(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stdout
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %5
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %12
br label %21
21: ; preds = %20
%22 = call i32 (...) @setsid()
%23 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = call i32 @close(i32 0)
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = call i32 @dup2(i32 0, i32 1)
%33 = call i32 @dup2(i32 0, i32 2)
%34 = load i32, i32* %3
%35 = load i8**, i8*** %4
%36 = call i32 @run(i32 %34, i8** %35)
store i32 %36, i32* %5
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@stdout = global i32 0
@.str.1 = [17 x i8] c"parent exited!!\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemonize(i32 %0, i8** %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = icmp eq i64 %3, 0
br i1 %9, label %14, label %10
10: ; preds = %8
%11 = load i32, i32* @stdout
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %8
%15 = tail call i32 (...) @setsid()
%16 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = tail call i32 @close(i32 0)
%23 = load i32, i32* @O_RDWR
%24 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = tail call i32 @dup2(i32 0, i32 1)
%26 = tail call i32 @dup2(i32 0, i32 2)
%27 = tail call i32 @run(i32 %0, i8** %1)
ret i32 %27
}