#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int max_pids(void) {
  int max_pidsfd = open("/proc/sys/kernel/pid_max", O_RDONLY);
  char max_pids[32];
  int max_pids_rbytes = read(max_pidsfd, max_pids, 32);
  close(max_pidsfd);
  if (max_pids_rbytes < 1)
    return 0;
  int num_pids = atoi(max_pids);
  return num_pids;
}


x86 assembly (clang, -O0):
<max_pids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x1,-0x34(%rbp)
jge    1192 <max_pids+0x52>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <max_pids+0x64>
lea    -0x30(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<max_pids>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %ebp,%ebp
jle    1183 <max_pids+0x43>
mov    %rsp,%rdi
call   1060 <atoi@plt>
jmp    1185 <max_pids+0x45>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@O_RDONLY = global i32 0
define i32 @max_pids() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 32)
store i32 %10, i32* %4
%11 = load i32, i32* %2
%12 = call i32 @close(i32 %11)
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %0
store i32 0, i32* %1
br label %20
16: ; preds = %0
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
store i32 %19, i32* %1
br label %20
20: ; preds = %16, %15
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@O_RDONLY = global i32 0
define i32 @max_pids() {
%1 = alloca [32 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%5 = call i32 @read(i32 %3, i8* %4, i32 32)
%6 = tail call i32 @close(i32 %3)
%7 = icmp slt i32 %5, 1
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 @atoi(i8* %4)
br label %10
10: ; preds = %0, %8
%11 = phi i32 [ %9, %8 ], [ 0, %0 ]
ret i32 %11
}