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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int pid_max_digits(void) {
  static int max_digits;
  ssize_t n;
  int fd;
  const int default_digits = 6;
  const int min_digits = 5;
  char buf[32];
  if (max_digits)
    goto ret;
  max_digits = default_digits;
  fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
  if (fd < 0)
    goto ret;
  n = read(fd, buf, sizeof(buf) - 1);
  (void)close(fd);
  if (n < 0)
    goto ret;
  buf[n] = '\0';
  max_digits = 0;
  while (buf[max_digits] >= '0' && buf[max_digits] <= '9')
    max_digits++;
  if (max_digits < min_digits)
    max_digits = min_digits;
ret:
  return max_digits;
}


x86 assembly (clang, -O0):
<pid_max_digits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x6,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
cmpl   $0x0,0x2eef(%rip)
je     1158 <pid_max_digits+0x28>
jmp    1238 <pid_max_digits+0x108>
movl   $0x6,0x2eda(%rip)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1189 <pid_max_digits+0x59>
jmp    1238 <pid_max_digits+0x108>
mov    -0xc(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1040 <read@plt>
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
cmpq   $0x0,-0x8(%rbp)
jae    11b6 <pid_max_digits+0x86>
jmp    1238 <pid_max_digits+0x108>
mov    -0x8(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
movl   $0x0,0x2e73(%rip)
movslq 0x2e6c(%rip),%rax
movsbl -0x40(%rbp,%rax,1),%ecx
xor    %eax,%eax
cmp    $0x30,%ecx
mov    %al,-0x41(%rbp)
jl     11f8 <pid_max_digits+0xc8>
movslq 0x2e52(%rip),%rax
movsbl -0x40(%rbp,%rax,1),%eax
cmp    $0x39,%eax
setle  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1208 <pid_max_digits+0xd8>
jmp    121c <pid_max_digits+0xec>
mov    0x2e2e(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e25(%rip)
jmp    11c9 <pid_max_digits+0x99>
cmpl   $0x5,0x2e19(%rip)
jge    1233 <pid_max_digits+0x103>
movl   $0x5,0x2e09(%rip)
jmp    1238 <pid_max_digits+0x108>
mov    0x2dfe(%rip),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_max_digits>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2efe(%rip),%eax
test   %eax,%eax
jne    11b9 <pid_max_digits+0x89>
movl   $0x6,0x2ef0(%rip)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11b3 <pid_max_digits+0x83>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1f,%edx
call   1040 <read@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1030 <close@plt>
movb   $0x0,(%rsp,%r14,1)
mov    $0xffffffff,%ecx
nopl   (%rax)
movzbl (%rbx),%eax
add    $0xd0,%al
add    $0x1,%ecx
add    $0x1,%rbx
cmp    $0xa,%al
jb     1190 <pid_max_digits+0x60>
cmp    $0x6,%ecx
mov    $0x5,%eax
cmovae %ecx,%eax
mov    %eax,0x2e8b(%rip)
jmp    11b9 <pid_max_digits+0x89>
mov    0x2e83(%rip),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@pid_max_digits.max_digits = internal global i32 0
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@O_RDONLY = global i32 0
define i32 @pid_max_digits() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [32 x i8]
store i32 6, i32* %3
store i32 5, i32* %4
%6 = load i32, i32* @pid_max_digits.max_digits
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
br label %51
9: ; preds = %0
store i32 6, i32* @pid_max_digits.max_digits
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
br label %51
15: ; preds = %9
%16 = load i32, i32* %2
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%18 = call i64 @read(i32 %16, i8* %17, i32 31)
store i64 %18, i64* %1
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
%21 = load i64, i64* %1
%22 = icmp ult i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %15
br label %51
24: ; preds = %15
%25 = load i64, i64* %1
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 %25
store i8 0, i8* %26
store i32 0, i32* @pid_max_digits.max_digits
br label %27
27: ; preds = %43, %24
%28 = load i32, i32* @pid_max_digits.max_digits
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp sge i32 %32, 48
br i1 %33, label %34, label %41
34: ; preds = %27
%35 = load i32, i32* @pid_max_digits.max_digits
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp sle i32 %39, 57
br label %41
41: ; preds = %34, %27
%42 = phi i1 [ false, %27 ], [ %40, %34 ]
br i1 %42, label %43, label %46
43: ; preds = %41
%44 = load i32, i32* @pid_max_digits.max_digits
%45 = add nsw i32 %44, 1
store i32 %45, i32* @pid_max_digits.max_digits
br label %27
46: ; preds = %41
%47 = load i32, i32* @pid_max_digits.max_digits
%48 = icmp slt i32 %47, 5
br i1 %48, label %49, label %50
49: ; preds = %46
store i32 5, i32* @pid_max_digits.max_digits
br label %50
50: ; preds = %49, %46
br label %51
51: ; preds = %50, %23, %14, %8
%52 = load i32, i32* @pid_max_digits.max_digits
ret i32 %52
}

LLVM IR (-O2):
@pid_max_digits.max_digits = internal unnamed_addr global i32 0
@.str = [25 x i8] c"/proc/sys/kernel/pid_max\00"
@O_RDONLY = global i32 0
define i32 @pid_max_digits() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @pid_max_digits.max_digits
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %26
5: ; preds = %0
store i32 6, i32* @pid_max_digits.max_digits
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = load i32, i32* @pid_max_digits.max_digits
br label %26
11: ; preds = %5
%12 = call i64 @read(i32 %7, i8* %2, i32 31)
%13 = tail call i32 @close(i32 %7)
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 %12
store i8 0, i8* %14
br label %15
15: ; preds = %15, %11
%16 = phi i64 [ %21, %15 ], [ 0, %11 ]
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = add i8 %18, -48
%20 = icmp ult i8 %19, 10
%21 = add nuw i64 %16, 1
br i1 %20, label %15, label %22
22: ; preds = %15
%23 = trunc i64 %16 to i32
%24 = icmp ugt i32 %23, 5
%25 = select i1 %24, i32 %23, i32 5
store i32 %25, i32* @pid_max_digits.max_digits
br label %26
26: ; preds = %9, %22, %0
%27 = phi i32 [ %10, %9 ], [ %25, %22 ], [ %3, %0 ]
ret i32 %27
}