#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int str_indexof(char *, char *);
int /*<<< orphan*/ str_starts_with(char *, char *);
int workerpool_get_nprocs() {
  FILE *f = fopen("/proc/cpuinfo", "r");
  size_t n = 0;
  char *buf = NULL;
  int nproc = 0;
  while (getline(&buf, &n, f) != -1) {
    if (!str_starts_with(buf, "processor"))
      continue;
    int colon = str_indexof(buf, ":");
    int v = atoi(&buf[colon + 1]);
    if (v > nproc)
      nproc = v;
  }
  free(buf);
  return nproc;
}


x86 assembly (clang, -O0):
<workerpool_get_nprocs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
lea    0xea0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     120f <workerpool_get_nprocs+0xaf>
mov    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1040 <str_starts_with@plt>
cmp    $0x0,%eax
jne    11ce <workerpool_get_nprocs+0x6e>
jmp    1196 <workerpool_get_nprocs+0x36>
mov    -0x18(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1050 <str_indexof@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rdi
call   1070 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    120a <workerpool_get_nprocs+0xaa>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1196 <workerpool_get_nprocs+0x36>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<workerpool_get_nprocs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8b(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
xor    %r14d,%r14d
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%rbp
lea    0xe64(%rip),%r12
lea    0xe67(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
mov    0x8(%rsp),%rdi
cmp    $0xffffffff,%eax
je     120e <workerpool_get_nprocs+0xae>
mov    %r12,%rsi
call   1040 <str_starts_with@plt>
test   %eax,%eax
je     11c0 <workerpool_get_nprocs+0x60>
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
call   1050 <str_indexof@plt>
mov    0x8(%rsp),%rcx
cltq
lea    (%rax,%rcx,1),%rdi
add    $0x1,%rdi
call   1070 <atoi@plt>
cmp    %r14d,%eax
cmovg  %eax,%r14d
jmp    11c0 <workerpool_get_nprocs+0x60>
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"processor\00"
@.str.3 = [2 x i8] c":\00"
define i32 @workerpool_get_nprocs() {
%1 = alloca i32*
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %1
store i64 0, i64* %2
store i8* null, i8** %3
store i32 0, i32* %4
br label %8
8: ; preds = %31, %16, %0
%9 = load i32*, i32** %1
%10 = call i32 @getline(i8** %3, i64* %2, i32* %9)
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %32
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = call i32 @str_starts_with(i8* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %12
br label %8
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i32 @str_indexof(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 %19, i32* %5
%20 = load i8*, i8** %3
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = load i32, i32* %4
%28 = icmp sgt i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = load i32, i32* %6
store i32 %30, i32* %4
br label %31
31: ; preds = %29, %17
br label %8
32: ; preds = %8
%33 = load i8*, i8** %3
%34 = call i32 @free(i8* %33)
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"processor\00"
@.str.3 = [2 x i8] c":\00"
define i32 @workerpool_get_nprocs() {
%1 = alloca i64
%2 = alloca i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = bitcast i8** %2 to i8*
store i8* null, i8** %2
br label %6
6: ; preds = %15, %0
%7 = phi i32 [ %24, %15 ], [ 0, %0 ]
br label %8
8: ; preds = %6, %12
%9 = call i32 @getline(i8** %2, i64* %1, i32* %3)
%10 = icmp eq i32 %9, -1
%11 = load i8*, i8** %2
br i1 %10, label %25, label %12
12: ; preds = %8
%13 = call i32 @str_starts_with(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32 %13, 0
br i1 %14, label %8, label %15
15: ; preds = %12
%16 = load i8*, i8** %2
%17 = call i32 @str_indexof(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = load i8*, i8** %2
%19 = add nsw i32 %17, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = call i32 @atoi(i8* %21)
%23 = icmp sgt i32 %22, %7
%24 = select i1 %23, i32 %22, i32 %7
br label %6
25: ; preds = %8
%26 = call i32 @free(i8* %11)
ret i32 %7
}