#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memcmp(char *, char *, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int __sc_nr_cpus() {
  int fd;
  fd = open("/proc/cpuinfo", O_RDONLY);
  if (fd == -1)
    return 1;
  else {
    int n, nr = 0;
    char buf[2048];
    while ((n = read(fd, buf, sizeof(buf))) > 0) {
      register int i = 0;
      while (i < n) {
        if ((buf[i] == 'p') && (!memcmp(buf + i, "processor", 9))) {
          ++nr;
          i += 9;
        }
        while (i < n && buf[i++] != '\n')
          ;
      }
    }
    close(fd);
    return nr;
  }
}


x86 assembly (clang, -O0):
<__sc_nr_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1179 <__sc_nr_cpus+0x39>
movl   $0x1,-0x4(%rbp)
jmp    1287 <__sc_nr_cpus+0x147>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1279 <__sc_nr_cpus+0x139>
movl   $0x0,-0x814(%rbp)
mov    -0x814(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1274 <__sc_nr_cpus+0x134>
movslq -0x814(%rbp),%rax
movsbl -0x810(%rbp,%rax,1),%eax
ff
cmp    $0x70,%eax
jne    1214 <__sc_nr_cpus+0xd4>
lea    -0x810(%rbp),%rdi
movslq -0x814(%rbp),%rax
add    %rax,%rdi
lea    0xe25(%rip),%rsi
mov    $0x9,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%eax
jne    1214 <__sc_nr_cpus+0xd4>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x814(%rbp),%eax
add    $0x9,%eax
mov    %eax,-0x814(%rbp)
jmp    1219 <__sc_nr_cpus+0xd9>
mov    -0x814(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x815(%rbp)
jge    1257 <__sc_nr_cpus+0x117>
mov    -0x814(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x814(%rbp)
cltq
movsbl -0x810(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
setne  %al
mov    %al,-0x815(%rbp)
mov    -0x815(%rbp),%al
test   $0x1,%al
jne    126a <__sc_nr_cpus+0x12a>
jmp    126f <__sc_nr_cpus+0x12f>
jmp    1219 <__sc_nr_cpus+0xd9>
jmp    11aa <__sc_nr_cpus+0x6a>
jmp    1180 <__sc_nr_cpus+0x40>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sc_nr_cpus>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1203 <__sc_nr_cpus+0xc3>
mov    %eax,%r13d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x800,%edx
call   1040 <read@plt>
xor    %r12d,%r12d
test   %eax,%eax
jle    11f9 <__sc_nr_cpus+0xb9>
mov    %rsp,%r15
jmp    11a4 <__sc_nr_cpus+0x64>
nopl   (%rax)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x800,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11f9 <__sc_nr_cpus+0xb9>
mov    %eax,%ebp
xor    %r14d,%r14d
movslq %r14d,%rbx
cmpb   $0x70,(%rsp,%rbx,1)
jne    11e0 <__sc_nr_cpus+0xa0>
lea    (%rsp,%rbx,1),%rdi
lea    0xe51(%rip),%rsi
mov    $0x9,%edx
call   1050 <memcmp@plt>
test   %eax,%eax
jne    11e0 <__sc_nr_cpus+0xa0>
add    $0x1,%r12d
add    $0x9,%r14d
movslq %r14d,%rbx
cs nopw 0x0(%rax,%rax,1)
cmp    %rbp,%rbx
jge    1190 <__sc_nr_cpus+0x50>
lea    0x1(%rbx),%r14
cmpb   $0xa,(%rsp,%rbx,1)
mov    %r14,%rbx
jne    11e0 <__sc_nr_cpus+0xa0>
cmp    %r14d,%ebp
jg     11a9 <__sc_nr_cpus+0x69>
jmp    1190 <__sc_nr_cpus+0x50>
mov    %r13d,%edi
call   1030 <close@plt>
jmp    1209 <__sc_nr_cpus+0xc9>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x808,%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"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"processor\00"
define i32 @__sc_nr_cpus() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2048 x i8]
%6 = alloca i32
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 1, i32* %1
br label %64
12: ; preds = %0
store i32 0, i32* %4
br label %13
13: ; preds = %59, %12
%14 = load i32, i32* %2
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 2048)
store i32 %16, i32* %3
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %60
18: ; preds = %13
store i32 0, i32* %6
br label %19
19: ; preds = %58, %18
%20 = load i32, i32* %6
%21 = load i32, i32* %3
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %59
23: ; preds = %19
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 112
br i1 %29, label %30, label %42
30: ; preds = %23
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = call i32 @memcmp(i8* %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 9)
%36 = icmp ne i32 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %30
%38 = load i32, i32* %4
%39 = add nsw i32 %38, 1
store i32 %39, i32* %4
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 9
store i32 %41, i32* %6
br label %42
42: ; preds = %37, %30, %23
br label %43
43: ; preds = %57, %42
%44 = load i32, i32* %6
%45 = load i32, i32* %3
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %55
47: ; preds = %43
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp ne i32 %53, 10
br label %55
55: ; preds = %47, %43
%56 = phi i1 [ false, %43 ], [ %54, %47 ]
br i1 %56, label %57, label %58
57: ; preds = %55
br label %43
58: ; preds = %55
br label %19
59: ; preds = %19
br label %13
60: ; preds = %13
%61 = load i32, i32* %2
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %4
store i32 %63, i32* %1
br label %64
64: ; preds = %60, %11
%65 = load i32, i32* %1
ret i32 %65
}

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