#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 /*<<< orphan*/ rlim_max;
  int /*<<< orphan*/ rlim_cur;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ RLIMIT_STACK;
int /*<<< orphan*/ SYS_brk;
int execv(char *, char *const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getrlimit(int /*<<< orphan*/, struct rlimit *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
long syscall(int /*<<< orphan*/, long);
int main(int argc, char *argv[]) {
  char *const dummy_argv[] = {"test", "stage2", NULL};
  long brk1, brk2;
  int status;
  struct rlimit rlimit;
  switch (argc) {
  case 1:
    status = getrlimit(RLIMIT_STACK, &rlimit);
    if (status < 0)
      exit(EXIT_FAILURE);
    rlimit.rlim_cur = rlimit.rlim_max;
    status = setrlimit(RLIMIT_STACK, &rlimit);
    if (status < 0)
      exit(EXIT_FAILURE);
    return execv(argv[0], dummy_argv);
  default:
    brk1 = syscall(SYS_brk, 0);
    brk2 = syscall(SYS_brk, brk1 + 1024 * 1024);
    exit(brk1 != brk2 ? EXIT_SUCCESS : EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c83(%rip),%rax
mov    %rax,-0x20(%rbp)
movaps 0x2c68(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
jne    1203 <main+0xb3>
jmp    118d <main+0x3d>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
call   1060 <getrlimit@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11ba <main+0x6a>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
call   1040 <setrlimit@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11ed <main+0x9d>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <execv@plt>
add    $0x60,%rsp
pop    %rbp
ret
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <syscall@plt>
mov    %rax,-0x38(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
add    $0x100000,%rsi
call   1030 <syscall@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     1255 <main+0x105>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
jmp    1261 <main+0x111>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2c92(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2c76(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x1,%edi
jne    11bb <main+0x6b>
mov    %rsi,%r14
mov    0x2e52(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1060 <getrlimit@plt>
test   %eax,%eax
js     11e3 <main+0x93>
mov    0x8(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1040 <setrlimit@plt>
test   %eax,%eax
js     11e3 <main+0x93>
mov    (%r14),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execv@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e1e(%rip),%r14
mov    (%r14),%edi
xor    %esi,%esi
call   1030 <syscall@plt>
mov    %rax,%rbx
mov    (%r14),%edi
lea    0x100000(%rax),%rsi
call   1030 <syscall@plt>
cmp    %rax,%rbx
jne    11f1 <main+0xa1>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@.str = [5 x i8] c"test\00"
@.str.1 = [7 x i8] c"stage2\00"
@__const.main.dummy_argv = [3 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0), i8* null]
@RLIMIT_STACK = global i32 0
@EXIT_FAILURE = global i32 0
@SYS_brk = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i8*]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.rlimit
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [3 x i8*]* %6 to i8*
%12 = load i32, i32* %4
switch i32 %12, label %38 [
i32 1, label %13
]
13: ; preds = %2
%14 = load i32, i32* @RLIMIT_STACK
%15 = call i32 @getrlimit(i32 %14, %struct.rlimit* %10)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
%22 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %10, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @RLIMIT_STACK
%26 = call i32 @setrlimit(i32 %25, %struct.rlimit* %10)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%37 = call i32 @execv(i8* %35, i8** %36)
ret i32 %37
38: ; preds = %2
%39 = load i32, i32* @SYS_brk
%40 = call i64 @syscall(i32 %39, i64 0)
store i64 %40, i64* %7
%41 = load i32, i32* @SYS_brk
%42 = load i64, i64* %7
%43 = add nsw i64 %42, 1048576
%44 = call i64 @syscall(i32 %41, i64 %43)
store i64 %44, i64* %8
%45 = load i64, i64* %7
%46 = load i64, i64* %8
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %38
%49 = load i32, i32* @EXIT_SUCCESS
br label %52
50: ; preds = %38
%51 = load i32, i32* @EXIT_FAILURE
br label %52
52: ; preds = %50, %48
%53 = phi i32 [ %49, %48 ], [ %51, %50 ]
%54 = call i32 @exit(i32 %53)
unreachable
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@.str = [5 x i8] c"test\00"
@.str.1 = [7 x i8] c"stage2\00"
@__const.main.dummy_argv = [3 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0), i8* null]
@RLIMIT_STACK = global i32 0
@EXIT_FAILURE = global i32 0
@SYS_brk = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8*]
%4 = alloca %struct.rlimit
%5 = bitcast [3 x i8*]* %3 to i8*
%6 = bitcast %struct.rlimit* %4 to i8*
%7 = icmp eq i32 %0, 1
br i1 %7, label %8, label %29
8: ; preds = %2
%9 = load i32, i32* @RLIMIT_STACK
%10 = call i32 @getrlimit(i32 %9, %struct.rlimit* %4)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @RLIMIT_STACK
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %4)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %15
%26 = load i8*, i8** %1
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%28 = call i32 @execv(i8* %26, i8** %27)
ret i32 %28
29: ; preds = %2
%30 = load i32, i32* @SYS_brk
%31 = tail call i64 @syscall(i32 %30, i64 0)
%32 = load i32, i32* @SYS_brk
%33 = add nsw i64 %31, 1048576
%34 = tail call i64 @syscall(i32 %32, i64 %33)
%35 = icmp eq i64 %31, %34
%36 = load i32, i32* @EXIT_SUCCESS
%37 = load i32, i32* @EXIT_FAILURE
%38 = select i1 %35, i32 %37, i32 %36
%39 = tail call i32 @exit(i32 %38)
unreachable
}