#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 {
  unsigned long rlim_cur;
  unsigned long rlim_max;
};

/* Variables and functions */
scalar_t__ ENOSYS;
unsigned long RLIM_INFINITY;
scalar_t__ errno;
scalar_t__ syscall(int, int, ...);
int getrlimit(int resource, struct rlimit *rlim) {
  unsigned long k_rlim[2];
  int ret = syscall(369, 0, resource, 0, rlim);
  if (!ret || errno != ENOSYS)
    return ret;
  if (syscall(191, resource, k_rlim) < 0)
    return -1;
  rlim->rlim_cur = k_rlim[0] == -1UL ? RLIM_INFINITY : k_rlim[0];
  rlim->rlim_max = k_rlim[1] == -1UL ? RLIM_INFINITY : k_rlim[1];
  return 0;
}


x86 assembly (clang, -O0):
<getrlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%r8
mov    $0x171,%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    $0x0,%al
call   1030 <syscall@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     115d <getrlimit+0x4d>
mov    0x2e8e(%rip),%rax
mov    (%rax),%rax
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
je     1168 <getrlimit+0x58>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <getrlimit+0xeb>
mov    -0x8(%rbp),%esi
lea    -0x20(%rbp),%rdx
mov    $0xbf,%edi
mov    $0x0,%al
call   1030 <syscall@plt>
cmp    $0x0,%rax
jge    1191 <getrlimit+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <getrlimit+0xeb>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11af <getrlimit+0x9f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b7 <getrlimit+0xa7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11e0 <getrlimit+0xd0>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11e8 <getrlimit+0xd8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrlimit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %ebx,%ebx
mov    $0x171,%edi
xor    %esi,%esi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r14,%r8
xor    %eax,%eax
call   1030 <syscall@plt>
test   %eax,%eax
je     1198 <getrlimit+0x88>
mov    0x2e99(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e9f(%rip),%rdx
mov    %eax,%ebx
cmp    (%rdx),%rcx
jne    1198 <getrlimit+0x88>
mov    %rsp,%rdx
mov    $0xbf,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <syscall@plt>
test   %rax,%rax
js     1193 <getrlimit+0x83>
mov    (%rsp),%rcx
mov    0x2e5f(%rip),%rax
cmp    $0xffffffffffffffff,%rcx
jne    117a <getrlimit+0x6a>
mov    (%rax),%rcx
mov    %rcx,(%r14)
mov    0x8(%rsp),%rcx
cmp    $0xffffffffffffffff,%rcx
jne    118b <getrlimit+0x7b>
mov    (%rax),%rcx
mov    %rcx,0x8(%r14)
xor    %ebx,%ebx
jmp    1198 <getrlimit+0x88>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@errno = global i64 0
@ENOSYS = global i64 0
@RLIM_INFINITY = global i64 0
define i32 @getrlimit(i32 %0, %struct.rlimit* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.rlimit*
%6 = alloca [2 x i64]
%7 = alloca i32
store i32 %0, i32* %4
store %struct.rlimit* %1, %struct.rlimit** %5
%8 = load i32, i32* %4
%9 = load %struct.rlimit*, %struct.rlimit** %5
%10 = call i64 (i32, i32, ...) @syscall(i32 369, i32 0, i32 %8, i32 0, %struct.rlimit* %9)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOSYS
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14, %2
%19 = load i32, i32* %7
store i32 %19, i32* %3
br label %51
20: ; preds = %14
%21 = load i32, i32* %4
%22 = getelementptr inbounds [2 x i64], [2 x i64]* %6, i64 0, i64 0
%23 = call i64 (i32, i32, ...) @syscall(i32 191, i32 %21, i64* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 -1, i32* %3
br label %51
26: ; preds = %20
%27 = getelementptr inbounds [2 x i64], [2 x i64]* %6, i64 0, i64 0
%28 = load i64, i64* %27
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i64, i64* @RLIM_INFINITY
br label %35
32: ; preds = %26
%33 = getelementptr inbounds [2 x i64], [2 x i64]* %6, i64 0, i64 0
%34 = load i64, i64* %33
br label %35
35: ; preds = %32, %30
%36 = phi i64 [ %31, %30 ], [ %34, %32 ]
%37 = load %struct.rlimit*, %struct.rlimit** %5
%38 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %37, i32 0, i32 0
store i64 %36, i64* %38
%39 = getelementptr inbounds [2 x i64], [2 x i64]* %6, i64 0, i64 1
%40 = load i64, i64* %39
%41 = icmp eq i64 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = load i64, i64* @RLIM_INFINITY
br label %47
44: ; preds = %35
%45 = getelementptr inbounds [2 x i64], [2 x i64]* %6, i64 0, i64 1
%46 = load i64, i64* %45
br label %47
47: ; preds = %44, %42
%48 = phi i64 [ %43, %42 ], [ %46, %44 ]
%49 = load %struct.rlimit*, %struct.rlimit** %5
%50 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %49, i32 0, i32 1
store i64 %48, i64* %50
store i32 0, i32* %3
br label %51
51: ; preds = %47, %25, %18
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@errno = global i64 0
@ENOSYS = global i64 0
@RLIM_INFINITY = global i64 0
define i32 @getrlimit(i32 %0, %struct.rlimit* %1) {
%3 = alloca [2 x i64]
%4 = bitcast [2 x i64]* %3 to i8*
%5 = tail call i64 (i32, i32, ...) @syscall(i32 369, i32 0, i32 %0, i32 0, %struct.rlimit* %1)
%6 = trunc i64 %5 to i32
%7 = icmp eq i32 %6, 0
br i1 %7, label %28, label %8
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOSYS
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 0
%14 = call i64 (i32, i32, ...) @syscall(i32 191, i32 %0, i64* %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %28, label %16
16: ; preds = %12
%17 = load i64, i64* %13
%18 = icmp eq i64 %17, -1
%19 = load i64, i64* @RLIM_INFINITY
%20 = select i1 %18, i64 %19, i64 %17
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
store i64 %20, i64* %21
%22 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 1
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, -1
%25 = load i64, i64* @RLIM_INFINITY
%26 = select i1 %24, i64 %25, i64 %23
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
store i64 %26, i64* %27
br label %28
28: ; preds = %12, %2, %8, %16
%29 = phi i32 [ 0, %16 ], [ %6, %8 ], [ 0, %2 ], [ -1, %12 ]
ret i32 %29
}