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

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NPROC;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void setThreadLimit(int64_t limit) {
  struct rlimit rl;
  if (getrlimit(RLIMIT_NPROC, &rl) != 0) {
    fprintf(stderr, "getrlimit() failed: %s\n", strerror(errno));
  }
  rl.rlim_cur = limit;
  if (setrlimit(RLIMIT_NPROC, &rl) != 0) {
    fprintf(stderr, "setrlimit() failed: %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<setThreadLimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0x0,%rax
je     1193 <setThreadLimit+0x53>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%rdx
lea    0xe72(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1040 <setrlimit@plt>
cmp    $0x0,%rax
je     11e1 <setThreadLimit+0xa1>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe3c(%rip),%rsi
call   1030 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setThreadLimit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e97(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
test   %rax,%rax
je     118b <setThreadLimit+0x4b>
mov    0x2e6e(%rip),%rax
mov    (%rax),%ebp
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe7f(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    %ebx,0x8(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <setrlimit@plt>
test   %rax,%rax
je     11c9 <setThreadLimit+0x89>
mov    0x2e30(%rip),%rax
mov    (%rax),%ebx
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe59(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@RLIMIT_NPROC = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"getrlimit() failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"setrlimit() failed: %s\0A\00"
define void @setThreadLimit(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.rlimit
store i32 %0, i32* %2
%4 = load i32, i32* @RLIMIT_NPROC
%5 = call i64 @getrlimit(i32 %4, %struct.rlimit* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %7, %1
%13 = load i32, i32* %2
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @RLIMIT_NPROC
%16 = call i64 @setrlimit(i32 %15, %struct.rlimit* %3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %18, %12
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_NPROC = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"getrlimit() failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"setrlimit() failed: %s\0A\00"
define void @setThreadLimit(i32 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = load i32, i32* @RLIMIT_NPROC
%5 = call i64 @getrlimit(i32 %4, %struct.rlimit* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %7, %1
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
store i32 %0, i32* %13
%14 = load i32, i32* @RLIMIT_NPROC
%15 = call i64 @setrlimit(i32 %14, %struct.rlimit* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %17, %12
ret void
}