#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 /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int strtol(char *, char **, int);
int board_rev(void) {
  char buf[128];
  char *ptr, *end, *res;
  static int rev = 0;
  FILE *fp;
  if (rev)
    return rev;
  fp = fopen("/proc/cpuinfo", "r");
  if (!fp)
    fatal("Unable to open /proc/cpuinfo: %m\n");
  while ((res = fgets(buf, 128, fp))) {
    if (!strncmp(buf, "Revision", 8))
      break;
  }
  fclose(fp);
  if (!res)
    fatal("servod: No 'Revision' record in /proc/cpuinfo\n");
  ptr = buf + strlen(buf) - 3;
  rev = strtol(ptr, &end, 16);
  if (end != ptr + 2)
    fatal("servod: Failed to parse Revision string\n");
  if (rev < 1)
    fatal("servod: Invalid board Revision\n");
  else if (rev < 4)
    rev = 1;
  else
    rev = 2;
  return rev;
}


x86 assembly (clang, -O0):
<board_rev>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
cmpl   $0x0,0x2eda(%rip)
je     1196 <board_rev+0x26>
mov    0x2ece(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    1314 <board_rev+0x1a4>
lea    0xe63(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jne    11ca <board_rev+0x5a>
lea    0xe4b(%rip),%rdi
call   1090 <fatal@plt>
jmp    11cf <board_rev+0x5f>
lea    -0x90(%rbp),%rdi
mov    -0xb0(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
mov    %rax,-0xa8(%rbp)
cmp    $0x0,%rax
je     1223 <board_rev+0xb3>
lea    -0x90(%rbp),%rdi
lea    0xe2c(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    121e <board_rev+0xae>
jmp    1223 <board_rev+0xb3>
jmp    11cf <board_rev+0x5f>
mov    -0xb0(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0xa8(%rbp)
jne    1249 <board_rev+0xd9>
lea    0xdf7(%rip),%rdi
call   1090 <fatal@plt>
lea    -0x90(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0xb8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xfffffffffffffffd,%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,0x2dc1(%rip)
mov    -0xa0(%rbp),%rax
mov    -0x98(%rbp),%rcx
add    $0x2,%rcx
cmp    %rcx,%rax
je     12c2 <board_rev+0x152>
lea    0xdad(%rip),%rdi
call   1090 <fatal@plt>
cmpl   $0x1,0x2d93(%rip)
jge    12e0 <board_rev+0x170>
lea    0xdbd(%rip),%rdi
call   1090 <fatal@plt>
jmp    130b <board_rev+0x19b>
cmpl   $0x4,0x2d75(%rip)
jge    12fc <board_rev+0x18c>
movl   $0x1,0x2d65(%rip)
jmp    1306 <board_rev+0x196>
movl   $0x2,0x2d56(%rip)
jmp    130b <board_rev+0x19b>
mov    0x2d4b(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<board_rev>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    0x2eda(%rip),%eax
test   %eax,%eax
je     1193 <board_rev+0x23>
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe66(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11ba <board_rev+0x4a>
lea    0xe5b(%rip),%rdi
call   1090 <fatal@plt>
lea    0x10(%rsp),%rbx
lea    0xe6c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1203 <board_rev+0x93>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11d0 <board_rev+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    1217 <board_rev+0xa7>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe29(%rip),%rdi
call   1090 <fatal@plt>
lea    0x10(%rsp),%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
lea    (%rsp,%rbx,1),%rdi
add    $0xd,%rdi
lea    0x8(%rsp),%rsi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,0x2e1b(%rip)
lea    (%rsp,%rbx,1),%rcx
add    $0xf,%rcx
cmp    %rcx,0x8(%rsp)
je     1262 <board_rev+0xf2>
lea    0xe13(%rip),%rdi
call   1090 <fatal@plt>
mov    0x2dfa(%rip),%eax
test   %eax,%eax
jle    127f <board_rev+0x10f>
cmp    $0x3,%eax
ja     1296 <board_rev+0x126>
movl   $0x1,0x2de7(%rip)
mov    $0x1,%eax
jmp    1186 <board_rev+0x16>
lea    0xe0d(%rip),%rdi
call   1090 <fatal@plt>
mov    0x2dcb(%rip),%eax
jmp    1186 <board_rev+0x16>
movl   $0x2,0x2dbc(%rip)
mov    $0x2,%eax
jmp    1186 <board_rev+0x16>

LLVM IR (-O0):
@board_rev.rev = internal global i32 0
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"Unable to open /proc/cpuinfo: %m\0A\00"
@.str.3 = [9 x i8] c"Revision\00"
@.str.4 = [47 x i8] c"servod: No 'Revision' record in /proc/cpuinfo\0A\00"
@.str.5 = [41 x i8] c"servod: Failed to parse Revision string\0A\00"
@.str.6 = [32 x i8] c"servod: Invalid board Revision\0A\00"
define i32 @board_rev() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = load i32, i32* @board_rev.rev
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = load i32, i32* @board_rev.rev
store i32 %10, i32* %1
br label %64
11: ; preds = %0
%12 = 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* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 @fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
br label %18
18: ; preds = %28, %17
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i8* @fgets(i8* %19, i32 128, i32* %20)
store i8* %21, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%25 = call i32 @strncmp(i8* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %23
br label %29
28: ; preds = %23
br label %18
29: ; preds = %27, %18
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %36, label %34
34: ; preds = %29
%35 = call i32 @fatal(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = getelementptr inbounds i8, i8* %41, i64 -3
store i8* %42, i8** %3
%43 = load i8*, i8** %3
%44 = call i32 @strtol(i8* %43, i8** %4, i32 16)
store i32 %44, i32* @board_rev.rev
%45 = load i8*, i8** %4
%46 = load i8*, i8** %3
%47 = getelementptr inbounds i8, i8* %46, i64 2
%48 = icmp ne i8* %45, %47
br i1 %48, label %49, label %51
49: ; preds = %36
%50 = call i32 @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %36
%52 = load i32, i32* @board_rev.rev
%53 = icmp slt i32 %52, 1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
br label %62
56: ; preds = %51
%57 = load i32, i32* @board_rev.rev
%58 = icmp slt i32 %57, 4
br i1 %58, label %59, label %60
59: ; preds = %56
store i32 1, i32* @board_rev.rev
br label %61
60: ; preds = %56
store i32 2, i32* @board_rev.rev
br label %61
61: ; preds = %60, %59
br label %62
62: ; preds = %61, %54
%63 = load i32, i32* @board_rev.rev
store i32 %63, i32* %1
br label %64
64: ; preds = %62, %9
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@board_rev.rev = internal unnamed_addr global i32 0
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"Unable to open /proc/cpuinfo: %m\0A\00"
@.str.3 = [9 x i8] c"Revision\00"
@.str.4 = [47 x i8] c"servod: No 'Revision' record in /proc/cpuinfo\0A\00"
@.str.5 = [41 x i8] c"servod: Failed to parse Revision string\0A\00"
@.str.6 = [32 x i8] c"servod: Invalid board Revision\0A\00"
define i32 @board_rev() {
%1 = alloca [128 x i8]
%2 = alloca i8*
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = bitcast i8** %2 to i8*
%5 = load i32, i32* @board_rev.rev
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %46
7: ; preds = %0
%8 = 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))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
br label %13
13: ; preds = %12, %16
%14 = call i8* @fgets(i8* %3, i32 128, i32* %8)
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = call i32 @strncmp(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %13
19: ; preds = %16
%20 = call i32 @fclose(i32* %8)
br label %24
21: ; preds = %13
%22 = call i32 @fclose(i32* %8)
%23 = call i32 @fatal(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %19, %21
%25 = call i32 @strlen(i8* %3)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 -3
%29 = call i32 @strtol(i8* %28, i8** %2, i32 16)
store i32 %29, i32* @board_rev.rev
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %27, i64 -1
%32 = icmp eq i8* %30, %31
br i1 %32, label %36, label %33
33: ; preds = %24
%34 = call i32 @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* @board_rev.rev
br label %36
36: ; preds = %33, %24
%37 = phi i32 [ %35, %33 ], [ %29, %24 ]
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i32 @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%41 = load i32, i32* @board_rev.rev
br label %46
42: ; preds = %36
%43 = icmp ult i32 %37, 4
br i1 %43, label %44, label %45
44: ; preds = %42
store i32 1, i32* @board_rev.rev
br label %46
45: ; preds = %42
store i32 2, i32* @board_rev.rev
br label %46
46: ; preds = %39, %45, %44, %0
%47 = phi i32 [ %5, %0 ], [ 1, %44 ], [ 2, %45 ], [ %41, %39 ]
ret i32 %47
}