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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ RLIMIT_NPROC;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ warnx(char *, int /*<<< orphan*/, int /*<<< orphan*/);
void setrlimit_nproc(rlim_t n) {
  pid_t pid = getpid();
  struct rlimit limit = {.rlim_cur = n, .rlim_max = n};
  warnx("(pid=%d): Setting RLIMIT_NPROC=%ld", pid, n);
  if (setrlimit(RLIMIT_NPROC, &limit) < 0)
    err(EXIT_FAILURE, "(pid=%d): setrlimit(RLIMIT_NPROC)", pid);
}


x86 assembly (clang, -O0):
<setrlimit_nproc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xe92(%rip),%rdi
call   1040 <warnx@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1060 <setrlimit@plt>
cmp    $0x0,%rax
jge    11a7 <setrlimit_nproc+0x67>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1050 <err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setrlimit_nproc>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
mov    %ebp,(%rsp)
mov    %ebp,0x4(%rsp)
lea    0xea4(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1040 <warnx@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <setrlimit@plt>
test   %rax,%rax
jns    1192 <setrlimit_nproc+0x52>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
mov    %ebx,%edx
call   1050 <err@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@.str = [35 x i8] c"(pid=%d): Setting RLIMIT_NPROC=%ld\00"
@RLIMIT_NPROC = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"(pid=%d): setrlimit(RLIMIT_NPROC)\00"
define void @setrlimit_nproc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.rlimit
store i32 %0, i32* %2
%5 = call i32 (...) @getpid()
store i32 %5, i32* %3
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%7 = load i32, i32* %2
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
%9 = load i32, i32* %2
store i32 %9, i32* %8
%10 = load i32, i32* %3
%11 = load i32, i32* %2
%12 = call i32 @warnx(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
%13 = load i32, i32* @RLIMIT_NPROC
%14 = call i64 @setrlimit(i32 %13, %struct.rlimit* %4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = load i32, i32* @EXIT_FAILURE
%18 = load i32, i32* %3
%19 = call i32 @err(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %16, %1
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@.str = [35 x i8] c"(pid=%d): Setting RLIMIT_NPROC=%ld\00"
@RLIMIT_NPROC = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"(pid=%d): setrlimit(RLIMIT_NPROC)\00"
define void @setrlimit_nproc(i32 %0) {
%2 = alloca %struct.rlimit
%3 = tail call i32 (...) @getpid()
%4 = bitcast %struct.rlimit* %2 to i8*
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
store i32 %0, i32* %5
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
store i32 %0, i32* %6
%7 = tail call i32 @warnx(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %0)
%8 = load i32, i32* @RLIMIT_NPROC
%9 = call i64 @setrlimit(i32 %8, %struct.rlimit* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @err(i32 %12, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %14
14: ; preds = %11, %1
ret void
}