#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 PATH_MAX;
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *);
int get_num_descr();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  int i;
  char si[5];
  char fdstr[5];
  int num = 30;
  char fullpath[PATH_MAX];
  int origfds = 0, fds = 0;
  if (argc == 1) {
    i = 1;
    origfds = get_num_descr();
  } else {
    sscanf(argv[1], "%i", &i);
    sscanf(argv[2], "%i", &origfds);
  }
  if (argv[0][0] != '/')
    readlink("/proc/self/exe", fullpath, PATH_MAX);
  else
    strcpy(fullpath, argv[0]);
  i++;
  if (i <= num) {
    sprintf(si, "%i", i);
    sprintf(fdstr, "%i", origfds);
    execlp(fullpath, "selfexec", si, fdstr, (char *)0);
  }
  printf("Number of recursive calls = %i.\n", --i);
  fds = get_num_descr();
  printf("Number of file descriptors: %d\n", fds);
  if (fds > origfds) {
    printf("Failed: number of open descriptors was increased\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1e,-0x24(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e7 <main+0x77>
movl   $0x1,-0x14(%rbp)
mov    $0x0,%al
call   1050 <get_num_descr@plt>
mov    %eax,-0x3c(%rbp)
jmp    1217 <main+0xa7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0a(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdf2(%rip),%rsi
lea    -0x3c(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1248 <main+0xd8>
mov    -0x50(%rbp),%rsi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
lea    0xdc5(%rip),%rdi
call   1040 <readlink@plt>
jmp    1258 <main+0xe8>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     12b0 <main+0x140>
lea    -0x19(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd85(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x1e(%rbp),%rdi
mov    -0x3c(%rbp),%edx
lea    0xd72(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x19(%rbp),%rdx
lea    -0x1e(%rbp),%rcx
lea    0xd6c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <execlp@plt>
mov    -0x14(%rbp),%esi
add    $0xffffffff,%esi
mov    %esi,-0x14(%rbp)
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <get_num_descr@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%esi
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jle    130f <main+0x19f>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    131d <main+0x1ad>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rsp,%r13
mov    0x2e52(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x0,-0x30(%rbp)
cmp    $0x1,%edi
jne    11d2 <main+0x52>
movl   $0x1,-0x2c(%rbp)
xor    %eax,%eax
call   1060 <get_num_descr@plt>
mov    %eax,-0x30(%rbp)
jmp    11f9 <main+0x79>
mov    0x8(%r14),%rdi
lea    0xe23(%rip),%r15
lea    -0x2c(%rbp),%rdx
mov    %r15,%rsi
call   1080 <sscanf@plt>
mov    0x10(%r14),%rdi
lea    -0x30(%rbp),%rdx
mov    %r15,%rsi
call   1080 <sscanf@plt>
mov    (%r14),%rsi
cmpb   $0x2f,(%rsi)
jne    120b <main+0x8b>
mov    %rbx,%rdi
call   1030 <strcpy@plt>
jmp    121e <main+0x9e>
mov    (%r12),%edx
lea    0xded(%rip),%rdi
mov    %rbx,%rsi
call   1050 <readlink@plt>
mov    -0x2c(%rbp),%eax
lea    0x1(%rax),%edx
mov    %edx,-0x2c(%rbp)
cmp    $0x1d,%eax
jg     126f <main+0xef>
lea    0xdcd(%rip),%r14
lea    -0x3a(%rbp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <sprintf@plt>
mov    -0x30(%rbp),%edx
lea    -0x35(%rbp),%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1090 <sprintf@plt>
lea    0xdb7(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
call   10a0 <execlp@plt>
mov    -0x2c(%rbp),%edx
add    $0xffffffff,%edx
mov    %edx,-0x2c(%rbp)
lea    0xd9f(%rip),%rdi
xor    %r14d,%r14d
mov    %edx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1060 <get_num_descr@plt>
mov    %eax,%ebx
lea    0xda4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    -0x30(%rbp),%ebx
jle    12b8 <main+0x138>
lea    0xdaf(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %r13,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"%i\00"
@.str.1 = [15 x i8] c"/proc/self/exe\00"
@.str.2 = [9 x i8] c"selfexec\00"
@.str.3 = [33 x i8] c"Number of recursive calls = %i.\0A\00"
@.str.4 = [32 x i8] c"Number of file descriptors: %d\0A\00"
@.str.5 = [50 x i8] c"Failed: number of open descriptors was increased\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [5 x i8]
%8 = alloca [5 x i8]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 30, i32* %9
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %10
%18 = alloca i8, i64 %16
store i64 %16, i64* %11
store i32 0, i32* %12
store i32 0, i32* %13
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %2
store i32 1, i32* %6
%22 = call i32 (...) @get_num_descr()
store i32 %22, i32* %12
br label %32
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %12)
br label %32
32: ; preds = %23, %21
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 47
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* @PATH_MAX
%42 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %41)
br label %48
43: ; preds = %32
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 0
%46 = load i8*, i8** %45
%47 = call i32 @strcpy(i8* %18, i8* %46)
br label %48
48: ; preds = %43, %40
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = load i32, i32* %9
%53 = icmp sle i32 %51, %52
br i1 %53, label %54, label %64
54: ; preds = %48
%55 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%56 = load i32, i32* %6
%57 = call i32 @sprintf(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %56)
%58 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %12
%60 = call i32 @sprintf(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %59)
%61 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%62 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%63 = call i32 @execlp(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %61, i8* %62, i8* null)
br label %64
64: ; preds = %54, %48
%65 = load i32, i32* %6
%66 = add nsw i32 %65, -1
store i32 %66, i32* %6
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = call i32 (...) @get_num_descr()
store i32 %68, i32* %13
%69 = load i32, i32* %13
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %69)
%71 = load i32, i32* %13
%72 = load i32, i32* %12
%73 = icmp sgt i32 %71, %72
br i1 %73, label %74, label %76
74: ; preds = %64
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %77
76: ; preds = %64
store i32 0, i32* %3
store i32 1, i32* %14
br label %77
77: ; preds = %76, %74
%78 = load i8*, i8** %10
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"%i\00"
@.str.1 = [15 x i8] c"/proc/self/exe\00"
@.str.2 = [9 x i8] c"selfexec\00"
@.str.3 = [33 x i8] c"Number of recursive calls = %i.\0A\00"
@.str.4 = [32 x i8] c"Number of file descriptors: %d\0A\00"
@str = [49 x i8] c"Failed: number of open descriptors was increased\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = alloca [5 x i8]
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = tail call i8* @llvm.stacksave()
%13 = alloca i8, i64 %11
%14 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%15 = icmp eq i32 %0, 1
br i1 %15, label %16, label %18
16: ; preds = %2
store i32 1, i32* %3
%17 = tail call i32 (...) @get_num_descr()
store i32 %17, i32* %6
br label %25
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %25
25: ; preds = %18, %16
%26 = load i8*, i8** %1
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 47
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = load i32, i32* @PATH_MAX
%31 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %30)
br label %34
32: ; preds = %25
%33 = call i32 @strcpy(i8* %13, i8* %26)
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
%37 = icmp slt i32 %35, 30
br i1 %37, label %38, label %44
38: ; preds = %34
%39 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %36)
%40 = load i32, i32* %6
%41 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %40)
%42 = call i32 @execlp(i8* %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %8, i8* %9, i8* null)
%43 = load i32, i32* %3
br label %44
44: ; preds = %38, %34
%45 = phi i32 [ %43, %38 ], [ %36, %34 ]
%46 = add nsw i32 %45, -1
store i32 %46, i32* %3
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = call i32 (...) @get_num_descr()
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %6
%51 = icmp sgt i32 %48, %50
br i1 %51, label %52, label %54
52: ; preds = %44
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
br label %54
54: ; preds = %44, %52
%55 = phi i32 [ 1, %52 ], [ 0, %44 ]
ret i32 %55
}