#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*/ uint8_t;
struct vc4_info {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ decode(struct vc4_info *, long, int /*<<< orphan*/ *,
                          size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
long strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ vc4_free_info(struct vc4_info *);
int /*<<< orphan*/ vc4_get_opcodes(struct vc4_info *);
struct vc4_info *vc4_read_arch_file(char *);
int main(int argc, char *argv[]) {
  char *arch = getenv("VC4_ARCH");
  if (arch == NULL)
    arch = "/home/marmar01/src/rpi/videocoreiv/videocoreiv.arch";
  struct vc4_info *info = vc4_read_arch_file(arch);
  if (info == NULL) {
    perror("Can't open videocoreiv.arch");
    return 1;
  }
  vc4_get_opcodes(info);
  FILE *fp;
  uint8_t buf[0x10000 * 8];
  size_t len;
  long off = 0;
  char *name = "bootcode.bin";
  if (argc > 2)
    off = strtol(argv[2], NULL, 0);
  if (argc > 1)
    name = argv[1];
  fp = fopen(name, "r");
  if (fp == NULL) {
    perror("Can't open file");
    return 1;
  }
  if (off != 0)
    fseek(fp, off, SEEK_SET);
  len = fread(buf, 1, 0x10000 * 8, fp);
  decode(info, off, buf, len);
  fclose(fp);
  vc4_free_info(info);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ef <main+0x3f>
lea    0xe1e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   10c0 <vc4_read_arch_file@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121f <main+0x6f>
lea    0xe2f(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x187>
mov    -0x20(%rbp),%rdi
call   10d0 <vc4_get_opcodes@plt>
movq   $0x0,-0x200040(%rbp)
lea    0xe1f(%rip),%rax
mov    %rax,-0x200048(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1265 <main+0xb5>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %rax,-0x200040(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    127e <main+0xce>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x200048(%rbp)
mov    -0x200048(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b8 <main+0x108>
lea    0xdc1(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x187>
cmpq   $0x0,-0x200040(%rbp)
je     12df <main+0x12f>
mov    -0x28(%rbp),%rdi
mov    -0x200040(%rbp),%rsi
mov    0x2d08(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
lea    -0x200030(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80000,%edx
call   1040 <fread@plt>
mov    %rax,-0x200038(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x200040(%rbp),%rsi
lea    -0x200030(%rbp),%rdx
mov    -0x200038(%rbp),%rcx
call   1080 <decode@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1090 <vc4_free_info@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x200050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x200008,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe37(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe31(%rip),%rdi
cmovne %rax,%rdi
call   10c0 <vc4_read_arch_file@plt>
test   %rax,%rax
je     12b2 <main+0x102>
mov    %rax,%r14
mov    %rax,%rdi
call   10d0 <vc4_get_opcodes@plt>
cmp    $0x3,%ebp
jl     120c <main+0x5c>
mov    0x10(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %rax,%r15
jmp    1214 <main+0x64>
cmp    $0x2,%ebp
jne    124b <main+0x9b>
xor    %r15d,%r15d
mov    0x8(%rbx),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12bb <main+0x10b>
mov    %rax,%rbx
test   %r15,%r15
je     1266 <main+0xb6>
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <fseek@plt>
jmp    1269 <main+0xb9>
lea    0xe07(%rip),%rdi
lea    0xe0d(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12bb <main+0x10b>
mov    %rax,%rbx
xor    %r15d,%r15d
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x80000,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1080 <decode@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1090 <vc4_free_info@plt>
xor    %eax,%eax
add    $0x200008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd84(%rip),%rdi
jmp    12c2 <main+0x112>
lea    0xda6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
jmp    12a4 <main+0xf4>

LLVM IR (-O0):
%struct.vc4_info = type { i32 }
@.str = [9 x i8] c"VC4_ARCH\00"
@.str.1 = [52 x i8] c"/home/marmar01/src/rpi/videocoreiv/videocoreiv.arch\00"
@.str.2 = [28 x i8] c"Can't open videocoreiv.arch\00"
@.str.3 = [13 x i8] c"bootcode.bin\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"Can't open file\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.vc4_info*
%8 = alloca i32*
%9 = alloca [524288 x i32]
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %17
17: ; preds = %16, %2
%18 = load i8*, i8** %6
%19 = call %struct.vc4_info* @vc4_read_arch_file(i8* %18)
store %struct.vc4_info* %19, %struct.vc4_info** %7
%20 = load %struct.vc4_info*, %struct.vc4_info** %7
%21 = icmp eq %struct.vc4_info* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %69
24: ; preds = %17
%25 = load %struct.vc4_info*, %struct.vc4_info** %7
%26 = call i32 @vc4_get_opcodes(%struct.vc4_info* %25)
store i64 0, i64* %11
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %12
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 2
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i64 @strtol(i8* %32, i32* null, i32 0)
store i64 %33, i64* %11
br label %34
34: ; preds = %29, %24
%35 = load i32, i32* %4
%36 = icmp sgt i32 %35, 1
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
store i8* %40, i8** %12
br label %41
41: ; preds = %37, %34
%42 = load i8*, i8** %12
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %43, i32** %8
%44 = load i32*, i32** %8
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %69
48: ; preds = %41
%49 = load i64, i64* %11
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32*, i32** %8
%53 = load i64, i64* %11
%54 = load i32, i32* @SEEK_SET
%55 = call i32 @fseek(i32* %52, i64 %53, i32 %54)
br label %56
56: ; preds = %51, %48
%57 = getelementptr inbounds [524288 x i32], [524288 x i32]* %9, i64 0, i64 0
%58 = load i32*, i32** %8
%59 = call i64 @fread(i32* %57, i32 1, i32 524288, i32* %58)
store i64 %59, i64* %10
%60 = load %struct.vc4_info*, %struct.vc4_info** %7
%61 = load i64, i64* %11
%62 = getelementptr inbounds [524288 x i32], [524288 x i32]* %9, i64 0, i64 0
%63 = load i64, i64* %10
%64 = call i32 @decode(%struct.vc4_info* %60, i64 %61, i32* %62, i64 %63)
%65 = load i32*, i32** %8
%66 = call i32 @fclose(i32* %65)
%67 = load %struct.vc4_info*, %struct.vc4_info** %7
%68 = call i32 @vc4_free_info(%struct.vc4_info* %67)
store i32 0, i32* %3
br label %69
69: ; preds = %56, %46, %22
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.vc4_info = type { i32 }
@.str = [9 x i8] c"VC4_ARCH\00"
@.str.1 = [52 x i8] c"/home/marmar01/src/rpi/videocoreiv/videocoreiv.arch\00"
@.str.2 = [28 x i8] c"Can't open videocoreiv.arch\00"
@.str.3 = [13 x i8] c"bootcode.bin\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"Can't open file\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [524288 x i32]
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
%6 = select i1 %5, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %4
%7 = tail call %struct.vc4_info* @vc4_read_arch_file(i8* %6)
%8 = icmp eq %struct.vc4_info* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %47
11: ; preds = %2
%12 = tail call i32 @vc4_get_opcodes(%struct.vc4_info* %7)
%13 = bitcast [524288 x i32]* %3 to i8*
%14 = icmp sgt i32 %0, 2
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i64 @strtol(i8* nocapture %17, i32* null, i32 0)
br label %21
19: ; preds = %11
%20 = icmp eq i32 %0, 2
br i1 %20, label %21, label %27
21: ; preds = %19, %15
%22 = phi i64 [ %18, %15 ], [ 0, %19 ]
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = tail call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %30, label %32
27: ; preds = %19
%28 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %37
30: ; preds = %27, %21
%31 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %45
32: ; preds = %21
%33 = icmp eq i64 %22, 0
br i1 %33, label %37, label %34
34: ; preds = %32
%35 = load i32, i32* @SEEK_SET
%36 = tail call i32 @fseek(i32* %25, i64 %22, i32 %35)
br label %37
37: ; preds = %27, %34, %32
%38 = phi i64 [ %22, %34 ], [ 0, %32 ], [ 0, %27 ]
%39 = phi i32* [ %25, %34 ], [ %25, %32 ], [ %28, %27 ]
%40 = getelementptr inbounds [524288 x i32], [524288 x i32]* %3, i64 0, i64 0
%41 = call i64 @fread(i32* %40, i32 1, i32 524288, i32* %39)
%42 = call i32 @decode(%struct.vc4_info* %7, i64 %38, i32* %40, i64 %41)
%43 = call i32 @fclose(i32* %39)
%44 = call i32 @vc4_free_info(%struct.vc4_info* %7)
br label %45
45: ; preds = %37, %30
%46 = phi i32 [ 1, %30 ], [ 0, %37 ]
br label %47
47: ; preds = %45, %9
%48 = phi i32 [ 1, %9 ], [ %46, %45 ]
ret i32 %48
}