#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;
typedef int /*<<< orphan*/ currdir;

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int deamon_init(void) {
  char currdir[1000];
  pid_t pid;
  getcwd(currdir, sizeof(currdir));
  if ((pid = fork()) < 0) {
    return -1;
  } else if (pid != 0) {
    exit(0);
  }
  setsid();
  chdir(currdir);
  umask(0);
  return 0;
}


x86 assembly (clang, -O0):
<deamon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
lea    -0x3f0(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <getcwd@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x3f8(%rbp)
cmp    $0x0,%rax
jge    11a0 <deamon_init+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <deamon_init+0x7b>
cmpq   $0x0,-0x3f8(%rbp)
je     11b5 <deamon_init+0x55>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11ba <deamon_init+0x5a>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    -0x3f0(%rbp),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deamon_init>:
push   %rbx
sub    $0x3f0,%rsp
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1040 <getcwd@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     119d <deamon_init+0x3d>
jne    11ad <deamon_init+0x4d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
mov    %rsp,%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
jmp    11a2 <deamon_init+0x42>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x3f0,%rsp
pop    %rbx
ret
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
define i32 @deamon_init() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%5 = call i32 @getcwd(i8* %4, i32 1000)
%6 = call i64 (...) @fork()
store i64 %6, i64* %3
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i32 -1, i32* %1
br label %20
9: ; preds = %0
%10 = load i64, i64* %3
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %9
br label %15
15: ; preds = %14
%16 = call i32 (...) @setsid()
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%18 = call i32 @chdir(i8* %17)
%19 = call i32 @umask(i32 0)
store i32 0, i32* %1
br label %20
20: ; preds = %15, %8
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
define i32 @deamon_init() {
%1 = alloca [1000 x i8]
%2 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%3 = call i32 @getcwd(i8* %2, i32 1000)
%4 = call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %0
%7 = icmp eq i64 %4, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = call i32 (...) @setsid()
%12 = call i32 @chdir(i8* %2)
%13 = call i32 @umask(i32 0)
br label %14
14: ; preds = %0, %10
%15 = phi i32 [ 0, %10 ], [ -1, %0 ]
ret i32 %15
}