#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;
  };
  nrlim = orlim;
  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    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%eax
jne    11f4 <BasicGetSetlimit+0x84>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
jmp    1202 <BasicGetSetlimit+0x92>
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%eax
jne    122d <BasicGetSetlimit+0xbd>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
jmp    123b <BasicGetSetlimit+0xcb>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    124d <BasicGetSetlimit+0xdd>
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     1288 <BasicGetSetlimit+0x118>
lea    0xd8d(%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     12c3 <BasicGetSetlimit+0x153>
lea    0xd52(%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     12f7 <BasicGetSetlimit+0x187>
lea    0xd1e(%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     132b <BasicGetSetlimit+0x1bb>
lea    0xcea(%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    0x18(%rsp),%rcx
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
cmp    %ecx,%eax
mov    0x2e24(%rip),%rdx
mov    (%rdx),%edx
mov    %ecx,%esi
cmove  %edx,%esi
mov    %esi,%edi
shr    $0x1f,%edi
add    %esi,%edi
sar    %edi
mov    %edi,0x8(%rsp)
shr    $0x20,%rcx
cmp    %ecx,%eax
cmove  %edx,%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
mov    %eax,0xc(%rsp)
cmp    %eax,%edi
jle    11f6 <BasicGetSetlimit+0x86>
mov    %eax,0x8(%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     1225 <BasicGetSetlimit+0xb5>
lea    0xdec(%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     1254 <BasicGetSetlimit+0xe4>
lea    0xdbd(%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     127d <BasicGetSetlimit+0x10d>
lea    0xd94(%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     12a6 <BasicGetSetlimit+0x136>
lea    0xd6b(%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 = bitcast %struct.rlimit* %4 to i8*
%16 = bitcast %struct.rlimit* %3 to i8*
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @RLIM_INFINITY
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i32, i32* @INT_MAX
%23 = sdiv i32 %22, 2
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i32 %23, i32* %24
br label %30
25: ; preds = %14
%26 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%27 = load i32, i32* %26
%28 = sdiv i32 %27, 2
%29 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i32 %28, i32* %29
br label %30
30: ; preds = %25, %21
%31 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load i32, i32* @RLIM_INFINITY
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @INT_MAX
%37 = sdiv i32 %36, 2
%38 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i32 %37, i32* %38
br label %44
39: ; preds = %30
%40 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 1
%41 = load i32, i32* %40
%42 = sdiv i32 %41, 2
%43 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i32 %42, i32* %43
br label %44
44: ; preds = %39, %35
%45 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%48 = load i32, i32* %47
%49 = icmp sgt i32 %46, %48
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%52 = load i32, i32* %51
%53 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i32 %52, i32* %53
br label %54
54: ; preds = %50, %44
%55 = call i32 @setrlimit_test(%struct.rlimit* %4)
%56 = call i32 @expect_zng(i32 0, i32 %55)
store i32 %56, i32* %1
%57 = load i32, i32* %1
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%61 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %62
62: ; preds = %59, %54
%63 = call i32 @getrlimit_test(%struct.rlimit* %5)
%64 = call i32 @expect_zng(i32 0, i32 %63)
store i32 %64, i32* %1
%65 = load i32, i32* %1
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%69 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %70
70: ; preds = %67, %62
%71 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%72 = load i32, i32* %71
%73 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
%74 = load i32, i32* %73
%75 = call i32 @expect_val(i32 %72, i32 %74)
store i32 %75, i32* %1
%76 = load i32, i32* %1
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %70
%79 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%80 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %81
81: ; preds = %78, %70
%82 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%83 = load i32, i32* %82
%84 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 1
%85 = load i32, i32* %84
%86 = call i32 @expect_val(i32 %83, i32 %85)
store i32 %86, i32* %1
%87 = load i32, i32* %1
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %81
%90 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%91 = call i32 @fflush(i32* null)
store i32 -1, i32* %2
br label %92
92: ; preds = %89, %81
%93 = call i32 (...) @testbreak()
%94 = load i32, i32* %2
ret i32 %94
}

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 i64
%2 = bitcast i64* %1 to %struct.rlimit*
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.rlimit*
%5 = alloca %struct.rlimit
%6 = bitcast i64* %1 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast %struct.rlimit* %5 to i8*
%9 = tail call i32 (...) @testbreak()
%10 = call i32 @getrlimit_test(%struct.rlimit* %2)
%11 = call i32 @expect_zng(i32 0, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%15 = call i32 @fflush(i32* null)
br label %16
16: ; preds = %13, %0
%17 = phi i32 [ -1, %13 ], [ 0, %0 ]
%18 = load i64, i64* %1
%19 = bitcast i64* %3 to i32*
%20 = trunc i64 %18 to i32
%21 = load i32, i32* @RLIM_INFINITY
%22 = icmp eq i32 %21, %20
%23 = load i32, i32* @INT_MAX
%24 = select i1 %22, i32 %23, i32 %20
%25 = sdiv i32 %24, 2
store i32 %25, i32* %19
%26 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 1
%27 = lshr i64 %18, 32
%28 = trunc i64 %27 to i32
%29 = icmp eq i32 %21, %28
%30 = select i1 %29, i32 %23, i32 %28
%31 = sdiv i32 %30, 2
store i32 %31, i32* %26
%32 = icmp sgt i32 %25, %31
br i1 %32, label %33, label %34
33: ; preds = %16
store i32 %31, i32* %19
br label %34
34: ; preds = %33, %16
%35 = call i32 @setrlimit_test(%struct.rlimit* %4)
%36 = call i32 @expect_zng(i32 0, i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%40 = call i32 @fflush(i32* null)
br label %41
41: ; preds = %38, %34
%42 = phi i32 [ -1, %38 ], [ %17, %34 ]
%43 = call i32 @getrlimit_test(%struct.rlimit* %5)
%44 = call i32 @expect_zng(i32 0, i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %41
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%48 = call i32 @fflush(i32* null)
br label %49
49: ; preds = %46, %41
%50 = phi i32 [ -1, %46 ], [ %42, %41 ]
%51 = load i32, i32* %19
%52 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i64 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @expect_val(i32 %51, i32 %53)
%55 = icmp eq i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %49
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%58 = call i32 @fflush(i32* null)
br label %59
59: ; preds = %56, %49
%60 = phi i32 [ -1, %56 ], [ %50, %49 ]
%61 = load i32, i32* %26
%62 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i64 0, i32 1
%63 = load i32, i32* %62
%64 = call i32 @expect_val(i32 %61, i32 %63)
%65 = icmp eq i32 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %59
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.4, i64 0, i64 0))
%68 = call i32 @fflush(i32* null)
br label %69
69: ; preds = %66, %59
%70 = phi i32 [ -1, %66 ], [ %60, %59 ]
%71 = call i32 (...) @testbreak()
ret i32 %70
}