#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 *);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *strchr(char *, char);
char *strstr(char *, char *);
int get_nb_cpus() {
  char *s, *pos;
  FILE *f;
  int number = -1;
  f = fopen("/proc/cpuinfo", "r");
  if (f != NULL) {
    s = (char *)calloc(1, 81);
    if (s != NULL) {
      number = -2;
      while (fgets(s, 80, f) != NULL) {
        pos = strstr(s, "processor");
        if (pos == s) {
          pos = strchr(s, ':');
          number = atoi(pos + 1);
        }
      }
      ++number;
      free(s);
    }
    fclose(f);
  }
  return number;
}


x86 assembly (clang, -O0):
<get_nb_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0x1c(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1256 <get_nb_cpus+0xd6>
mov    $0x1,%edi
mov    $0x51,%esi
call   1070 <calloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     124d <get_nb_cpus+0xcd>
movl   $0xfffffffe,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x50,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     123b <get_nb_cpus+0xbb>
mov    -0x8(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1236 <get_nb_cpus+0xb6>
mov    -0x8(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    11d6 <get_nb_cpus+0x56>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_nb_cpus>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe72(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1240 <get_nb_cpus+0xc0>
mov    %rax,%r14
mov    $0x1,%edi
mov    $0x51,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     1247 <get_nb_cpus+0xc7>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
mov    $0xfffffffe,%ebp
test   %rax,%rax
je     1233 <get_nb_cpus+0xb3>
lea    0xe2c(%rip),%r15
jmp    1205 <get_nb_cpus+0x85>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1233 <get_nb_cpus+0xb3>
mov    $0x9,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strncmp@plt>
test   %eax,%eax
jne    11f0 <get_nb_cpus+0x70>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
lea    0x1(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
jmp    11f0 <get_nb_cpus+0x70>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    124c <get_nb_cpus+0xcc>
mov    $0xffffffff,%ebp
jmp    1254 <get_nb_cpus+0xd4>
mov    $0xffffffff,%ebp
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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"
define i32 @get_nb_cpus() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i32 -1, i32* %4
%5 = 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* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp ne i32* %6, null
br i1 %7, label %8, label %40
8: ; preds = %0
%9 = call i64 @calloc(i32 1, i32 81)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %1
%11 = load i8*, i8** %1
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %37
13: ; preds = %8
store i32 -2, i32* %4
br label %14
14: ; preds = %31, %13
%15 = load i8*, i8** %1
%16 = load i32*, i32** %3
%17 = call i32* @fgets(i8* %15, i32 80, i32* %16)
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %32
19: ; preds = %14
%20 = load i8*, i8** %1
%21 = call i8* @strstr(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i8* %21, i8** %2
%22 = load i8*, i8** %2
%23 = load i8*, i8** %1
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i8*, i8** %1
%27 = call i8* @strchr(i8* %26, i8 signext 58)
store i8* %27, i8** %2
%28 = load i8*, i8** %2
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %4
br label %31
31: ; preds = %25, %19
br label %14
32: ; preds = %14
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %4
%35 = load i8*, i8** %1
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %32, %8
%38 = load i32*, i32** %3
%39 = call i32 @fclose(i32* %38)
br label %40
40: ; preds = %37, %0
%41 = load i32, i32* %4
ret i32 %41
}

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"
define i32 @get_nb_cpus() {
%1 = 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))
%2 = icmp eq i32* %1, null
br i1 %2, label %29, label %3
3: ; preds = %0
%4 = tail call i64 @calloc(i32 1, i32 81)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %26, label %7
7: ; preds = %3
%8 = tail call i32* @fgets(i8* %5, i32 80, i32* %1)
%9 = icmp eq i32* %8, null
br i1 %9, label %22, label %10
10: ; preds = %7, %18
%11 = phi i32 [ %19, %18 ], [ -2, %7 ]
%12 = tail call i32 @strncmp(i8* %5, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 9)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i8* @strchr(i8* %5, i8 signext 58)
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = tail call i32 @atoi(i8* %16)
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ %17, %14 ], [ %11, %10 ]
%20 = tail call i32* @fgets(i8* %5, i32 80, i32* %1)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %10
22: ; preds = %18, %7
%23 = phi i32 [ -2, %7 ], [ %19, %18 ]
%24 = add nsw i32 %23, 1
%25 = tail call i32 @free(i8* %5)
br label %26
26: ; preds = %22, %3
%27 = phi i32 [ %24, %22 ], [ -1, %3 ]
%28 = tail call i32 @fclose(i32* %1)
br label %29
29: ; preds = %26, %0
%30 = phi i32 [ %27, %26 ], [ -1, %0 ]
ret i32 %30
}