#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*/ close(int);
int open(char *, int /*<<< orphan*/);
long read(int, char *, int);
long strtol(char *, char **, int);
unsigned get_pid_digits(void) {
  char pidbuf[24];
  char *endp;
  long rc;
  int fd;
  static unsigned ret;
  if (ret) {
    goto out;
  }
  ret = 5;
  fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
  if (fd == -1) {
    goto out;
  }
  rc = read(fd, pidbuf, sizeof pidbuf);
  close(fd);
  if (rc < 3) {
    goto out;
  }
  pidbuf[rc] = '\0';
  rc = strtol(pidbuf, &endp, 10);
  if (rc < 42) {
    goto out;
  }
  if (*endp && *endp != '\n') {
    goto out;
  }
  rc--;
  ret = 0;
  while (rc) {
    rc /= 10;
    ret++;
  }
out:
  return ret;
}


x86 assembly (clang, -O0):
<get_pid_digits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
cmpl   $0x0,0x2ef5(%rip)
je     115a <get_pid_digits+0x1a>
jmp    1258 <get_pid_digits+0x118>
movl   $0x5,0x2ee0(%rip)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    118b <get_pid_digits+0x4b>
jmp    1258 <get_pid_digits+0x118>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
cmpq   $0x3,-0x30(%rbp)
jge    11b8 <get_pid_digits+0x78>
jmp    1258 <get_pid_digits+0x118>
mov    -0x30(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x2a,-0x30(%rbp)
jge    11e7 <get_pid_digits+0xa7>
jmp    1258 <get_pid_digits+0x118>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     120c <get_pid_digits+0xcc>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     120c <get_pid_digits+0xcc>
jmp    1258 <get_pid_digits+0x118>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,0x2e22(%rip)
cmpq   $0x0,-0x30(%rbp)
je     1253 <get_pid_digits+0x113>
mov    -0x30(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
mov    0x2dff(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2df6(%rip)
jmp    1222 <get_pid_digits+0xe2>
jmp    1258 <get_pid_digits+0x118>
mov    0x2de6(%rip),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_digits>:
push   %r14
push   %rbx
sub    $0x28,%rsp
cmpl   $0x0,0x2ef6(%rip)
je     115e <get_pid_digits+0x1e>
mov    0x2eee(%rip),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
movl   $0x5,0x2edc(%rip)
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1150 <get_pid_digits+0x10>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x18,%edx
call   1040 <read@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0x3,%r14
jl     1150 <get_pid_digits+0x10>
movb   $0x0,0x10(%rsp,%r14,1)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
cmp    $0x2a,%rax
jl     1150 <get_pid_digits+0x10>
mov    %rax,%rcx
mov    0x8(%rsp),%rax
mov    (%rax),%al
cmp    $0xa,%al
je     11db <get_pid_digits+0x9b>
test   %al,%al
jne    1150 <get_pid_digits+0x10>
add    $0xffffffffffffffff,%rcx
xor    %esi,%esi
movabs $0x6666666666666667,%rdi
66 66 66
nopl   0x0(%rax,%rax,1)
mov    %rcx,%rax
imul   %rdi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
add    $0x1,%esi
add    $0x9,%rcx
cmp    $0x12,%rcx
mov    %rdx,%rcx
ja     11f0 <get_pid_digits+0xb0>
mov    %esi,0x2e2a(%rip)
jmp    1150 <get_pid_digits+0x10>

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

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