#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 */
struct rlimit {
  int rlim_max;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main() {
  umask(0);
  struct rlimit rl;
  if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
    perror("getrlimit");
    exit(-1);
  }
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(-1);
  } else if (pid != 0)
    exit(0);
  setsid();
  chdir("/");
  if (chdir("/") < 0) {
    perror("chdir");
    exit(-1);
  }
  if (rl.rlim_max == RLIM_INFINITY) {
    rl.rlim_max = 1024;
  }
  int i;
  for (i = 0; i < rl.rlim_max; i++) {
    close(i);
  }
  while (1)
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1090 <getrlimit@plt>
cmp    $0x0,%rax
jge    11c8 <main+0x48>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11f4 <main+0x74>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     1206 <main+0x86>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    120b <main+0x8b>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xdf6(%rip),%rdi
call   1030 <chdir@plt>
lea    0xdea(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    124a <main+0xca>
lea    0xdd6(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%eax
jne    1263 <main+0xe3>
movl   $0x400,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    128c <main+0x10c>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    126a <main+0xea>
jmp    1291 <main+0x111>
jmp    1291 <main+0x111>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1090 <getrlimit@plt>
test   %rax,%rax
js     1212 <main+0x92>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     121b <main+0x9b>
jne    1224 <main+0xa4>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe4f(%rip),%rbx
mov    %rbx,%rdi
call   1030 <chdir@plt>
mov    %rbx,%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     122b <main+0xab>
mov    0x8(%rsp),%eax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
jne    11ee <main+0x6e>
movl   $0x400,0x8(%rsp)
jmp    11f2 <main+0x72>
test   %eax,%eax
jle    1210 <main+0x90>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1200 <main+0x80>
jmp    1210 <main+0x90>
lea    0xde7(%rip),%rdi
jmp    1232 <main+0xb2>
lea    0xde8(%rip),%rdi
jmp    1232 <main+0xb2>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@RLIM_INFINITY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.rlimit
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @umask(i32 0)
%6 = load i32, i32* @RLIMIT_NOFILE
%7 = call i64 @getrlimit(i32 %6, %struct.rlimit* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i64 (...) @fork()
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %12
%20 = load i64, i64* %3
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %19
br label %25
25: ; preds = %24
%26 = call i32 (...) @setsid()
%27 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %25
%34 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @RLIM_INFINITY
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
store i32 1024, i32* %39
br label %40
40: ; preds = %38, %33
store i32 0, i32* %4
br label %41
41: ; preds = %49, %40
%42 = load i32, i32* %4
%43 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%44 = load i32, i32* %43
%45 = icmp slt i32 %42, %44
br i1 %45, label %46, label %52
46: ; preds = %41
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
br label %41
52: ; preds = %41
br label %53
53: ; preds = %52, %53
br label %53
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@RLIM_INFINITY = global i32 0
define i32 @main() {
%1 = alloca %struct.rlimit
%2 = tail call i32 @umask(i32 0)
%3 = bitcast %struct.rlimit* %1 to i8*
%4 = load i32, i32* @RLIMIT_NOFILE
%5 = call i64 @getrlimit(i32 %4, %struct.rlimit* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %10
%17 = icmp eq i64 %11, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %16
%21 = call i32 (...) @setsid()
%22 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %20
%29 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @RLIM_INFINITY
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 1024, i32* %29
br label %36
34: ; preds = %28
%35 = icmp sgt i32 %30, 0
br i1 %35, label %36, label %43
36: ; preds = %33, %34
br label %37
37: ; preds = %36, %37
%38 = phi i32 [ %40, %37 ], [ 0, %36 ]
%39 = call i32 @close(i32 %38)
%40 = add nuw nsw i32 %38, 1
%41 = load i32, i32* %29
%42 = icmp slt i32 %40, %41
br i1 %42, label %37, label %43
43: ; preds = %37, %34
br label %44
44: ; preds = %43, %44
br label %44
}