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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/ *, int);
int linux_proc_get_pid_max(void) {
  static const char filename[] = "/proc/sys/kernel/pid_max";
  FILE *file;
  char buf[100];
  int retval = -1;
  file = fopen(filename, "r");
  if (file == NULL) {
    fprintf(stderr, "unable to open %s\n", filename);
    return -1;
  }
  if (fgets(buf, sizeof(buf), file) != NULL)
    retval = strtol(buf, NULL, 10);
  fclose(file);
  return retval;
}


x86 assembly (clang, -O0):
<linux_proc_get_pid_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0xffffffff,-0x84(%rbp)
ff ff ff
lea    0xe94(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11af <linux_proc_get_pid_max+0x5f>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
lea    0xe62(%rip),%rdx
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <linux_proc_get_pid_max+0xa5>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11e3 <linux_proc_get_pid_max+0x93>
lea    -0x80(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x84(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linux_proc_get_pid_max>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
lea    0xea3(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1199 <linux_proc_get_pid_max+0x49>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11bc <linux_proc_get_pid_max+0x6c>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,%ebp
jmp    11c1 <linux_proc_get_pid_max+0x71>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
lea    0xe50(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    11c9 <linux_proc_get_pid_max+0x79>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@linux_proc_get_pid_max.filename = internal constant [25 x i8] c"/proc/sys/kernel/pid_max\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"unable to open %s\0A\00"
define i32 @linux_proc_get_pid_max() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
%4 = alloca i32
store i32 -1, i32* %4
%5 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @linux_proc_get_pid_max.filename, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @linux_proc_get_pid_max.filename, i64 0, i64 0))
store i32 -1, i32* %1
br label %23
11: ; preds = %0
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32* @fgets(i8* %12, i32 100, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%18 = call i32 @strtol(i8* %17, i32* null, i32 10)
store i32 %18, i32* %4
br label %19
19: ; preds = %16, %11
%20 = load i32*, i32** %2
%21 = call i32 @fclose(i32* %20)
%22 = load i32, i32* %4
store i32 %22, i32* %1
br label %23
23: ; preds = %19, %8
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@linux_proc_get_pid_max.filename = internal constant [25 x i8] c"/proc/sys/kernel/pid_max\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"unable to open %s\0A\00"
define i32 @linux_proc_get_pid_max() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @linux_proc_get_pid_max.filename, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @linux_proc_get_pid_max.filename, i64 0, i64 0))
br label %16
8: ; preds = %0
%9 = call i32* @fgets(i8* %2, i32 100, i32* %3)
%10 = icmp eq i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 @strtol(i8* nocapture %2, i32* null, i32 10)
br label %13
13: ; preds = %11, %8
%14 = phi i32 [ %12, %11 ], [ -1, %8 ]
%15 = call i32 @fclose(i32* %3)
br label %16
16: ; preds = %13, %5
%17 = phi i32 [ -1, %5 ], [ %14, %13 ]
ret i32 %17
}