#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*/ O_WRONLY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wd_timer();
int main(void) {
  int cur_fd;
  switch (fork()) {
  case -1:
    perror("fork()");
    exit(1);
  case 0:
    if (chdir("/"))
      perror("chdir()");
    cur_fd = open("/dev/null", O_WRONLY);
    if (cur_fd > 0) {
      dup2(cur_fd, 0);
      dup2(cur_fd, 1);
      dup2(cur_fd, 2);
    }
    break;
  default:
    exit(0);
  }
  sleep(120);
  wd_timer();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11b9 <main+0x39>
jmp    11a9 <main+0x29>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     11cf <main+0x4f>
jmp    123b <main+0xbb>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe31(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%eax
je     11f0 <main+0x70>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0xe11(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1236 <main+0xb6>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
jmp    1242 <main+0xc2>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x78,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1070 <wd_timer@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11fb <main+0x7b>
lea    0xe74(%rip),%rdi
call   1030 <chdir@plt>
test   %eax,%eax
jne    11ed <main+0x6d>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
jle    11d8 <main+0x58>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    $0x78,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1070 <wd_timer@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
jmp    119c <main+0x1c>
cmp    $0xffffffff,%eax
jne    1216 <main+0x96>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [8 x i8] c"chdir()\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @fork()
switch i32 %3, label %25 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %0
%5 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i32, i32* %2
%19 = call i32 @dup2(i32 %18, i32 0)
%20 = load i32, i32* %2
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = load i32, i32* %2
%23 = call i32 @dup2(i32 %22, i32 2)
br label %24
24: ; preds = %17, %12
br label %27
25: ; preds = %0
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %24
%28 = call i32 @sleep(i32 120)
%29 = call i32 (...) @wd_timer()
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [8 x i8] c"chdir()\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %18 [
i32 -1, label %2
i32 0, label %5
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @exit(i32 1)
unreachable
5: ; preds = %0
%6 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = tail call i32 @dup2(i32 %12, i32 0)
%16 = tail call i32 @dup2(i32 %12, i32 1)
%17 = tail call i32 @dup2(i32 %12, i32 2)
br label %20
18: ; preds = %0
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %10, %14
%21 = tail call i32 @sleep(i32 120)
%22 = tail call i32 (...) @wd_timer()
ret i32 0
}