#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 get_pid_max_digits(void) {
  ssize_t n;
  int digits, fd;
  const int default_digits = 6;
  const int min_digits = 5;
  char buf[32];
  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';
  digits = 0;
  while (buf[digits] >= '0' && buf[digits] <= '9')
    digits++;
  if (digits < min_digits)
    digits = min_digits;
ret:
  return digits;
}


x86 assembly (clang, -O0):
<get_pid_max_digits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x6,-0x14(%rbp)
movl   $0x5,-0x18(%rbp)
movl   $0x6,-0xc(%rbp)
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1174 <get_pid_max_digits+0x44>
jmp    120e <get_pid_max_digits+0xde>
mov    -0x10(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1040 <read@plt>
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
cmpq   $0x0,-0x8(%rbp)
jae    11a1 <get_pid_max_digits+0x71>
jmp    120e <get_pid_max_digits+0xde>
mov    -0x8(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movsbl -0x40(%rbp,%rax,1),%ecx
xor    %eax,%eax
cmp    $0x30,%ecx
mov    %al,-0x41(%rbp)
jl     11da <get_pid_max_digits+0xaa>
movslq -0xc(%rbp),%rax
movsbl -0x40(%rbp,%rax,1),%eax
cmp    $0x39,%eax
setle  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11ea <get_pid_max_digits+0xba>
jmp    11f8 <get_pid_max_digits+0xc8>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b1 <get_pid_max_digits+0x81>
cmpl   $0x5,-0xc(%rbp)
jge    1209 <get_pid_max_digits+0xd9>
movl   $0x5,-0xc(%rbp)
jmp    120e <get_pid_max_digits+0xde>
mov    -0xc(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_max_digits>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     119d <get_pid_max_digits+0x6d>
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   0x0(%rax)
movzbl (%rbx),%eax
add    $0xd0,%al
add    $0x1,%ecx
add    $0x1,%rbx
cmp    $0xa,%al
jb     1180 <get_pid_max_digits+0x50>
cmp    $0x6,%ecx
mov    $0x5,%eax
cmovae %ecx,%eax
jmp    11a2 <get_pid_max_digits+0x72>
mov    $0x6,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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