#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 {
  void *rlim_max;
  void *rlim_cur;
};
typedef int /*<<< orphan*/ rlim_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_STACK;
void *RLIM_INFINITY;
int /*<<< orphan*/ assert(int);
int execv(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
void testChildStackLimit(rlim_t StackLimit, char *ToRun) {
  int Res;
  struct rlimit Limit;
  Limit.rlim_cur = RLIM_INFINITY;
  Limit.rlim_max = RLIM_INFINITY;
  Res = setrlimit(RLIMIT_STACK, &Limit);
  if (Res != 0) {
    fprintf(stderr, "in esan::initializeLibrary\n");
    fprintf(stderr,
            "==1234==The stack size limit is beyond the maximum supported.\n");
    fprintf(stderr, "Re-execing with a stack size below 1TB.\n");
    fprintf(stderr, "in esan::initializeLibrary\n");
    fprintf(stderr, "done\n");
    fprintf(stderr, "in esan::finalizeLibrary\n");
    return;
  }
  pid_t Child = fork();
  assert(Child >= 0);
  if (Child > 0) {
    pid_t WaitRes = waitpid(Child, NULL, 0);
    assert(WaitRes == Child);
  } else {
    char *Args[2];
    Args[0] = ToRun;
    Args[1] = NULL;
    Res = execv(ToRun, Args);
    assert(0);
  }
}


x86 assembly (clang, -O0):
<testChildStackLimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1050 <setrlimit@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     122d <testChildStackLimit+0xcd>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12a9 <testChildStackLimit+0x149>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x30(%rbp)
jle    1282 <testChildStackLimit+0x122>
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
jmp    12a9 <testChildStackLimit+0x149>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1070 <execv@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
call   1030 <assert@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testChildStackLimit>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
mov    %rax,0x18(%rsp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%rsi
call   1050 <setrlimit@plt>
test   %eax,%eax
je     11f1 <testChildStackLimit+0x91>
mov    0x2e44(%rip),%rbx
mov    (%rbx),%edi
lea    0xe5b(%rip),%r14
mov    %r14,%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe97(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
mov    %r14,%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xea8(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xea0(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1245 <testChildStackLimit+0xe5>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1030 <assert@plt>
test   %rbx,%rbx
jle    1226 <testChildStackLimit+0xc6>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
cmp    %rbx,%rax
sete   %dil
jmp    1240 <testChildStackLimit+0xe0>
mov    %r14,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <execv@plt>
xor    %edi,%edi
call   1030 <assert@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rlimit = type { i8*, i8* }
@RLIM_INFINITY = global i8* null
@RLIMIT_STACK = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"in esan::initializeLibrary\0A\00"
@.str.1 = [63 x i8] c"==1234==The stack size limit is beyond the maximum supported.\0A\00"
@.str.2 = [41 x i8] c"Re-execing with a stack size below 1TB.\0A\00"
@.str.3 = [6 x i8] c"done\0A\00"
@.str.4 = [26 x i8] c"in esan::finalizeLibrary\0A\00"
define void @testChildStackLimit(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.rlimit
%7 = alloca i64
%8 = alloca i64
%9 = alloca [2 x i8*]
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** @RLIM_INFINITY
%11 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
store i8* %10, i8** %11
%12 = load i8*, i8** @RLIM_INFINITY
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
store i8* %12, i8** %13
%14 = load i32, i32* @RLIMIT_STACK
%15 = call i32 @setrlimit(i32 %14, %struct.rlimit* %6)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %55
31: ; preds = %2
%32 = call i64 (...) @fork()
store i64 %32, i64* %7
%33 = load i64, i64* %7
%34 = icmp sge i64 %33, 0
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i64, i64* %7
%38 = icmp sgt i64 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %31
%40 = load i64, i64* %7
%41 = call i64 @waitpid(i64 %40, i32* null, i32 0)
store i64 %41, i64* %8
%42 = load i64, i64* %8
%43 = load i64, i64* %7
%44 = icmp eq i64 %42, %43
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
br label %55
47: ; preds = %31
%48 = load i8*, i8** %4
%49 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
store i8* %48, i8** %49
%50 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 1
store i8* null, i8** %50
%51 = load i8*, i8** %4
%52 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
%53 = call i32 @execv(i8* %51, i8** %52)
store i32 %53, i32* %5
%54 = call i32 @assert(i32 0)
br label %55
55: ; preds = %18, %47, %39
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i8*, i8* }
@RLIM_INFINITY = global i8* null
@RLIMIT_STACK = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"in esan::initializeLibrary\0A\00"
@.str.1 = [63 x i8] c"==1234==The stack size limit is beyond the maximum supported.\0A\00"
@.str.2 = [41 x i8] c"Re-execing with a stack size below 1TB.\0A\00"
@.str.3 = [6 x i8] c"done\0A\00"
@.str.4 = [26 x i8] c"in esan::finalizeLibrary\0A\00"
define void @testChildStackLimit(i32 %0, i8* %1) {
%3 = alloca %struct.rlimit
%4 = alloca [2 x i8*]
%5 = bitcast %struct.rlimit* %3 to i8*
%6 = load i8*, i8** @RLIM_INFINITY
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
store i8* %6, i8** %7
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
store i8* %6, i8** %8
%9 = load i32, i32* @RLIMIT_STACK
%10 = call i32 @setrlimit(i32 %9, %struct.rlimit* %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %43
25: ; preds = %2
%26 = call i64 (...) @fork()
%27 = lshr i64 %26, 63
%28 = trunc i64 %27 to i32
%29 = xor i32 %28, 1
%30 = call i32 @assert(i32 %29)
%31 = icmp sgt i64 %26, 0
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = call i64 @waitpid(i64 %26, i32* null, i32 0)
%34 = icmp eq i64 %33, %26
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
br label %43
37: ; preds = %25
%38 = bitcast [2 x i8*]* %4 to i8*
%39 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
store i8* %1, i8** %39
%40 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 1
store i8* null, i8** %40
%41 = call i32 @execv(i8* %1, i8** %39)
%42 = call i32 @assert(i32 0)
br label %43
43: ; preds = %32, %37, %12
ret void
}