#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int a_fork(int nochdir, int noclose) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0)
    return -1;
  if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  sid = setsid();
  if (sid < 0)
    exit(EXIT_FAILURE);
  if (!nochdir) {
    if (chdir("/") < 0)
      return -1;
  }
  if (!noclose) {
    if (!freopen("/dev/null", "r", stdin) ||
        !freopen("/dev/null", "w", stdout) ||
        !freopen("/dev/null", "w", stderr))
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<a_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1190 <a_fork+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <a_fork+0x136>
cmpq   $0x0,-0x18(%rbp)
jle    11a9 <a_fork+0x49>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11d4 <a_fork+0x74>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1205 <a_fork+0xa5>
lea    0xe1b(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1200 <a_fork+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <a_fork+0x136>
jmp    1205 <a_fork+0xa5>
cmpl   $0x0,-0xc(%rbp)
jne    128f <a_fork+0x12f>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xde3(%rip),%rdi
lea    0xde6(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%eax
je     127e <a_fork+0x11e>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
lea    0xdbe(%rip),%rdi
lea    0xdc3(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%eax
je     127e <a_fork+0x11e>
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
lea    0xd99(%rip),%rdi
lea    0xd9e(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%eax
jne    128a <a_fork+0x12a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <a_fork+0x136>
jmp    128f <a_fork+0x12f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a_fork>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     11b3 <a_fork+0x53>
jne    121d <a_fork+0xbd>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     122b <a_fork+0xcb>
test   %ebx,%ebx
jne    11ac <a_fork+0x4c>
lea    0xe5e(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11b3 <a_fork+0x53>
test   %ebp,%ebp
je     11bb <a_fork+0x5b>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
lea    0xe37(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1040 <freopen@plt>
test   %eax,%eax
je     11b3 <a_fork+0x53>
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    0xe17(%rip),%rdi
lea    0xe1c(%rip),%rsi
call   1040 <freopen@plt>
test   %eax,%eax
je     11b3 <a_fork+0x53>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
lea    0xdf7(%rip),%rdi
lea    0xdfc(%rip),%rsi
call   1040 <freopen@plt>
test   %eax,%eax
jne    11b0 <a_fork+0x50>
jmp    11b3 <a_fork+0x53>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define i32 @a_fork(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %52
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @EXIT_SUCCESS
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %12
%19 = call i32 @umask(i32 0)
%20 = call i64 (...) @setsid()
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %3
br label %52
33: ; preds = %29
br label %34
34: ; preds = %33, %26
%35 = load i32, i32* %5
%36 = icmp ne i32 %35, 0
br i1 %36, label %51, label %37
37: ; preds = %34
%38 = load i32, i32* @stdin
%39 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %37
%42 = load i32, i32* @stdout
%43 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %45, %41, %37
store i32 -1, i32* %3
br label %52
50: ; preds = %45
br label %51
51: ; preds = %50, %34
store i32 0, i32* %3
br label %52
52: ; preds = %51, %49, %32, %11
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define i32 @a_fork(i32 %0, i32 %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %37, label %5
5: ; preds = %2
%6 = icmp eq i64 %3, 0
br i1 %6, label %10, label %7
7: ; preds = %5
%8 = load i32, i32* @EXIT_SUCCESS
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %5
%11 = tail call i32 @umask(i32 0)
%12 = tail call i64 (...) @setsid()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = icmp eq i32 %0, 0
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp slt i64 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %19, %17
%23 = icmp eq i32 %1, 0
br i1 %23, label %24, label %36
24: ; preds = %22
%25 = load i32, i32* @stdin
%26 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %37, label %28
28: ; preds = %24
%29 = load i32, i32* @stdout
%30 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32, %22
br label %37
37: ; preds = %24, %28, %32, %19, %2, %36
%38 = phi i32 [ 0, %36 ], [ -1, %2 ], [ -1, %19 ], [ -1, %32 ], [ -1, %28 ], [ -1, %24 ]
ret i32 %38
}