#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ my_printf(char *);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize() {
  my_printf("daemonize\n");
  pid_t pid = fork();
  if (pid < 0) {
    my_printf("Error fork failed\n");
    return 0;
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  if (setsid() < 0) {
    my_printf("Error setsid failed\n");
    return 0;
  }
  pid = fork();
  if (pid < 0) {
    my_printf("Error 2. fork failed\n");
    return 0;
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  my_printf(" successful\n");
  return 1;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1040 <my_printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1192 <daemonize+0x42>
lea    0xe8a(%rip),%rdi
call   1040 <my_printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    123f <daemonize+0xef>
cmpq   $0x0,-0x10(%rbp)
jle    11ab <daemonize+0x5b>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11b0 <daemonize+0x60>
mov    $0x0,%al
call   1030 <setsid@plt>
cmp    $0x0,%rax
jge    11d9 <daemonize+0x89>
lea    0xe56(%rip),%rdi
call   1040 <my_printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    123f <daemonize+0xef>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1207 <daemonize+0xb7>
lea    0xe3d(%rip),%rdi
call   1040 <my_printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    123f <daemonize+0xef>
cmpq   $0x0,-0x10(%rbp)
jle    1220 <daemonize+0xd0>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1225 <daemonize+0xd5>
xor    %edi,%edi
call   1050 <umask@plt>
lea    0xe16(%rip),%rdi
call   1040 <my_printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1040 <my_printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11a0 <daemonize+0x50>
jne    11c2 <daemonize+0x72>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <setsid@plt>
test   %rax,%rax
js     11a9 <daemonize+0x59>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11b2 <daemonize+0x62>
jne    11c2 <daemonize+0x72>
xor    %edi,%edi
call   1050 <umask@plt>
mov    $0x1,%ebx
lea    0xeab(%rip),%rdi
jmp    11b9 <daemonize+0x69>
lea    0xe64(%rip),%rdi
jmp    11b9 <daemonize+0x69>
lea    0xe6e(%rip),%rdi
jmp    11b9 <daemonize+0x69>
lea    0xe7a(%rip),%rdi
call   1040 <my_printf@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"daemonize\0A\00"
@.str.1 = [19 x i8] c"Error fork failed\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [21 x i8] c"Error setsid failed\0A\00"
@.str.3 = [22 x i8] c"Error 2. fork failed\0A\00"
@.str.4 = [13 x i8] c" successful\0A\00"
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i64
%3 = call i32 @my_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @my_printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %37
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %9
br label %16
16: ; preds = %15
%17 = call i64 (...) @setsid()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @my_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %37
21: ; preds = %16
%22 = call i64 (...) @fork()
store i64 %22, i64* %2
%23 = load i64, i64* %2
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @my_printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %37
27: ; preds = %21
%28 = load i64, i64* %2
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %27
br label %34
34: ; preds = %33
%35 = call i32 @umask(i32 0)
%36 = call i32 @my_printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %37
37: ; preds = %34, %25, %19, %7
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [11 x i8] c"daemonize\0A\00"
@.str.1 = [19 x i8] c"Error fork failed\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [21 x i8] c"Error setsid failed\0A\00"
@.str.3 = [22 x i8] c"Error 2. fork failed\0A\00"
@.str.4 = [13 x i8] c" successful\0A\00"
define i32 @daemonize() {
%1 = tail call i32 @my_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %22, label %4
4: ; preds = %0
%5 = icmp eq i64 %2, 0
br i1 %5, label %9, label %6
6: ; preds = %4
%7 = load i32, i32* @EXIT_SUCCESS
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %4
%10 = tail call i64 (...) @setsid()
%11 = icmp slt i64 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %9
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = icmp eq i64 %13, 0
br i1 %16, label %20, label %17
17: ; preds = %15
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %15
%21 = tail call i32 @umask(i32 0)
br label %22
22: ; preds = %12, %9, %0, %20
%23 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %20 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), %12 ]
%24 = phi i32 [ 1, %20 ], [ 0, %0 ], [ 0, %9 ], [ 0, %12 ]
%25 = tail call i32 @my_printf(i8* %23)
ret i32 %24
}