#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void daemonize(char *name) {
  pid_t result = fork();
  if (result == -1) {
    fprintf(stderr, "%s: fork(): %s\n", name, strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (result > 0)
    exit(EXIT_SUCCESS);
  setsid();
  result = fork();
  while (result == -1) {
    fprintf(stderr, "%s: fork(): %s\n", name, strerror(errno));
    sleep(1);
    result = fork();
  }
  if (result > 0)
    exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c6 <daemonize+0x66>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe4d(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jle    11de <daemonize+0x7e>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1030 <setsid@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    124a <daemonize+0xea>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
jmp    11ef <daemonize+0x8f>
cmpl   $0x0,-0xc(%rbp)
jle    1262 <daemonize+0x102>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11fd <daemonize+0x9d>
test   %eax,%eax
jg     11ef <daemonize+0x8f>
xor    %eax,%eax
call   1030 <setsid@plt>
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
jne    11e2 <daemonize+0x82>
mov    0x2e47(%rip),%r15
mov    0x2e38(%rip),%r12
lea    0xe59(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%r15),%ebp
mov    (%r12),%edi
call   1060 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11b0 <daemonize+0x50>
test   %eax,%eax
jg     11ef <daemonize+0x8f>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebp
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xde5(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"%s: fork(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define void @daemonize(i8* %0) {
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %11
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* @EXIT_SUCCESS
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %11
%17 = tail call i32 (...) @setsid()
%18 = tail call i32 (...) @fork()
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %28
20: ; preds = %16, %20
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %23)
%25 = tail call i32 @sleep(i32 1)
%26 = tail call i32 (...) @fork()
%27 = icmp eq i32 %26, -1
br i1 %27, label %20, label %28
28: ; preds = %20, %16
%29 = phi i32 [ %18, %16 ], [ %26, %20 ]
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @EXIT_SUCCESS
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %28
ret void
}