#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 */
scalar_t__ EINVAL;
scalar_t__ EMFILE;
scalar_t__ EPERM;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct rlimit oldrlim;
  struct rlimit newrlim;
  int fd;
  {
    int i;
    for (i = 3; i < 64; i++)
      close(i);
  };
  if (getrlimit(RLIMIT_NOFILE, &oldrlim) < 0) {
    perror("getrlimit");
    exit(1);
  }
  newrlim.rlim_cur = oldrlim.rlim_max + 1;
  newrlim.rlim_max = oldrlim.rlim_max;
  if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1) {
    if (errno != EINVAL) {
      fprintf(stderr, "setrlimit exceeding hardlimit must set errno=EINVAL\n");
      exit(1);
    }
  } else {
    fprintf(stderr, "setrlimit exceeding hardlimit must return -1\n");
    exit(1);
  }
  newrlim.rlim_cur = oldrlim.rlim_max;
  newrlim.rlim_max = oldrlim.rlim_max + 1;
  if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1) {
    if (errno != EPERM) {
      fprintf(stderr, "setrlimit changing hardlimit must set errno=EPERM\n");
      exit(1);
    }
  } else {
    fprintf(stderr, "setrlimit changing hardlimit must return -1\n");
    exit(1);
  }
  newrlim.rlim_cur = oldrlim.rlim_cur / 2;
  newrlim.rlim_max = oldrlim.rlim_max;
  if (setrlimit(RLIMIT_NOFILE, &newrlim) < 0) {
    perror("setrlimit");
    exit(1);
  }
  if (getrlimit(RLIMIT_NOFILE, &newrlim) < 0) {
    perror("getrlimit");
    exit(1);
  }
  if (newrlim.rlim_cur != oldrlim.rlim_cur / 2) {
    fprintf(stderr, "rlim_cur is %llu (should be %llu)\n",
            (unsigned long long)newrlim.rlim_cur,
            (unsigned long long)oldrlim.rlim_cur / 2);
  }
  if (newrlim.rlim_max != oldrlim.rlim_max) {
    fprintf(stderr, "rlim_max is %llu (should be %llu)\n",
            (unsigned long long)newrlim.rlim_max,
            (unsigned long long)oldrlim.rlim_max);
  }
  newrlim.rlim_cur -= 3;
  while (newrlim.rlim_cur-- > 0) {
    if (open("/dev/null", O_RDONLY) < 0) {
      perror("open");
    }
  }
  if ((fd = open("/dev/null", O_RDONLY)) >= 0) {
    fprintf(stderr, "open succeeded with fd %d - it should have failed!\n", fd);
  } else if (errno != EMFILE) {
    perror("open");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x28(%rbp)
cmpl   $0x40,-0x28(%rbp)
jge    11ad <main+0x3d>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    118d <main+0x1d>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1090 <getrlimit@plt>
cmp    $0x0,%rax
jge    11df <main+0x6f>
lea    0xe30(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    1249 <main+0xd9>
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
je     1244 <main+0xd4>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    126a <main+0xfa>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    12d4 <main+0x164>
mov    0x2d2d(%rip),%rax
mov    (%rax),%rax
mov    0x2d03(%rip),%rcx
cmp    (%rcx),%rax
je     12cf <main+0x15f>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12f5 <main+0x185>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <setrlimit@plt>
cmp    $0x0,%eax
jge    133a <main+0x1ca>
lea    0xda2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1090 <getrlimit@plt>
cmp    $0x0,%rax
jge    136c <main+0x1fc>
lea    0xca3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
je     13ad <main+0x23d>
mov    0x2c3f(%rip),%rax
mov    (%rax),%edi
movslq -0x20(%rbp),%rdx
movslq -0x18(%rbp),%rcx
shr    $0x1,%rcx
lea    0xd31(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     13d8 <main+0x268>
mov    0x2c10(%rip),%rax
mov    (%rax),%edi
movslq -0x1c(%rbp),%rdx
movslq -0x14(%rbp),%rcx
lea    0xd29(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x0,%eax
jle    1424 <main+0x2b4>
mov    0x2bec(%rip),%rax
mov    (%rax),%esi
lea    0xd18(%rip),%rdi
call   1060 <open@plt>
cmp    $0x0,%eax
jge    141f <main+0x2af>
lea    0xd0d(%rip),%rdi
call   1070 <perror@plt>
jmp    13e1 <main+0x271>
mov    0x2bbd(%rip),%rax
mov    (%rax),%esi
lea    0xce9(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jl     1464 <main+0x2f4>
mov    0x2b84(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xcd4(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    148f <main+0x31f>
mov    0x2b5d(%rip),%rax
mov    (%rax),%rax
mov    0x2b3b(%rip),%rcx
cmp    (%rcx),%rax
je     148a <main+0x31a>
lea    0xca2(%rip),%rdi
call   1070 <perror@plt>
jmp    148f <main+0x31f>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x3,%ebx
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%ebx
cmp    $0x40,%ebx
jne    1180 <main+0x10>
mov    0x2e4a(%rip),%rbx
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
call   1090 <getrlimit@plt>
test   %rax,%rax
jns    11bd <main+0x4d>
lea    0xe52(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x14(%rsp),%eax
lea    0x1(%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1050 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    1257 <main+0xe7>
mov    0x2de4(%rip),%r15
mov    (%r15),%rax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
jne    1269 <main+0xf9>
mov    0x14(%rsp),%eax
mov    %eax,0x8(%rsp)
add    $0x1,%eax
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1050 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    127b <main+0x10b>
mov    (%r15),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    128d <main+0x11d>
mov    0x10(%rsp),%eax
mov    0x14(%rsp),%ecx
mov    %eax,%edx
shr    $0x1f,%edx
add    %eax,%edx
sar    %edx
mov    %edx,0x8(%rsp)
mov    %ecx,0xc(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1050 <setrlimit@plt>
test   %eax,%eax
jns    12ae <main+0x13e>
lea    0xe7b(%rip),%rdi
jmp    11ae <main+0x3e>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
jmp    129d <main+0x12d>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
jmp    129d <main+0x12d>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
jmp    129d <main+0x12d>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1090 <getrlimit@plt>
test   %rax,%rax
js     11a7 <main+0x37>
movslq 0x8(%rsp),%rdx
movslq 0x10(%rsp),%rcx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
cmp    %eax,%edx
je     12f4 <main+0x184>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
shr    %rcx
lea    0xdea(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
movslq 0xc(%rsp),%rdx
movslq 0x14(%rsp),%rcx
cmp    %ecx,%edx
je     1319 <main+0x1a9>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x8(%rsp),%eax
lea    -0x4(%rax),%ecx
mov    %ecx,0x8(%rsp)
cmp    $0x4,%eax
jl     1369 <main+0x1f9>
mov    0x2cb8(%rip),%r12
lea    0xde6(%rip),%rbx
lea    0xde9(%rip),%r14
jmp    134f <main+0x1df>
mov    0x8(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x8(%rsp)
test   %eax,%eax
jle    1369 <main+0x1f9>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    1340 <main+0x1d0>
mov    %r14,%rdi
call   1070 <perror@plt>
jmp    1340 <main+0x1d0>
mov    0x2c78(%rip),%rax
mov    (%rax),%esi
lea    0xda4(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    13a4 <main+0x234>
mov    (%r15),%rax
mov    0x2c24(%rip),%rcx
cmp    (%rcx),%rax
je     139d <main+0x22d>
lea    0xd8f(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2c25(%rip),%rcx
mov    (%rcx),%edi
lea    0xd78(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str.1 = [53 x i8] c"setrlimit exceeding hardlimit must set errno=EINVAL\0A\00"
@.str.2 = [46 x i8] c"setrlimit exceeding hardlimit must return -1\0A\00"
@EPERM = global i64 0
@.str.3 = [51 x i8] c"setrlimit changing hardlimit must set errno=EPERM\0A\00"
@.str.4 = [45 x i8] c"setrlimit changing hardlimit must return -1\0A\00"
@.str.5 = [10 x i8] c"setrlimit\00"
@.str.6 = [35 x i8] c"rlim_cur is %llu (should be %llu)\0A\00"
@.str.7 = [35 x i8] c"rlim_max is %llu (should be %llu)\0A\00"
@.str.8 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.9 = [5 x i8] c"open\00"
@.str.10 = [52 x i8] c"open succeeded with fd %d - it should have failed!\0A\00"
@EMFILE = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.rlimit
%7 = alloca %struct.rlimit
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 3, i32* %9
br label %10
10: ; preds = %16, %2
%11 = load i32, i32* %9
%12 = icmp slt i32 %11, 64
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* %9
%15 = call i32 @close(i32 %14)
br label %16
16: ; preds = %13
%17 = load i32, i32* %9
%18 = add nsw i32 %17, 1
store i32 %18, i32* %9
br label %10
19: ; preds = %10
%20 = load i32, i32* @RLIMIT_NOFILE
%21 = call i64 @getrlimit(i32 %20, %struct.rlimit* %6)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%28 = load i32, i32* %27
%29 = add nsw i32 %28, 1
%30 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @RLIMIT_NOFILE
%35 = call i32 @setrlimit(i32 %34, %struct.rlimit* %7)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %46
37: ; preds = %26
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINVAL
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %37
br label %50
46: ; preds = %26
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %45
%51 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%52 = load i32, i32* %51
%53 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%55 = load i32, i32* %54
%56 = add nsw i32 %55, 1
%57 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
store i32 %56, i32* %57
%58 = load i32, i32* @RLIMIT_NOFILE
%59 = call i32 @setrlimit(i32 %58, %struct.rlimit* %7)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %70
61: ; preds = %50
%62 = load i64, i64* @errno
%63 = load i64, i64* @EPERM
%64 = icmp ne i64 %62, %63
br i1 %64, label %65, label %69
65: ; preds = %61
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %61
br label %74
70: ; preds = %50
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %69
%75 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%76 = load i32, i32* %75
%77 = sdiv i32 %76, 2
%78 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i32 %77, i32* %78
%79 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%80 = load i32, i32* %79
%81 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
store i32 %80, i32* %81
%82 = load i32, i32* @RLIMIT_NOFILE
%83 = call i32 @setrlimit(i32 %82, %struct.rlimit* %7)
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %74
%86 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%87 = call i32 @exit(i32 1)
unreachable
88: ; preds = %74
%89 = load i32, i32* @RLIMIT_NOFILE
%90 = call i64 @getrlimit(i32 %89, %struct.rlimit* %7)
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %88
%93 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%94 = call i32 @exit(i32 1)
unreachable
95: ; preds = %88
%96 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%97 = load i32, i32* %96
%98 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%99 = load i32, i32* %98
%100 = sdiv i32 %99, 2
%101 = icmp ne i32 %97, %100
br i1 %101, label %102, label %112
102: ; preds = %95
%103 = load i32, i32* @stderr
%104 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%105 = load i32, i32* %104
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%108 = load i32, i32* %107
%109 = sext i32 %108 to i64
%110 = udiv i64 %109, 2
%111 = call i32 (i32, i8*, ...) @fprintf(i32 %103, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i64 %106, i64 %110)
br label %112
112: ; preds = %102, %95
%113 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
%114 = load i32, i32* %113
%115 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%116 = load i32, i32* %115
%117 = icmp ne i32 %114, %116
br i1 %117, label %118, label %127
118: ; preds = %112
%119 = load i32, i32* @stderr
%120 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
%121 = load i32, i32* %120
%122 = sext i32 %121 to i64
%123 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%124 = load i32, i32* %123
%125 = sext i32 %124 to i64
%126 = call i32 (i32, i8*, ...) @fprintf(i32 %119, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i64 %122, i64 %125)
br label %127
127: ; preds = %118, %112
%128 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%129 = load i32, i32* %128
%130 = sub nsw i32 %129, 3
store i32 %130, i32* %128
br label %131
131: ; preds = %142, %127
%132 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%133 = load i32, i32* %132
%134 = add nsw i32 %133, -1
store i32 %134, i32* %132
%135 = icmp sgt i32 %133, 0
br i1 %135, label %136, label %143
136: ; preds = %131
%137 = load i32, i32* @O_RDONLY
%138 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 %137)
%139 = icmp slt i32 %138, 0
br i1 %139, label %140, label %142
140: ; preds = %136
%141 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %142
142: ; preds = %140, %136
br label %131
143: ; preds = %131
%144 = load i32, i32* @O_RDONLY
%145 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 %144)
store i32 %145, i32* %8
%146 = icmp sge i32 %145, 0
br i1 %146, label %147, label %151
147: ; preds = %143
%148 = load i32, i32* @stderr
%149 = load i32, i32* %8
%150 = call i32 (i32, i8*, ...) @fprintf(i32 %148, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.10, i64 0, i64 0), i32 %149)
br label %158
151: ; preds = %143
%152 = load i64, i64* @errno
%153 = load i64, i64* @EMFILE
%154 = icmp ne i64 %152, %153
br i1 %154, label %155, label %157
155: ; preds = %151
%156 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %157
157: ; preds = %155, %151
br label %158
158: ; preds = %157, %147
%159 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str.1 = [53 x i8] c"setrlimit exceeding hardlimit must set errno=EINVAL\0A\00"
@.str.2 = [46 x i8] c"setrlimit exceeding hardlimit must return -1\0A\00"
@EPERM = global i64 0
@.str.3 = [51 x i8] c"setrlimit changing hardlimit must set errno=EPERM\0A\00"
@.str.4 = [45 x i8] c"setrlimit changing hardlimit must return -1\0A\00"
@.str.5 = [10 x i8] c"setrlimit\00"
@.str.6 = [35 x i8] c"rlim_cur is %llu (should be %llu)\0A\00"
@.str.7 = [35 x i8] c"rlim_max is %llu (should be %llu)\0A\00"
@.str.8 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@.str.9 = [5 x i8] c"open\00"
@.str.10 = [52 x i8] c"open succeeded with fd %d - it should have failed!\0A\00"
@EMFILE = global i64 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.rlimit
%4 = alloca %struct.rlimit
%5 = bitcast %struct.rlimit* %3 to i8*
%6 = bitcast %struct.rlimit* %4 to i8*
br label %7
7: ; preds = %2, %7
%8 = phi i32 [ 3, %2 ], [ %10, %7 ]
%9 = tail call i32 @close(i32 %8)
%10 = add nuw nsw i32 %8, 1
%11 = icmp eq i32 %10, 64
br i1 %11, label %12, label %7
12: ; preds = %7
%13 = load i32, i32* @RLIMIT_NOFILE
%14 = call i64 @getrlimit(i32 %13, %struct.rlimit* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
%23 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 1
store i32 %21, i32* %24
%25 = load i32, i32* @RLIMIT_NOFILE
%26 = call i32 @setrlimit(i32 %25, %struct.rlimit* %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %36
28: ; preds = %19
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINVAL
%31 = icmp eq i64 %29, %30
br i1 %31, label %40, label %32
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %19
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %28
%41 = load i32, i32* %20
store i32 %41, i32* %23
%42 = add nsw i32 %41, 1
store i32 %42, i32* %24
%43 = load i32, i32* @RLIMIT_NOFILE
%44 = call i32 @setrlimit(i32 %43, %struct.rlimit* %4)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load i64, i64* @errno
%48 = load i64, i64* @EPERM
%49 = icmp eq i64 %47, %48
br i1 %49, label %58, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %40
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %46
%59 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
%60 = load i32, i32* %59
%61 = sdiv i32 %60, 2
store i32 %61, i32* %23
%62 = load i32, i32* %20
store i32 %62, i32* %24
%63 = load i32, i32* @RLIMIT_NOFILE
%64 = call i32 @setrlimit(i32 %63, %struct.rlimit* %4)
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %58
%67 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %58
%70 = load i32, i32* @RLIMIT_NOFILE
%71 = call i64 @getrlimit(i32 %70, %struct.rlimit* %4)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %69
%77 = load i32, i32* %23
%78 = load i32, i32* %59
%79 = sdiv i32 %78, 2
%80 = icmp eq i32 %77, %79
br i1 %80, label %87, label %81
81: ; preds = %76
%82 = load i32, i32* @stderr
%83 = sext i32 %77 to i64
%84 = sext i32 %78 to i64
%85 = lshr i64 %84, 1
%86 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i64 %83, i64 %85)
br label %87
87: ; preds = %81, %76
%88 = load i32, i32* %24
%89 = load i32, i32* %20
%90 = icmp eq i32 %88, %89
br i1 %90, label %96, label %91
91: ; preds = %87
%92 = load i32, i32* @stderr
%93 = sext i32 %88 to i64
%94 = sext i32 %89 to i64
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i64 %93, i64 %94)
br label %96
96: ; preds = %91, %87
%97 = load i32, i32* %23
%98 = add nsw i32 %97, -4
store i32 %98, i32* %23
%99 = icmp sgt i32 %97, 3
br i1 %99, label %100, label %110
100: ; preds = %96, %106
%101 = load i32, i32* @O_RDONLY
%102 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 %101)
%103 = icmp slt i32 %102, 0
br i1 %103, label %104, label %106
104: ; preds = %100
%105 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %106
106: ; preds = %104, %100
%107 = load i32, i32* %23
%108 = add nsw i32 %107, -1
store i32 %108, i32* %23
%109 = icmp sgt i32 %107, 0
br i1 %109, label %100, label %110
110: ; preds = %106, %96
%111 = load i32, i32* @O_RDONLY
%112 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 %111)
%113 = icmp sgt i32 %112, -1
br i1 %113, label %114, label %117
114: ; preds = %110
%115 = load i32, i32* @stderr
%116 = call i32 (i32, i8*, ...) @fprintf(i32 %115, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.10, i64 0, i64 0), i32 %112)
br label %123
117: ; preds = %110
%118 = load i64, i64* @errno
%119 = load i64, i64* @EMFILE
%120 = icmp eq i64 %118, %119
br i1 %120, label %123, label %121
121: ; preds = %117
%122 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %123
123: ; preds = %117, %121, %114
%124 = call i32 @exit(i32 0)
unreachable
}