#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 atoi(char *);
int chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int);
int setsid();
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  int count = 2;
  int sleep_time = 1;
  if (argc > 1) {
    count = atoi(argv[1]);
  }
  if (argc > 2) {
    sleep_time = atoi(argv[2]);
  }
  for (int i = 0; i < count; i++) {
    pid_t child_pid;
    child_pid = fork();
    if (child_pid == 0) {
      if (setsid() == -1)
        return 1;
      if (chdir("/") == -1)
        return 1;
      close(0);
      close(1);
      close(2);
      open("/dev/null", 0002);
      dup(0);
      dup(0);
      sleep(sleep_time);
      exit(0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ce <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11e8 <main+0x58>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12aa <main+0x11a>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1297 <main+0x107>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    122d <main+0x9d>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x121>
lea    0xdcc(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    124e <main+0xbe>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x121>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
lea    0xd92(%rip),%rdi
mov    $0x2,%esi
call   1070 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    -0x18(%rbp),%edi
call   10a0 <sleep@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    129c <main+0x10c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ef <main+0x5f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     11c9 <main+0x39>
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    $0x1,%r14d
cmp    $0x2,%ebx
je     11c3 <main+0x33>
mov    0x10(%r15),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
test   %ebp,%ebp
jg     11e0 <main+0x50>
jmp    11f1 <main+0x61>
mov    $0x2,%ebp
mov    $0x1,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11f5 <main+0x65>
add    $0xffffffff,%ebp
jne    11e0 <main+0x50>
xor    %ebp,%ebp
jmp    1217 <main+0x87>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
je     1217 <main+0x87>
lea    0xdf3(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1224 <main+0x94>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
lea    0xdbc(%rip),%rdi
mov    $0x2,%esi
call   1070 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    %r14d,%edi
call   10a0 <sleep@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %16
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp eq i32 %0, 2
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
br label %13
13: ; preds = %9, %4
%14 = phi i32 [ %12, %9 ], [ 1, %4 ]
%15 = icmp sgt i32 %7, 0
br i1 %15, label %16, label %41
16: ; preds = %2, %13
%17 = phi i32 [ %14, %13 ], [ 1, %2 ]
%18 = phi i32 [ %7, %13 ], [ 2, %2 ]
br label %21
19: ; preds = %21
%20 = icmp eq i32 %25, %18
br i1 %20, label %41, label %21
21: ; preds = %16, %19
%22 = phi i32 [ %25, %19 ], [ 0, %16 ]
%23 = tail call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
%25 = add nuw nsw i32 %22, 1
br i1 %24, label %26, label %19
26: ; preds = %21
%27 = tail call i32 (...) @setsid()
%28 = icmp eq i32 %27, -1
br i1 %28, label %41, label %29
29: ; preds = %26
%30 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i32 %30, -1
br i1 %31, label %41, label %32
32: ; preds = %29
%33 = tail call i32 @close(i32 0)
%34 = tail call i32 @close(i32 1)
%35 = tail call i32 @close(i32 2)
%36 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 2)
%37 = tail call i32 @dup(i32 0)
%38 = tail call i32 @dup(i32 0)
%39 = tail call i32 @sleep(i32 %17)
%40 = tail call i32 @exit(i32 0)
unreachable
41: ; preds = %19, %13, %29, %26
%42 = phi i32 [ 1, %29 ], [ 1, %26 ], [ 0, %13 ], [ 0, %19 ]
ret i32 %42
}