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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *strstr(char *, char *);
char *strtok(char *, char *);
int strtol(char *, char **, int);
int bcm2835_get_pi_version(void) {
  int rev = 0;
  char buff[512];
  char *p;
  char *pend;
  FILE *fd;
  memset(buff, 0, sizeof(buff));
  fd = fopen("/proc/cpuinfo", "r");
  if (fd) {
    while (fgets(buff, sizeof(buff), fd) != NULL && rev == 0) {
      if ((strstr(buff, "Revision")) != NULL) {
        if ((p = strtok(buff, ":")) != NULL) {
          if ((p = strtok(NULL, ":")) != NULL) {
            *p = 'x';
            *--p = '0';
            rev = strtol(p, &pend, 16);
            if (!*pend) {
              rev = 0;
            } else {
              rev = (rev < 4) ? 1 : 2;
            }
          }
        }
      }
    }
    if (fd) {
      fclose(fd);
    }
  }
  return rev;
}


x86 assembly (clang, -O0):
<bcm2835_get_pi_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
lea    0xe64(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
je     1314 <bcm2835_get_pi_version+0x1a4>
jmp    11c2 <bcm2835_get_pi_version+0x52>
lea    -0x210(%rbp),%rdi
mov    -0x228(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x229(%rbp)
je     11fc <bcm2835_get_pi_version+0x8c>
cmpl   $0x0,-0x4(%rbp)
sete   %al
mov    %al,-0x229(%rbp)
mov    -0x229(%rbp),%al
test   $0x1,%al
jne    120f <bcm2835_get_pi_version+0x9f>
jmp    12f5 <bcm2835_get_pi_version+0x185>
lea    -0x210(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     12f0 <bcm2835_get_pi_version+0x180>
lea    -0x210(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x218(%rbp)
cmp    $0x0,%rax
je     12eb <bcm2835_get_pi_version+0x17b>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdbe(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x218(%rbp)
cmp    $0x0,%rax
je     12e6 <bcm2835_get_pi_version+0x176>
mov    -0x218(%rbp),%rax
movb   $0x78,(%rax)
mov    -0x218(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x218(%rbp)
movb   $0x30,-0x1(%rax)
mov    -0x218(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,-0x4(%rbp)
mov    -0x220(%rbp),%rax
cmpb   $0x0,(%rax)
jne    12cb <bcm2835_get_pi_version+0x15b>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <bcm2835_get_pi_version+0x171>
mov    -0x4(%rbp),%edx
mov    $0x2,%eax
mov    $0x1,%ecx
cmp    $0x4,%edx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12e6 <bcm2835_get_pi_version+0x176>
jmp    12eb <bcm2835_get_pi_version+0x17b>
jmp    12f0 <bcm2835_get_pi_version+0x180>
jmp    11c2 <bcm2835_get_pi_version+0x52>
cmpq   $0x0,-0x228(%rbp)
je     130f <bcm2835_get_pi_version+0x19f>
mov    -0x228(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1314 <bcm2835_get_pi_version+0x1a4>
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcm2835_get_pi_version>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
lea    0x10(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
lea    0xe65(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1289 <bcm2835_get_pi_version+0x119>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     127f <bcm2835_get_pi_version+0x10f>
lea    0xe3b(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xe38(%rip),%r13
lea    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <strstr@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1260 <bcm2835_get_pi_version+0xf0>
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     1260 <bcm2835_get_pi_version+0xf0>
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r13,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     1260 <bcm2835_get_pi_version+0xf0>
mov    %rax,%rdi
add    $0xffffffffffffffff,%rdi
movw   $0x7830,-0x1(%rax)
mov    %rbp,%rsi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1260 <bcm2835_get_pi_version+0xf0>
xor    %ecx,%ecx
cmp    $0x4,%eax
setl   %cl
mov    $0x2,%ebx
sub    %ecx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1281 <bcm2835_get_pi_version+0x111>
test   %ebx,%ebx
je     11f0 <bcm2835_get_pi_version+0x80>
jmp    1281 <bcm2835_get_pi_version+0x111>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x218,%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 = [9 x i8] c"Revision\00"
@.str.3 = [2 x i8] c":\00"
define i32 @bcm2835_get_pi_version() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i32 0, i32* %1
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 512)
%8 = 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* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %59
11: ; preds = %0
br label %12
12: ; preds = %51, %11
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%14 = load i32*, i32** %5
%15 = call i32* @fgets(i8* %13, i32 512, i32* %14)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* %1
%19 = icmp eq i32 %18, 0
br label %20
20: ; preds = %17, %12
%21 = phi i1 [ false, %12 ], [ %19, %17 ]
br i1 %21, label %22, label %52
22: ; preds = %20
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%24 = call i32* @strstr(i8* %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %51
26: ; preds = %22
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%28 = call i8* @strtok(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %28, i8** %3
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %50
30: ; preds = %26
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %31, i8** %3
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %49
33: ; preds = %30
%34 = load i8*, i8** %3
store i8 120, i8* %34
%35 = load i8*, i8** %3
%36 = getelementptr inbounds i8, i8* %35, i32 -1
store i8* %36, i8** %3
store i8 48, i8* %36
%37 = load i8*, i8** %3
%38 = call i32 @strtol(i8* %37, i8** %4, i32 16)
store i32 %38, i32* %1
%39 = load i8*, i8** %4
%40 = load i8, i8* %39
%41 = icmp ne i8 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %33
store i32 0, i32* %1
br label %48
43: ; preds = %33
%44 = load i32, i32* %1
%45 = icmp slt i32 %44, 4
%46 = zext i1 %45 to i64
%47 = select i1 %45, i32 1, i32 2
store i32 %47, i32* %1
br label %48
48: ; preds = %43, %42
br label %49
49: ; preds = %48, %30
br label %50
50: ; preds = %49, %26
br label %51
51: ; preds = %50, %22
br label %12
52: ; preds = %20
%53 = load i32*, i32** %5
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32*, i32** %5
%57 = call i32 @fclose(i32* %56)
br label %58
58: ; preds = %55, %52
br label %59
59: ; preds = %58, %0
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"Revision\00"
@.str.3 = [2 x i8] c":\00"
define i32 @bcm2835_get_pi_version() {
%1 = alloca [512 x i8]
%2 = alloca i8*
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%4 = bitcast i8** %2 to i8*
%5 = call i32 @memset(i8* %3, i32 0, i32 512)
%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))
%7 = icmp eq i32* %6, null
br i1 %7, label %38, label %8
8: ; preds = %0
%9 = call i32* @fgets(i8* %3, i32 512, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %35, label %11
11: ; preds = %8, %29
%12 = call i32* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %29, label %14
14: ; preds = %11
%15 = call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %29, label %17
17: ; preds = %14
%18 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %29, label %20
20: ; preds = %17
store i8 120, i8* %18
%21 = getelementptr inbounds i8, i8* %18, i64 -1
store i8 48, i8* %21
%22 = call i32 @strtol(i8* %21, i8** %2, i32 16)
%23 = load i8*, i8** %2
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = icmp slt i32 %22, 4
%28 = select i1 %27, i32 1, i32 2
br label %29
29: ; preds = %20, %14, %26, %17, %11
%30 = phi i32 [ %28, %26 ], [ 0, %17 ], [ 0, %14 ], [ 0, %11 ], [ 0, %20 ]
%31 = call i32* @fgets(i8* %3, i32 512, i32* %6)
%32 = icmp ne i32* %31, null
%33 = icmp eq i32 %30, 0
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %11, label %35
35: ; preds = %29, %8
%36 = phi i32 [ 0, %8 ], [ %30, %29 ]
%37 = call i32 @fclose(i32* %6)
br label %38
38: ; preds = %35, %0
%39 = phi i32 [ %36, %35 ], [ 0, %0 ]
ret i32 %39
}