#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*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int pid = fork();
  switch (pid) {
  case 0:
    setsid();
    chdir("/");
    printf("%d\n", getpid());
    close(stdin);
    close(stdout);
    close(stderr);
    sleep(10);
    exit(0);
  case -1:
    printf("Fail: unable to fork");
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     1229 <main+0xa9>
jmp    11b4 <main+0x34>
mov    -0x18(%rbp),%eax
test   %eax,%eax
jne    1237 <main+0xb7>
jmp    11c4 <main+0x44>
mov    $0x0,%al
call   1070 <setsid@plt>
lea    0xe2e(%rip),%rdi
call   1040 <chdir@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11f6 <main+0x76>
test   %eax,%eax
jne    1204 <main+0x84>
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xe61(%rip),%rdi
call   1040 <chdir@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe50(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe09(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [21 x i8] c"Fail: unable to fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
switch i32 %8, label %24 [
i32 0, label %9
i32 -1, label %22
]
9: ; preds = %2
%10 = call i32 (...) @setsid()
%11 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @stdin
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* @stdout
%17 = call i32 @close(i32 %16)
%18 = load i32, i32* @stderr
%19 = call i32 @close(i32 %18)
%20 = call i32 @sleep(i32 10)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %2, %22
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [21 x i8] c"Fail: unable to fork\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %19 [
i32 0, label %4
i32 -1, label %17
]
4: ; preds = %2
%5 = tail call i32 (...) @setsid()
%6 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @stdin
%10 = tail call i32 @close(i32 %9)
%11 = load i32, i32* @stdout
%12 = tail call i32 @close(i32 %11)
%13 = load i32, i32* @stderr
%14 = tail call i32 @close(i32 %13)
%15 = tail call i32 @sleep(i32 10)
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %2, %17
ret i32 0
}