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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int get_pid_max() {
  static char buffer[1024];
  FILE *fd = fopen("/proc/sys/kernel/pid_max", "r");
  if (fd == NULL)
    return -1;
  if (fgets(buffer, sizeof(buffer), fd) == NULL) {
    fclose(fd);
    return -1;
  }
  fclose(fd);
  return atoi(buffer);
}


x86 assembly (clang, -O0):
<get_pid_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
lea    0xec3(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1176 <get_pid_max+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <get_pid_max+0x82>
mov    -0x10(%rbp),%rdx
lea    0x2ecf(%rip),%rdi
mov    $0x400,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11aa <get_pid_max+0x6a>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <get_pid_max+0x82>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0x2e96(%rip),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_max>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rdi
lea    0xec7(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1196 <get_pid_max+0x56>
mov    %rax,%rbx
lea    0x2eea(%rip),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     1196 <get_pid_max+0x56>
lea    0x2ec6(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <atoi@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@get_pid_max.buffer = internal global [1024 x i8] zeroinitializer
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_pid_max() {
%1 = alloca i32
%2 = alloca i32*
%3 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %2
%4 = load i32*, i32** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 -1, i32* %1
br label %18
7: ; preds = %0
%8 = load i32*, i32** %2
%9 = call i32* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_pid_max.buffer, i64 0, i64 0), i32 1024, i32* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32*, i32** %2
%13 = call i32 @fclose(i32* %12)
store i32 -1, i32* %1
br label %18
14: ; preds = %7
%15 = load i32*, i32** %2
%16 = call i32 @fclose(i32* %15)
%17 = call i32 @atoi(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_pid_max.buffer, i64 0, i64 0))
store i32 %17, i32* %1
br label %18
18: ; preds = %14, %11, %6
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
@get_pid_max.buffer = internal global [1024 x i8] zeroinitializer
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_pid_max() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = tail call i32* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_pid_max.buffer, i64 0, i64 0), i32 1024, i32* %1)
%5 = icmp eq i32* %4, null
%6 = tail call i32 @fclose(i32* %1)
br i1 %5, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @atoi(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_pid_max.buffer, i64 0, i64 0))
br label %9
9: ; preds = %3, %0, %7
%10 = phi i32 [ %8, %7 ], [ -1, %0 ], [ -1, %3 ]
ret i32 %10
}