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

/* Variables and functions */
int INT_MAX;
int RLIM_INFINITY;
int expect_val(int, int);
int expect_zng(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ getrlimit_test(struct rlimit *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setrlimit_test(struct rlimit *);
int /*<<< orphan*/ testbreak();
int BasicGetSetlimit(void) {
  int passed;
  int block = 0;
  struct rlimit orlim;
  struct rlimit nrlim;
  struct rlimit crlim;
  testbreak();
  passed = expect_zng(0, getrlimit_test(&orlim));
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  if (nrlim.rlim_cur == RLIM_INFINITY) {
    nrlim.rlim_cur = INT_MAX / 2;
  } else {
    nrlim.rlim_cur = orlim.rlim_cur / 2;
  }
  if (nrlim.rlim_max == RLIM_INFINITY) {
    nrlim.rlim_max = INT_MAX / 2;
  } else {
    nrlim.rlim_max = orlim.rlim_max / 2;
  }
  if (nrlim.rlim_cur > nrlim.rlim_max) {
    nrlim.rlim_cur = nrlim.rlim_max;
  }
  passed = expect_zng(0, setrlimit_test(&nrlim));
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  passed = expect_zng(0, getrlimit_test(&crlim));
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  passed = expect_val(nrlim.rlim_cur, crlim.rlim_cur);
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  passed = expect_val(nrlim.rlim_max, crlim.rlim_max);
  if (passed != 0) {
    printf("\tFailed Phase 2: This call did something unexpected.\n");
    fflush(NULL);
    block = -1;
  };
  testbreak();
  return block;
}


x86 assembly (clang, -O0):
<BasicGetSetlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1060 <testbreak@plt>
lea    -0x10(%rbp),%rdi
call   1090 <getrlimit_test@plt>
mov    %eax,%esi
xor    %edi,%edi
call   1040 <expect_zng@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11c1 <BasicGetSetlimit+0x51>
lea    0xe54(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%eax
jne    11ec <BasicGetSetlimit+0x7c>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
jmp    11fa <BasicGetSetlimit+0x8a>
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%eax
jne    1225 <BasicGetSetlimit+0xb5>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
jmp    1233 <BasicGetSetlimit+0xc3>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1245 <BasicGetSetlimit+0xd5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <setrlimit_test@plt>
mov    %eax,%esi
xor    %edi,%edi
call   1040 <expect_zng@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1280 <BasicGetSetlimit+0x110>
lea    0xd95(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
call   1090 <getrlimit_test@plt>
mov    %eax,%esi
xor    %edi,%edi
call   1040 <expect_zng@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     12bb <BasicGetSetlimit+0x14b>
lea    0xd5a(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <expect_val@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     12ef <BasicGetSetlimit+0x17f>
lea    0xd26(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1070 <expect_val@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1323 <BasicGetSetlimit+0x1b3>
lea    0xcf2(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    $0x0,%al
call   1060 <testbreak@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BasicGetSetlimit>:
push   %rbx
sub    $0x20,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <testbreak@plt>
lea    0x18(%rsp),%rdi
call   1090 <getrlimit_test@plt>
xor    %edi,%edi
mov    %eax,%esi
call   1040 <expect_zng@plt>
test   %eax,%eax
je     11ad <BasicGetSetlimit+0x3d>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    $0xffffffff,%ebx
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %ecx,0x8(%rsp)
mov    %ecx,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <setrlimit_test@plt>
xor    %edi,%edi
mov    %eax,%esi
call   1040 <expect_zng@plt>
test   %eax,%eax
je     11f6 <BasicGetSetlimit+0x86>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    $0xffffffff,%ebx
lea    0x10(%rsp),%rdi
call   1090 <getrlimit_test@plt>
xor    %edi,%edi
mov    %eax,%esi
call   1040 <expect_zng@plt>
test   %eax,%eax
je     1225 <BasicGetSetlimit+0xb5>
lea    0xdec(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    $0xffffffff,%ebx
mov    0x8(%rsp),%edi
mov    0x10(%rsp),%esi
call   1070 <expect_val@plt>
test   %eax,%eax
je     124e <BasicGetSetlimit+0xde>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    $0xffffffff,%ebx
mov    0xc(%rsp),%edi
mov    0x14(%rsp),%esi
call   1070 <expect_val@plt>
test   %eax,%eax
je     1277 <BasicGetSetlimit+0x107>
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    $0xffffffff,%ebx
xor    %eax,%eax
call   1060 <testbreak@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@.str = [54 x i8] c"\09Failed Phase 2: This call did something unexpected.\0A\00"
@RLIM_INFINITY = global i32 0
@INT_MAX = global i32 0
define i32 @BasicGetSetlimit() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.rlimit
%4 = alloca %struct.rlimit
%5 = alloca %struct.rlimit
store i32 0, i32* %2
%6 = call i32 (...) @testbreak()
%7 = call i32 @getrlimit_test(%struct.rlimit* %3)
%8 = call i32 @expect_zng(i32 0, i32 %7)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %14
14: ; preds = %11, %0
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @RLIM_INFINITY
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @INT_MAX
%21 = sdiv i32 %20, 2
%22 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i32 %21, i32* %22
br label %28
23: ; preds = %14
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 2
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i32 %26, i32* %27
br label %28
28: ; preds = %23, %19
%29 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i32, i32* @RLIM_INFINITY
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32, i32* @INT_MAX
%35 = sdiv i32 %34, 2
%36 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i32 %35, i32* %36
br label %42
37: ; preds = %28
%38 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 1
%39 = load i32, i32* %38
%40 = sdiv i32 %39, 2
%41 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i32 %40, i32* %41
br label %42
42: ; preds = %37, %33
%43 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%46 = load i32, i32* %45
%47 = icmp sgt i32 %44, %46
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i32 %50, i32* %51
br label %52
52: ; preds = %48, %42
%53 = call i32 @setrlimit_test(%struct.rlimit* %4)
%54 = call i32 @expect_zng(i32 0, i32 %53)
store i32 %54, i32* %1
%55 = load i32, i32* %1
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%59 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %60
60: ; preds = %57, %52
%61 = call i32 @getrlimit_test(%struct.rlimit* %5)
%62 = call i32 @expect_zng(i32 0, i32 %61)
store i32 %62, i32* %1
%63 = load i32, i32* %1
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %60
%66 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%67 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %68
68: ; preds = %65, %60
%69 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%70 = load i32, i32* %69
%71 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i32 @expect_val(i32 %70, i32 %72)
store i32 %73, i32* %1
%74 = load i32, i32* %1
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %68
%77 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%78 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %79
79: ; preds = %76, %68
%80 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%81 = load i32, i32* %80
%82 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 1
%83 = load i32, i32* %82
%84 = call i32 @expect_val(i32 %81, i32 %83)
store i32 %84, i32* %1
%85 = load i32, i32* %1
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %79
%88 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%89 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %90
90: ; preds = %87, %79
%91 = call i32 (...) @testbreak()
%92 = load i32, i32* %2
ret i32 %92
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIM_INFINITY = global i32 0
@INT_MAX = global i32 0
@str.4 = [53 x i8] c"\09Failed Phase 2: This call did something unexpected.\00"
define i32 @BasicGetSetlimit() {
%1 = alloca %struct.rlimit
%2 = alloca %struct.rlimit
%3 = alloca %struct.rlimit
%4 = bitcast %struct.rlimit* %1 to i8*
%5 = bitcast %struct.rlimit* %2 to i8*
%6 = bitcast %struct.rlimit* %3 to i8*
%7 = tail call i32 (...) @testbreak()
%8 = call i32 @getrlimit_test(%struct.rlimit* %1)
%9 = call i32 @expect_zng(i32 0, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%13 = call i32 @fflush(i32* null)
br label %14
14: ; preds = %0, %11
%15 = phi i32 [ -1, %11 ], [ 0, %0 ]
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%17 = load i32, i32* @INT_MAX
%18 = sdiv i32 %17, 2
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @setrlimit_test(%struct.rlimit* %2)
%21 = call i32 @expect_zng(i32 0, i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %14
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%25 = call i32 @fflush(i32* null)
br label %26
26: ; preds = %23, %14
%27 = phi i32 [ -1, %23 ], [ %15, %14 ]
%28 = call i32 @getrlimit_test(%struct.rlimit* %3)
%29 = call i32 @expect_zng(i32 0, i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%33 = call i32 @fflush(i32* null)
br label %34
34: ; preds = %31, %26
%35 = phi i32 [ -1, %31 ], [ %27, %26 ]
%36 = load i32, i32* %16
%37 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @expect_val(i32 %36, i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %34
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%43 = call i32 @fflush(i32* null)
br label %44
44: ; preds = %41, %34
%45 = phi i32 [ -1, %41 ], [ %35, %34 ]
%46 = load i32, i32* %19
%47 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
%48 = load i32, i32* %47
%49 = call i32 @expect_val(i32 %46, i32 %48)
%50 = icmp eq i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %44
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%53 = call i32 @fflush(i32* null)
br label %54
54: ; preds = %51, %44
%55 = phi i32 [ -1, %51 ], [ %45, %44 ]
%56 = call i32 (...) @testbreak()
ret i32 %55
}