#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 */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned int *, unsigned int *,
           unsigned int *, char *, unsigned int *);
int /*<<< orphan*/ *strstr(char *, char *);
int load_table(const char *filename, unsigned short *table, int size) {
  FILE *fp;
  fp = fopen(filename, "r");
  if (!fp) {
    return -1;
  }
  unsigned int address, cycles, branch, memory_utilization;
  char instruction_name[10];
  while (!feof(fp) &&
         fscanf(fp, "%x:%d:%d:%[^:]:%d\n", &address, &cycles, &branch,
                instruction_name, &memory_utilization) == 5) {
    if (strstr(instruction_name, "ldr") != NULL) {
      cycles = cycles | 0x40;
      cycles = cycles | (memory_utilization << 8);
    } else if (strstr(instruction_name, "str") != NULL) {
      cycles = cycles | 0x20;
    }
    if (branch == 1) {
      cycles = cycles | 0x80;
    }
    if (address >> 1 >= size) {
      return -1;
    }
    table[address >> 1] = cycles;
  }
  if (fclose(fp) != 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<load_table>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea7(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118e <load_table+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <load_table+0x16c>
jmp    1193 <load_table+0x43>
mov    -0x28(%rbp),%rdi
call   1050 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x43(%rbp)
jne    11dd <load_table+0x8d>
mov    -0x28(%rbp),%rdi
lea    -0x42(%rbp),%r9
lea    0xe45(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%r8
lea    -0x38(%rbp),%rax
mov    %rax,(%rsp)
call   1040 <fscanf@plt>
cmp    $0x5,%eax
sete   %al
mov    %al,-0x43(%rbp)
mov    -0x43(%rbp),%al
test   $0x1,%al
jne    11ed <load_table+0x9d>
jmp    1296 <load_table+0x146>
lea    -0x42(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1223 <load_table+0xd3>
mov    -0x30(%rbp),%eax
or     $0x40,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    -0x38(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x30(%rbp)
jmp    124b <load_table+0xfb>
lea    -0x42(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1246 <load_table+0xf6>
mov    -0x30(%rbp),%eax
or     $0x20,%eax
mov    %eax,-0x30(%rbp)
jmp    124b <load_table+0xfb>
cmpl   $0x1,-0x34(%rbp)
jne    1260 <load_table+0x110>
mov    -0x30(%rbp),%eax
or     $0x80,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
shr    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jb     127b <load_table+0x12b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <load_table+0x16c>
mov    -0x30(%rbp),%eax
mov    %ax,%dx
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shr    $0x1,%ecx
mov    %ecx,%ecx
mov    %dx,(%rax,%rcx,2)
jmp    1193 <load_table+0x43>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     12b5 <load_table+0x165>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <load_table+0x16c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_table>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     125f <load_table+0x10f>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     1199 <load_table+0x49>
mov    %rbx,%rdi
call   1030 <fclose@plt>
neg    %rax
sbb    %eax,%eax
jmp    1264 <load_table+0x114>
lea    0xc(%rsp),%r13
lea    0x14(%rsp),%r15
lea    0x1e(%rsp),%r12
nopl   0x0(%rax,%rax,1)
lea    0x10(%rsp),%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
lea    0xe3d(%rip),%rsi
lea    0x18(%rsp),%rdx
mov    %r13,%rcx
mov    %r15,%r8
mov    %r12,%r9
call   1040 <fscanf@plt>
cmp    $0x5,%eax
jne    1187 <load_table+0x37>
mov    %r12,%rdi
lea    0xe2e(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1210 <load_table+0xc0>
mov    0x10(%rsp),%eax
shl    $0x8,%eax
or     0xc(%rsp),%eax
or     $0x40,%eax
mov    %eax,0xc(%rsp)
jmp    1229 <load_table+0xd9>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
lea    0xdfd(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1229 <load_table+0xd9>
orb    $0x20,0xc(%rsp)
cmpl   $0x1,0x14(%rsp)
jne    1235 <load_table+0xe5>
orb    $0x80,0xc(%rsp)
mov    0x18(%rsp),%eax
shr    %rax
cmp    %ebp,%eax
jae    125f <load_table+0x10f>
movzwl 0xc(%rsp),%ecx
mov    %cx,(%r14,%rax,2)
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11b0 <load_table+0x60>
jmp    1187 <load_table+0x37>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"%x:%d:%d:%[^:]:%d\0A\00"
@.str.2 = [4 x i8] c"ldr\00"
@.str.3 = [4 x i8] c"str\00"
define i32 @load_table(i8* %0, i16* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [10 x i8]
store i8* %0, i8** %5
store i16* %1, i16** %6
store i32 %2, i32* %7
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 -1, i32* %4
br label %76
19: ; preds = %3
br label %20
20: ; preds = %62, %19
%21 = load i32*, i32** %8
%22 = call i32 @feof(i32* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = load i32*, i32** %8
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%27 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32* %9, i32* %10, i32* %11, i8* %26, i32* %12)
%28 = icmp eq i32 %27, 5
br label %29
29: ; preds = %24, %20
%30 = phi i1 [ false, %20 ], [ %28, %24 ]
br i1 %30, label %31, label %70
31: ; preds = %29
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%33 = call i32* @strstr(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i32, i32* %10
%37 = or i32 %36, 64
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = load i32, i32* %12
%40 = shl i32 %39, 8
%41 = or i32 %38, %40
store i32 %41, i32* %10
br label %50
42: ; preds = %31
%43 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%44 = call i32* @strstr(i8* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %10
%48 = or i32 %47, 32
store i32 %48, i32* %10
br label %49
49: ; preds = %46, %42
br label %50
50: ; preds = %49, %35
%51 = load i32, i32* %11
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* %10
%55 = or i32 %54, 128
store i32 %55, i32* %10
br label %56
56: ; preds = %53, %50
%57 = load i32, i32* %9
%58 = lshr i32 %57, 1
%59 = load i32, i32* %7
%60 = icmp uge i32 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %56
store i32 -1, i32* %4
br label %76
62: ; preds = %56
%63 = load i32, i32* %10
%64 = trunc i32 %63 to i16
%65 = load i16*, i16** %6
%66 = load i32, i32* %9
%67 = lshr i32 %66, 1
%68 = zext i32 %67 to i64
%69 = getelementptr inbounds i16, i16* %65, i64 %68
store i16 %64, i16* %69
br label %20
70: ; preds = %29
%71 = load i32*, i32** %8
%72 = call i64 @fclose(i32* %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %70
store i32 -1, i32* %4
br label %76
75: ; preds = %70
store i32 0, i32* %4
br label %76
76: ; preds = %75, %74, %61, %18
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"%x:%d:%d:%[^:]:%d\0A\00"
@.str.2 = [4 x i8] c"ldr\00"
@.str.3 = [4 x i8] c"str\00"
define i32 @load_table(i8* nocapture %0, i16* nocapture writeonly %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [10 x i8]
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %60, label %11
11: ; preds = %3
%12 = bitcast i32* %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = bitcast i32* %6 to i8*
%15 = bitcast i32* %7 to i8*
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%17 = call i32 @feof(i32* %9)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %54
19: ; preds = %11, %47
%20 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %5, i32* %6, i8* %16, i32* %7)
%21 = icmp eq i32 %20, 5
br i1 %21, label %22, label %54
22: ; preds = %19
%23 = call i32* @strstr(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = load i32, i32* %5
%27 = or i32 %26, 64
%28 = load i32, i32* %7
%29 = shl i32 %28, 8
%30 = or i32 %29, %27
store i32 %30, i32* %5
br label %37
31: ; preds = %22
%32 = call i32* @strstr(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i32, i32* %5
%36 = or i32 %35, 32
store i32 %36, i32* %5
br label %37
37: ; preds = %31, %34, %25
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* %5
%42 = or i32 %41, 128
store i32 %42, i32* %5
br label %43
43: ; preds = %40, %37
%44 = load i32, i32* %4
%45 = lshr i32 %44, 1
%46 = icmp ult i32 %45, %2
br i1 %46, label %47, label %58
47: ; preds = %43
%48 = load i32, i32* %5
%49 = trunc i32 %48 to i16
%50 = zext i32 %45 to i64
%51 = getelementptr inbounds i16, i16* %1, i64 %50
store i16 %49, i16* %51
%52 = call i32 @feof(i32* %9)
%53 = icmp eq i32 %52, 0
br i1 %53, label %19, label %54
54: ; preds = %19, %47, %11
%55 = call i64 @fclose(i32* %9)
%56 = icmp ne i64 %55, 0
%57 = sext i1 %56 to i32
br label %58
58: ; preds = %43, %54
%59 = phi i32 [ %57, %54 ], [ -1, %43 ]
br label %60
60: ; preds = %3, %58
%61 = phi i32 [ %59, %58 ], [ -1, %3 ]
ret i32 %61
}