#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_ERR;
scalar_t__ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ mced_log(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(void) {
  switch (fork()) {
  case -1:
    mced_log(LOG_ERR, "ERR: fork: %s\n", strerror(errno));
    return -1;
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  setsid();
  umask(0);
  if (chdir("/") < 0) {
    mced_log(LOG_ERR, "ERR: chdir(\"/\"): %s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
sub    $0xffffffff,%eax
je     11a2 <daemonize+0x32>
jmp    1192 <daemonize+0x22>
mov    -0x8(%rbp),%eax
test   %eax,%eax
je     11d9 <daemonize+0x69>
jmp    11de <daemonize+0x6e>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%edx
lea    0xe38(%rip),%rsi
call   1030 <mced_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <daemonize+0xde>
jmp    11ec <daemonize+0x7c>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe0e(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1247 <daemonize+0xd7>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xddb(%rip),%rsi
call   1030 <mced_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <daemonize+0xde>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11ad <daemonize+0x3d>
test   %eax,%eax
jne    11e5 <daemonize+0x75>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe75(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jns    11dc <daemonize+0x6c>
lea    0xe66(%rip),%rbx
jmp    11b4 <daemonize+0x44>
lea    0xe4c(%rip),%rbx
mov    0x2e0d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <mced_log@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [15 x i8] c"ERR: fork: %s\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [21 x i8] c"ERR: chdir(\22/\22): %s\0A\00"
define i32 @daemonize() {
%1 = alloca i32
%2 = call i32 (...) @fork()
switch i32 %2, label %9 [
i32 -1, label %3
i32 0, label %8
]
3: ; preds = %0
%4 = load i32, i32* @LOG_ERR
%5 = load i32, i32* @errno
%6 = call i32 @strerror(i32 %5)
%7 = call i32 @mced_log(i32 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 -1, i32* %1
br label %23
8: ; preds = %0
br label %12
9: ; preds = %0
%10 = load i32, i32* @EXIT_SUCCESS
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %8
%13 = call i32 (...) @setsid()
%14 = call i32 @umask(i32 0)
%15 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i32, i32* @LOG_ERR
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @mced_log(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %20)
store i32 -1, i32* %1
br label %23
22: ; preds = %12
store i32 0, i32* %1
br label %23
23: ; preds = %22, %17, %3
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [15 x i8] c"ERR: fork: %s\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [21 x i8] c"ERR: chdir(\22/\22): %s\0A\00"
define i32 @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 -1, label %10
i32 0, label %5
]
2: ; preds = %0
%3 = load i32, i32* @EXIT_SUCCESS
%4 = tail call i32 @exit(i32 %3)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = tail call i32 @umask(i32 0)
%8 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %5, %0
%11 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), %5 ]
%12 = load i32, i32* @LOG_ERR
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @mced_log(i32 %12, i8* %11, i32 %14)
br label %16
16: ; preds = %10, %5
%17 = phi i32 [ 0, %5 ], [ -1, %10 ]
ret i32 %17
}