#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 atol(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strncasecmp(char *, char *, int);
int calculate_smp() {
  int result = 1;
  FILE *proc;
  if (proc = fopen("/proc/cpuinfo", "r")) {
    char string[1024];
    while (!feof(proc)) {
      fgets(string, 1024, proc);
      if (!strncasecmp(string, "processor", 9)) {
        char *ptr = strchr(string, ':');
        if (ptr) {
          ptr++;
          result = atol(ptr) + 1;
        }
      } else if (!strncasecmp(string, "cpus detected", 13)) {
        char *ptr = strchr(string, ':');
        if (ptr) {
          ptr++;
          result = atol(ptr);
        }
      }
    }
    fclose(proc);
  }
  return result;
}


x86 assembly (clang, -O0):
<calculate_smp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x1,-0x4(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12cb <calculate_smp+0x15b>
jmp    11a8 <calculate_smp+0x38>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c6 <calculate_smp+0x56>
jmp    12c2 <calculate_smp+0x152>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
lea    -0x410(%rbp),%rdi
lea    0xe25(%rip),%rsi
mov    $0x9,%edx
call   1070 <strncasecmp@plt>
cmp    $0x0,%eax
jne    124b <calculate_smp+0xdb>
lea    -0x410(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     1246 <calculate_smp+0xd6>
mov    -0x418(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
call   1080 <atol@plt>
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    12bd <calculate_smp+0x14d>
lea    -0x410(%rbp),%rdi
lea    0xdbf(%rip),%rsi
mov    $0xd,%edx
call   1070 <strncasecmp@plt>
cmp    $0x0,%eax
jne    12b8 <calculate_smp+0x148>
lea    -0x410(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     12b3 <calculate_smp+0x143>
mov    -0x420(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rdi
call   1080 <atol@plt>
mov    %eax,-0x4(%rbp)
jmp    12b8 <calculate_smp+0x148>
jmp    12bd <calculate_smp+0x14d>
jmp    11a8 <calculate_smp+0x38>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_smp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
lea    0xe7a(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11b6 <calculate_smp+0x46>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
mov    $0x1,%r14d
test   %eax,%eax
je     11cf <calculate_smp+0x5f>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11bc <calculate_smp+0x4c>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r14d
mov    %rsp,%rbp
lea    0xe2f(%rip),%r15
lea    0xe32(%rip),%r12
jmp    11fc <calculate_smp+0x8c>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11ac <calculate_smp+0x3c>
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x9,%edx
call   1070 <strncasecmp@plt>
mov    %rbp,%rdi
test   %eax,%eax
je     1260 <calculate_smp+0xf0>
mov    %r12,%rsi
mov    $0xd,%edx
call   1070 <strncasecmp@plt>
test   %eax,%eax
jne    11f0 <calculate_smp+0x80>
mov    %rbp,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11f0 <calculate_smp+0x80>
add    $0x1,%rax
mov    %rax,%rdi
call   1080 <atol@plt>
mov    %eax,%r14d
jmp    11f0 <calculate_smp+0x80>
nopw   0x0(%rax,%rax,1)
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11f0 <calculate_smp+0x80>
add    $0x1,%rax
mov    %rax,%rdi
call   1080 <atol@plt>
mov    %eax,%r14d
add    $0x1,%r14d
jmp    11f0 <calculate_smp+0x80>

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 = [14 x i8] c"cpus detected\00"
define i32 @calculate_smp() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i8*
%5 = alloca i8*
store i32 1, i32* %1
%6 = 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* %6, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %8, label %53
8: ; preds = %0
br label %9
9: ; preds = %49, %8
%10 = load i32*, i32** %2
%11 = call i32 @feof(i32* %10)
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
br i1 %13, label %14, label %50
14: ; preds = %9
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %2
%17 = call i32 @fgets(i8* %15, i32 1024, i32* %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%19 = call i32 @strncasecmp(i8* %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 9)
%20 = icmp ne i32 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %14
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = call i8* @strchr(i8* %22, i8 signext 58)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = call i32 @atol(i8* %29)
%31 = add nsw i32 %30, 1
store i32 %31, i32* %1
br label %32
32: ; preds = %26, %21
br label %49
33: ; preds = %14
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%35 = call i32 @strncasecmp(i8* %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 13)
%36 = icmp ne i32 %35, 0
br i1 %36, label %48, label %37
37: ; preds = %33
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%39 = call i8* @strchr(i8* %38, i8 signext 58)
store i8* %39, i8** %5
%40 = load i8*, i8** %5
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %5
%45 = load i8*, i8** %5
%46 = call i32 @atol(i8* %45)
store i32 %46, i32* %1
br label %47
47: ; preds = %42, %37
br label %48
48: ; preds = %47, %33
br label %49
49: ; preds = %48, %32
br label %9
50: ; preds = %9
%51 = load i32*, i32** %2
%52 = call i32 @fclose(i32* %51)
br label %53
53: ; preds = %50, %0
%54 = load i32, i32* %1
ret i32 %54
}

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 = [14 x i8] c"cpus detected\00"
define i32 @calculate_smp() {
%1 = alloca [1024 x i8]
%2 = 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))
%3 = icmp eq i32* %2, null
br i1 %3, label %36, label %4
4: ; preds = %0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%6 = call i32 @feof(i32* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %33
8: ; preds = %4, %29
%9 = phi i32 [ %30, %29 ], [ 1, %4 ]
%10 = call i32 @fgets(i8* %5, i32 1024, i32* %2)
%11 = call i32 @strncasecmp(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 9)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = call i8* @strchr(i8* %5, i8 signext 58)
%15 = icmp eq i8* %14, null
br i1 %15, label %29, label %16
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %14, i64 1
%18 = call i32 @atol(i8* %17)
%19 = add nsw i32 %18, 1
br label %29
20: ; preds = %8
%21 = call i32 @strncasecmp(i8* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 13)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = call i8* @strchr(i8* %5, i8 signext 58)
%25 = icmp eq i8* %24, null
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = getelementptr inbounds i8, i8* %24, i64 1
%28 = call i32 @atol(i8* %27)
br label %29
29: ; preds = %23, %26, %13, %16, %20
%30 = phi i32 [ %9, %20 ], [ %19, %16 ], [ %9, %13 ], [ %28, %26 ], [ %9, %23 ]
%31 = call i32 @feof(i32* %2)
%32 = icmp eq i32 %31, 0
br i1 %32, label %8, label %33
33: ; preds = %29, %4
%34 = phi i32 [ 1, %4 ], [ %30, %29 ]
%35 = call i32 @fclose(i32* %2)
br label %36
36: ; preds = %33, %0
%37 = phi i32 [ %34, %33 ], [ 1, %0 ]
ret i32 %37
}