#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int main(int argc, const char *argv[]) {
  int pid = fork();
  switch (pid) {
  case 0:
    setsid();
    chdir("/");
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    sleep(10);
    exit(0);
  case -1:
    printf("Fail: unable to fork\n");
    break;
  default:
    printf("OK: demon with pid %d is created\n", pid);
    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   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     1202 <main+0x92>
jmp    11a4 <main+0x34>
mov    -0x18(%rbp),%eax
test   %eax,%eax
jne    1215 <main+0xa5>
jmp    11b4 <main+0x44>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xe3e(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1226 <main+0xb6>
mov    -0x14(%rbp),%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <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     11a3 <main+0x23>
test   %eax,%eax
je     11b3 <main+0x33>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11af <main+0x2f>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xe3f(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [22 x i8] c"Fail: unable to fork\0A\00"
@.str.2 = [34 x i8] c"OK: demon with pid %d is created\0A\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 %22 [
i32 0, label %9
i32 -1, label %20
]
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 = load i32, i32* @STDIN_FILENO
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* @STDERR_FILENO
%17 = call i32 @close(i32 %16)
%18 = call i32 @sleep(i32 10)
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %25
22: ; preds = %2
%23 = load i32, i32* %6
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %20
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [34 x i8] c"OK: demon with pid %d is created\0A\00"
@str = [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 %17 [
i32 0, label %4
i32 -1, label %15
]
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 = load i32, i32* @STDIN_FILENO
%8 = tail call i32 @close(i32 %7)
%9 = load i32, i32* @STDOUT_FILENO
%10 = tail call i32 @close(i32 %9)
%11 = load i32, i32* @STDERR_FILENO
%12 = tail call i32 @close(i32 %11)
%13 = tail call i32 @sleep(i32 10)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %19
17: ; preds = %2
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %3)
br label %19
19: ; preds = %17, %15
ret i32 0
}