#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;
};
typedef int /*<<< orphan*/ __rlimit_resource_t;

/* Variables and functions */
int RLIM_INFINITY;
int __setrlimit(int /*<<< orphan*/, struct rlimit *);
int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits) {
  struct rlimit rlimits_small;
  rlimits_small.rlim_cur =
      (((unsigned long int)rlimits->rlim_cur) < (RLIM_INFINITY >> 1)
           ? ((unsigned long int)rlimits->rlim_cur)
           : (RLIM_INFINITY >> 1));
  rlimits_small.rlim_max =
      (((unsigned long int)rlimits->rlim_max) < (RLIM_INFINITY >> 1)
           ? ((unsigned long int)rlimits->rlim_max)
           : (RLIM_INFINITY >> 1));
  return (__setrlimit(resource, &rlimits_small));
}


x86 assembly (clang, -O0):
<setrlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2eb3(%rip),%rcx
mov    (%rcx),%ecx
sar    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    114e <setrlimit+0x3e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1160 <setrlimit+0x50>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
sar    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%ecx
sar    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    1199 <setrlimit+0x89>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11ab <setrlimit+0x9b>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
sar    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <__setrlimit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setrlimit>:
sub    $0x18,%rsp
mov    (%rsi),%rax
mov    0x2ec2(%rip),%rcx
mov    (%rcx),%ecx
sar    %ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
cmovae %rcx,%rax
mov    %rax,0x8(%rsp)
mov    0x8(%rsi),%rax
cmp    %rcx,%rax
cmovb  %rax,%rcx
mov    %rcx,0x10(%rsp)
lea    0x8(%rsp),%rsi
call   1030 <__setrlimit@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@RLIM_INFINITY = global i32 0
define i32 @setrlimit(i32 %0, %struct.rlimit* %1) {
%3 = alloca i32
%4 = alloca %struct.rlimit*
%5 = alloca %struct.rlimit
store i32 %0, i32* %3
store %struct.rlimit* %1, %struct.rlimit** %4
%6 = load %struct.rlimit*, %struct.rlimit** %4
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load i32, i32* @RLIM_INFINITY
%10 = ashr i32 %9, 1
%11 = sext i32 %10 to i64
%12 = icmp ult i64 %8, %11
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load %struct.rlimit*, %struct.rlimit** %4
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %14, i32 0, i32 0
%16 = load i64, i64* %15
br label %21
17: ; preds = %2
%18 = load i32, i32* @RLIM_INFINITY
%19 = ashr i32 %18, 1
%20 = sext i32 %19 to i64
br label %21
21: ; preds = %17, %13
%22 = phi i64 [ %16, %13 ], [ %20, %17 ]
%23 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
store i64 %22, i64* %23
%24 = load %struct.rlimit*, %struct.rlimit** %4
%25 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = load i32, i32* @RLIM_INFINITY
%28 = ashr i32 %27, 1
%29 = sext i32 %28 to i64
%30 = icmp ult i64 %26, %29
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = load %struct.rlimit*, %struct.rlimit** %4
%33 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %32, i32 0, i32 1
%34 = load i64, i64* %33
br label %39
35: ; preds = %21
%36 = load i32, i32* @RLIM_INFINITY
%37 = ashr i32 %36, 1
%38 = sext i32 %37 to i64
br label %39
39: ; preds = %35, %31
%40 = phi i64 [ %34, %31 ], [ %38, %35 ]
%41 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 1
store i64 %40, i64* %41
%42 = load i32, i32* %3
%43 = call i32 @__setrlimit(i32 %42, %struct.rlimit* %5)
ret i32 %43
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@RLIM_INFINITY = global i32 0
define i32 @setrlimit(i32 %0, %struct.rlimit* nocapture %1) {
%3 = alloca %struct.rlimit
%4 = bitcast %struct.rlimit* %3 to i8*
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%6 = load i64, i64* %5
%7 = load i32, i32* @RLIM_INFINITY
%8 = ashr i32 %7, 1
%9 = sext i32 %8 to i64
%10 = icmp ult i64 %6, %9
%11 = select i1 %10, i64 %6, i64 %9
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
store i64 %11, i64* %12
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
%14 = load i64, i64* %13
%15 = icmp ult i64 %14, %9
%16 = select i1 %15, i64 %14, i64 %9
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
store i64 %16, i64* %17
%18 = call i32 @__setrlimit(i32 %0, %struct.rlimit* %3)
ret i32 %18
}