#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
long atol(char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
long getPidMax() {
  char buf[(512)];
  ssize_t numRead;
  int fd;
  long pid_max;
  snprintf(buf, (512), "%s/sys/kernel/pid_max", ("/proc"));
  fd = open(buf, O_RDONLY);
  if (fd == -1) {
    return -1;
  }
  numRead = read(fd, buf, (512));
  if (numRead == -1) {
    return -1;
  }
  pid_max = atol(buf);
  if (pid_max == 0) {
    return -1;
  }
  return pid_max;
}


x86 assembly (clang, -O0):
<getPidMax>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
lea    -0x210(%rbp),%rdi
mov    $0x200,%esi
lea    0xea2(%rip),%rdx
lea    0xeb1(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x210(%rbp),%rdi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x218(%rbp)
cmpl   $0xffffffff,-0x218(%rbp)
jne    119f <getPidMax+0x5f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    120f <getPidMax+0xcf>
mov    -0x218(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0xffffffff,-0x214(%rbp)
jne    11d6 <getPidMax+0x96>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    120f <getPidMax+0xcf>
lea    -0x210(%rbp),%rdi
call   1050 <atol@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    1204 <getPidMax+0xc4>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    120f <getPidMax+0xcf>
mov    -0x220(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPidMax>:
push   %rbx
sub    $0x200,%rsp
lea    0xeb1(%rip),%rdx
lea    0xec0(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1030 <snprintf@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    $0xffffffffffffffff,%rbx
cmp    $0xffffffff,%eax
je     11a9 <getPidMax+0x69>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11a9 <getPidMax+0x69>
mov    %rsp,%rdi
call   1050 <atol@plt>
cmp    $0x1,%rax
sbb    %rbx,%rbx
or     %rax,%rbx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"%s/sys/kernel/pid_max\00"
@.str.1 = [6 x i8] c"/proc\00"
@O_RDONLY = global i32 0
define i64 @getPidMax() {
%1 = alloca i64
%2 = alloca [512 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %0
store i64 -1, i64* %1
br label %29
14: ; preds = %0
%15 = load i32, i32* %4
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 512)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %14
store i64 -1, i64* %1
br label %29
21: ; preds = %14
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%23 = call i64 @atol(i8* %22)
store i64 %23, i64* %5
%24 = load i64, i64* %5
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i64 -1, i64* %1
br label %29
27: ; preds = %21
%28 = load i64, i64* %5
store i64 %28, i64* %1
br label %29
29: ; preds = %27, %26, %20, %13
%30 = load i64, i64* %1
ret i64 %30
}

LLVM IR (-O2):
@.str = [22 x i8] c"%s/sys/kernel/pid_max\00"
@.str.1 = [6 x i8] c"/proc\00"
@O_RDONLY = global i32 0
define i64 @getPidMax() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = call i32 @snprintf(i8* %2, i32 512, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* %2, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %14, label %7
7: ; preds = %0
%8 = call i32 @read(i32 %5, i8* %2, i32 512)
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = call i64 @atol(i8* %2)
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i64 -1, i64 %11
br label %14
14: ; preds = %10, %7, %0
%15 = phi i64 [ -1, %0 ], [ -1, %7 ], [ %13, %10 ]
ret i64 %15
}