#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*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(void) {
  pid_t pid = fork();
  if (pid < 0)
    return 1;
  if (pid > 0)
    return 1;
  if (setsid() < 0)
    return 1;
  pid = fork();
  if (pid < 0)
    return 1;
  if (pid > 0)
    return 1;
  umask(0);
  if (chdir("/") < 0)
    return 1;
  close(STDOUT_FILENO);
  close(STDIN_FILENO);
  close(STDERR_FILENO);
  openlog("usbwall", LOG_PID, LOG_DAEMON);
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    118a <daemonize+0x2a>
movl   $0x1,-0x4(%rbp)
jmp    126f <daemonize+0x10f>
cmpq   $0x0,-0x10(%rbp)
jle    11a1 <daemonize+0x41>
movl   $0x1,-0x4(%rbp)
jmp    126f <daemonize+0x10f>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11be <daemonize+0x5e>
movl   $0x1,-0x4(%rbp)
jmp    126f <daemonize+0x10f>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11e0 <daemonize+0x80>
movl   $0x1,-0x4(%rbp)
jmp    126f <daemonize+0x10f>
cmpq   $0x0,-0x10(%rbp)
jle    11f7 <daemonize+0x97>
movl   $0x1,-0x4(%rbp)
jmp    126f <daemonize+0x10f>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xdfb(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1220 <daemonize+0xc0>
movl   $0x1,-0x4(%rbp)
jmp    126f <daemonize+0x10f>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    0xd9f(%rip),%rdi
call   1070 <openlog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     1176 <daemonize+0x16>
mov    %ebx,%eax
pop    %rbx
ret
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1172 <daemonize+0x12>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    1172 <daemonize+0x12>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe64(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1172 <daemonize+0x12>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
lea    0xe19(%rip),%rdi
call   1070 <openlog@plt>
xor    %ebx,%ebx
jmp    1172 <daemonize+0x12>

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [8 x i8] c"usbwall\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define i32 @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %23
3: ; preds = %0
%4 = tail call i64 (...) @setsid()
%5 = icmp slt i64 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %6
%10 = tail call i32 @umask(i32 0)
%11 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp slt i64 %11, 0
br i1 %12, label %23, label %13
13: ; preds = %9
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @close(i32 %14)
%16 = load i32, i32* @STDIN_FILENO
%17 = tail call i32 @close(i32 %16)
%18 = load i32, i32* @STDERR_FILENO
%19 = tail call i32 @close(i32 %18)
%20 = load i32, i32* @LOG_PID
%21 = load i32, i32* @LOG_DAEMON
%22 = tail call i32 @openlog(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
br label %23
23: ; preds = %9, %6, %3, %0, %13
%24 = phi i32 [ 0, %13 ], [ 1, %0 ], [ 1, %3 ], [ 1, %6 ], [ 1, %9 ]
ret i32 %24
}