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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int get_max_proc(int *res) {
  const int buf_size = 100;
  char buf[buf_size];
  char pid_max_path[] = "/proc/sys/kernel/pid_max";
  char error_msg[buf_size];
  FILE *fp = fopen(pid_max_path, "r");
  if (!fp) {
    perror("Couldn't open /proc/sys/kernel/pid_max");
    return 1;
  }
  if (!fgets(buf, buf_size, fp)) {
    perror("Couldn't read from /proc/sys/kernel/pid_max");
    return 1;
  }
  fclose(fp);
  *res = atoi(buf);
  if (*res < 1) {
    sprintf(error_msg, "Error while parsing pid_max: %d", *res);
    perror(error_msg);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_max_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x64,-0x14(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    0xe7c(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe79(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe76(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe74(%rip),%al
mov    %al,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11e5 <get_max_proc+0x85>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    127f <get_max_proc+0x11f>
mov    -0x48(%rbp),%rdx
lea    -0xb0(%rbp),%rdi
mov    $0x64,%esi
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    1222 <get_max_proc+0xc2>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    127f <get_max_proc+0x11f>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0xb0(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1271 <get_max_proc+0x111>
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    -0x120(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x120(%rbp),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_max_proc>:
push   %r14
push   %rbx
sub    $0xf8,%rsp
mov    %rdi,%r14
movups 0xe95(%rip),%xmm0
movups %xmm0,0x9(%rsp)
movaps 0xe80(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe8e(%rip),%rsi
mov    %rsp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11d9 <get_max_proc+0x79>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     11e2 <get_max_proc+0x82>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0x20(%rsp),%rdi
call   1070 <atoi@plt>
mov    %eax,(%r14)
xor    %ebx,%ebx
test   %eax,%eax
jle    11f5 <get_max_proc+0x95>
mov    %ebx,%eax
add    $0xf8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3b(%rip),%rdi
jmp    11e9 <get_max_proc+0x89>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
jmp    11cc <get_max_proc+0x6c>
lea    0xe72(%rip),%rsi
lea    0x90(%rsp),%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1060 <perror@plt>
jmp    11cc <get_max_proc+0x6c>

LLVM IR (-O0):
@__const.get_max_proc.pid_max_path = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [39 x i8] c"Couldn't open /proc/sys/kernel/pid_max\00"
@.str.2 = [44 x i8] c"Couldn't read from /proc/sys/kernel/pid_max\00"
@.str.3 = [32 x i8] c"Error while parsing pid_max: %d\00"
define i32 @get_max_proc(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [25 x i8]
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %3
store i32 100, i32* %4
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %5
%10 = alloca i8, i64 100
%11 = bitcast [25 x i8]* %6 to i8*
%12 = alloca i8, i64 100
%13 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
store i32 1, i32* %8
br label %39
19: ; preds = %1
%20 = load i32*, i32** %7
%21 = call i32 @fgets(i8* %10, i32 100, i32* %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
store i32 1, i32* %8
br label %39
25: ; preds = %19
%26 = load i32*, i32** %7
%27 = call i32 @fclose(i32* %26)
%28 = call i32 @atoi(i8* %10)
%29 = load i32*, i32** %3
store i32 %28, i32* %29
%30 = load i32*, i32** %3
%31 = load i32, i32* %30
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %38
33: ; preds = %25
%34 = load i32*, i32** %3
%35 = load i32, i32* %34
%36 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = call i32 @perror(i8* %12)
br label %38
38: ; preds = %33, %25
store i32 0, i32* %2
store i32 1, i32* %8
br label %39
39: ; preds = %38, %23, %17
%40 = load i8*, i8** %5
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@__const.get_max_proc.pid_max_path = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [39 x i8] c"Couldn't open /proc/sys/kernel/pid_max\00"
@.str.2 = [44 x i8] c"Couldn't read from /proc/sys/kernel/pid_max\00"
@.str.3 = [32 x i8] c"Error while parsing pid_max: %d\00"
define i32 @get_max_proc(i32* nocapture writeonly %0) {
%2 = alloca [25 x i8]
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%6 = alloca [100 x i8]
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%8 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %24
12: ; preds = %1
%13 = call i32 @fgets(i8* %4, i32 100, i32* %8)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %24
17: ; preds = %12
%18 = call i32 @fclose(i32* %8)
%19 = call i32 @atoi(i8* %4)
store i32 %19, i32* %0
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%23 = call i32 @perror(i8* %7)
br label %24
24: ; preds = %17, %21, %15, %10
%25 = phi i32 [ 1, %15 ], [ 1, %10 ], [ 0, %21 ], [ 0, %17 ]
ret i32 %25
}