#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 int /*<<< orphan*/ fds;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char *, char **);
char **malloc(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  char **exec_argv;
  int fds[12000];
  int max = sizeof(fds) / sizeof(fds[0]);
  int i;
  for (i = 0; i < max; i++) {
    fds[i] = open("/dev/null", 0, O_WRONLY);
    if (fds[i] < 0) {
      if (i < 1200)
        return 17;
      max = i;
      break;
    }
  }
  for (i = max - 150; i < max; i++)
    close(fds[i]);
  if (argc < 2)
    return 1;
  exec_argv = malloc(sizeof(char *) * argc);
  if (!exec_argv)
    return 2;
  for (i = 0; i < argc - 1; i++) {
    exec_argv[i] = argv[i + 1];
  }
  exec_argv[i] = NULL;
  execvp(exec_argv[0], exec_argv);
  perror("Failed to exec");
  return 3;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xbbb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2ee0,-0xbba4(%rbp)
movl   $0x0,-0xbba8(%rbp)
mov    -0xbba8(%rbp),%eax
cmp    -0xbba4(%rbp),%eax
jge    1211 <main+0xc1>
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
lea    0xe61(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,%ecx
movslq -0xbba8(%rbp),%rax
mov    %ecx,-0xbba0(%rbp,%rax,4)
movslq -0xbba8(%rbp),%rax
cmpl   $0x0,-0xbba0(%rbp,%rax,4)
jge    11f8 <main+0xa8>
cmpl   $0x4b0,-0xbba8(%rbp)
jge    11e7 <main+0x97>
movl   $0x11,-0x4(%rbp)
jmp    1326 <main+0x1d6>
mov    -0xbba8(%rbp),%eax
mov    %eax,-0xbba4(%rbp)
jmp    1211 <main+0xc1>
jmp    11fd <main+0xad>
mov    -0xbba8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xbba8(%rbp)
jmp    117d <main+0x2d>
mov    -0xbba4(%rbp),%eax
sub    $0x96,%eax
mov    %eax,-0xbba8(%rbp)
mov    -0xbba8(%rbp),%eax
cmp    -0xbba4(%rbp),%eax
jge    125b <main+0x10b>
movslq -0xbba8(%rbp),%rax
mov    -0xbba0(%rbp,%rax,4),%edi
call   1030 <close@plt>
mov    -0xbba8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xbba8(%rbp)
jmp    1222 <main+0xd2>
cmpl   $0x2,-0x8(%rbp)
jge    1271 <main+0x121>
movl   $0x1,-0x4(%rbp)
jmp    1326 <main+0x1d6>
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    129b <main+0x14b>
movl   $0x2,-0x4(%rbp)
jmp    1326 <main+0x1d6>
movl   $0x0,-0xbba8(%rbp)
mov    -0xbba8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12f0 <main+0x1a0>
mov    -0x10(%rbp),%rax
mov    -0xbba8(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x18(%rbp),%rax
movslq -0xbba8(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xbba8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xbba8(%rbp)
jmp    12a5 <main+0x155>
mov    -0x18(%rbp),%rax
movslq -0xbba8(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xcf0(%rip),%rdi
call   1060 <perror@plt>
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xbbb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xbb88,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebp,%ebp
mov    0x2e58(%rip),%r13
lea    0xe79(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,(%rsp,%rbp,4)
test   %eax,%eax
js     11b9 <main+0x59>
add    $0x1,%rbp
cmp    $0x2ee0,%rbp
jne    1190 <main+0x30>
mov    $0x2ee0,%ebp
jmp    11c6 <main+0x66>
mov    $0x11,%eax
cmp    $0x4b0,%ebp
jb     1216 <main+0xb6>
mov    %ebp,%ebx
add    $0xffffffffffffff6a,%rbx
movslq %ebp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp,%rbx,4),%edi
call   1030 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11e0 <main+0x80>
mov    $0x1,%eax
cmp    $0x2,%r12d
jl     1216 <main+0xb6>
lea    0x0(,%r12,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    $0x2,%eax
test   %r15,%r15
jne    1228 <main+0xc8>
add    $0xbb88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0xffffffff,%r12d
add    $0x8,%r14
lea    0x0(,%r12,8),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
movq   $0x0,(%r15,%r12,8)
mov    (%r15),%rdi
mov    %r15,%rsi
call   1080 <execvp@plt>
lea    0xdad(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%eax
jmp    1216 <main+0xb6>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.1 = [15 x i8] c"Failed to exec\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca [12000 x i32]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12000, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %32, %2
%11 = load i32, i32* %9
%12 = load i32, i32* %8
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %35
14: ; preds = %10
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %15)
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [12000 x i32], [12000 x i32]* %7, i64 0, i64 %18
store i32 %16, i32* %19
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [12000 x i32], [12000 x i32]* %7, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %14
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 1200
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 17, i32* %3
br label %95
29: ; preds = %25
%30 = load i32, i32* %9
store i32 %30, i32* %8
br label %35
31: ; preds = %14
br label %32
32: ; preds = %31
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9
br label %10
35: ; preds = %29, %10
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 150
store i32 %37, i32* %9
br label %38
38: ; preds = %48, %35
%39 = load i32, i32* %9
%40 = load i32, i32* %8
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [12000 x i32], [12000 x i32]* %7, i64 0, i64 %44
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %42
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %38
51: ; preds = %38
%52 = load i32, i32* %4
%53 = icmp slt i32 %52, 2
br i1 %53, label %54, label %55
54: ; preds = %51
store i32 1, i32* %3
br label %95
55: ; preds = %51
%56 = load i32, i32* %4
%57 = sext i32 %56 to i64
%58 = mul i64 8, %57
%59 = trunc i64 %58 to i32
%60 = call i8** @malloc(i32 %59)
store i8** %60, i8*** %6
%61 = load i8**, i8*** %6
%62 = icmp ne i8** %61, null
br i1 %62, label %64, label %63
63: ; preds = %55
store i32 2, i32* %3
br label %95
64: ; preds = %55
store i32 0, i32* %9
br label %65
65: ; preds = %81, %64
%66 = load i32, i32* %9
%67 = load i32, i32* %4
%68 = sub nsw i32 %67, 1
%69 = icmp slt i32 %66, %68
br i1 %69, label %70, label %84
70: ; preds = %65
%71 = load i8**, i8*** %5
%72 = load i32, i32* %9
%73 = add nsw i32 %72, 1
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8*, i8** %71, i64 %74
%76 = load i8*, i8** %75
%77 = load i8**, i8*** %6
%78 = load i32, i32* %9
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8*, i8** %77, i64 %79
store i8* %76, i8** %80
br label %81
81: ; preds = %70
%82 = load i32, i32* %9
%83 = add nsw i32 %82, 1
store i32 %83, i32* %9
br label %65
84: ; preds = %65
%85 = load i8**, i8*** %6
%86 = load i32, i32* %9
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8*, i8** %85, i64 %87
store i8* null, i8** %88
%89 = load i8**, i8*** %6
%90 = getelementptr inbounds i8*, i8** %89, i64 0
%91 = load i8*, i8** %90
%92 = load i8**, i8*** %6
%93 = call i32 @execvp(i8* %91, i8** %92)
%94 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %3
br label %95
95: ; preds = %84, %63, %54, %28
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.1 = [15 x i8] c"Failed to exec\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [12000 x i32]
%4 = bitcast [12000 x i32]* %3 to i8*
br label %5
5: ; preds = %2, %14
%6 = phi i64 [ 0, %2 ], [ %15, %14 ]
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %7)
%9 = getelementptr inbounds [12000 x i32], [12000 x i32]* %3, i64 0, i64 %6
store i32 %8, i32* %9
%10 = icmp slt i32 %8, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = trunc i64 %6 to i32
%13 = icmp ult i32 %12, 1200
br i1 %13, label %48, label %17
14: ; preds = %5
%15 = add nuw nsw i64 %6, 1
%16 = icmp eq i64 %15, 12000
br i1 %16, label %17, label %5
17: ; preds = %14, %11
%18 = phi i32 [ %12, %11 ], [ 12000, %14 ]
%19 = zext i32 %18 to i64
%20 = add nsw i64 %19, -150
%21 = sext i32 %18 to i64
br label %22
22: ; preds = %17, %22
%23 = phi i64 [ %20, %17 ], [ %27, %22 ]
%24 = getelementptr inbounds [12000 x i32], [12000 x i32]* %3, i64 0, i64 %23
%25 = load i32, i32* %24
%26 = tail call i32 @close(i32 %25)
%27 = add nsw i64 %23, 1
%28 = icmp eq i64 %27, %21
br i1 %28, label %29, label %22
29: ; preds = %22
%30 = icmp slt i32 %0, 2
br i1 %30, label %48, label %31
31: ; preds = %29
%32 = shl i32 %0, 3
%33 = tail call i8** @malloc(i32 %32)
%34 = icmp eq i8** %33, null
br i1 %34, label %48, label %35
35: ; preds = %31
%36 = bitcast i8** %33 to i8*
%37 = add nsw i32 %0, -1
%38 = getelementptr i8*, i8** %1, i64 1
%39 = bitcast i8** %38 to i8*
%40 = zext i32 %37 to i64
%41 = shl nuw nsw i64 %40, 3
%42 = add nsw i32 %0, -1
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %33, i64 %43
store i8* null, i8** %44
%45 = load i8*, i8** %33
%46 = tail call i32 @execvp(i8* %45, i8** %33)
%47 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %31, %29, %11, %35
%49 = phi i32 [ 3, %35 ], [ 17, %11 ], [ 1, %29 ], [ 2, %31 ]
ret i32 %49
}