#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 /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
char *read_from_file(int, int);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int max_processes() {
  int fd;
  char filename[64];
  char *read_buf;
  int max_procs = 0;
  sprintf(filename, "/proc/sys/kernel/pid_max");
  fd = open(filename, O_RDONLY);
  if (fd != -1) {
    read_buf = read_from_file(fd, 64);
    if (read_buf != NULL) {
      sscanf(read_buf, "%d", &max_procs);
      free(read_buf);
    }
  }
  return max_procs;
}


x86 assembly (clang, -O0):
<max_processes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x5c(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     11cc <max_processes+0x7c>
mov    -0x4(%rbp),%edi
mov    $0x40,%esi
call   1040 <read_from_file@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     11c7 <max_processes+0x77>
mov    -0x58(%rbp),%rdi
lea    0xe64(%rip),%rsi
lea    -0x5c(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    11cc <max_processes+0x7c>
mov    -0x5c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<max_processes>:
push   %r14
push   %rbx
sub    $0x58,%rsp
movl   $0x0,0xc(%rsp)
movups 0xeaa(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movups 0xea7(%rip),%xmm0
movups %xmm0,0x19(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1060 <open@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     11b5 <max_processes+0x75>
mov    %eax,%edi
mov    $0x40,%esi
call   1040 <read_from_file@plt>
test   %rax,%rax
je     11b5 <max_processes+0x75>
mov    %rax,%r14
lea    0xe7d(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <sscanf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @max_processes() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %4
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %1
%10 = load i32, i32* %1
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %23
12: ; preds = %0
%13 = load i32, i32* %1
%14 = call i8* @read_from_file(i32 %13, i32 64)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%20 = load i8*, i8** %3
%21 = call i32 @free(i8* %20)
br label %22
22: ; preds = %17, %12
br label %23
23: ; preds = %22, %0
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @max_processes() {
%1 = alloca [64 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %15, label %8
8: ; preds = %0
%9 = tail call i8* @read_from_file(i32 %6, i32 64)
%10 = icmp eq i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%13 = call i32 @free(i8* %9)
%14 = load i32, i32* %2
br label %15
15: ; preds = %8, %11, %0
%16 = phi i32 [ 0, %8 ], [ %14, %11 ], [ 0, %0 ]
ret i32 %16
}