#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
scalar_t__ opcode(unsigned char *, int);
int /*<<< orphan*/ printf(char *, char *);
int disassemble(char *filename) {
  FILE *file = fopen(filename, "rb");
  if (file == NULL) {
    printf("Error: cannot open '%s'\n", filename);
    return -1;
  }
  fseek(file, 0L, SEEK_END);
  int fsize = ftell(file);
  fseek(file, 0L, SEEK_SET);
  unsigned char *hexdump = malloc(fsize);
  fread(hexdump, fsize, 1, file);
  fclose(file);
  int pc = 0;
  while (pc < fsize) {
    pc += opcode(hexdump, pc);
  }
  free(hexdump);
  return 0;
}


x86 assembly (clang, -O0):
<disassemble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d7 <disassemble+0x47>
mov    -0x10(%rbp),%rsi
lea    0xe3d(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <disassemble+0xea>
mov    -0x18(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x1c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    126a <disassemble+0xda>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1080 <opcode@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
jmp    1240 <disassemble+0xb0>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disassemble>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
lea    0xe5e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     123d <disassemble+0xad>
mov    %rax,%r15
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %eax,%r12d
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r12d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %r12d,%r12d
jle    1233 <disassemble+0xa3>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <opcode@plt>
add    %ebp,%eax
mov    %eax,%ebp
cmp    %r12d,%eax
jl     1220 <disassemble+0x90>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1252 <disassemble+0xc2>
lea    0xdbf(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Error: cannot open '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @disassemble(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i32 -1, i32* %2
br label %47
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i64 0, i32 %17)
%19 = load i32*, i32** %4
%20 = call i32 @ftell(i32* %19)
store i32 %20, i32* %5
%21 = load i32*, i32** %4
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i64 0, i32 %22)
%24 = load i32, i32* %5
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = load i32, i32* %5
%28 = load i32*, i32** %4
%29 = call i32 @fread(i8* %26, i32 %27, i32 1, i32* %28)
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %7
br label %32
32: ; preds = %36, %15
%33 = load i32, i32* %7
%34 = load i32, i32* %5
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = load i8*, i8** %6
%38 = load i32, i32* %7
%39 = call i64 @opcode(i8* %37, i32 %38)
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = add nsw i64 %41, %39
%43 = trunc i64 %42 to i32
store i32 %43, i32* %7
br label %32
44: ; preds = %32
%45 = load i8*, i8** %6
%46 = call i32 @free(i8* %45)
store i32 0, i32* %2
br label %47
47: ; preds = %44, %12
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Error: cannot open '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @disassemble(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %24
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i64 0, i32 %7)
%9 = tail call i32 @ftell(i32* %2)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %2, i64 0, i32 %10)
%12 = tail call i8* @malloc(i32 %9)
%13 = tail call i32 @fread(i8* %12, i32 %9, i32 1, i32* %2)
%14 = tail call i32 @fclose(i32* %2)
%15 = icmp sgt i32 %9, 0
br i1 %15, label %16, label %22
16: ; preds = %6, %16
%17 = phi i32 [ %20, %16 ], [ 0, %6 ]
%18 = tail call i64 @opcode(i8* %12, i32 %17)
%19 = trunc i64 %18 to i32
%20 = add i32 %17, %19
%21 = icmp slt i32 %20, %9
br i1 %21, label %16, label %22
22: ; preds = %16, %6
%23 = tail call i32 @free(i8* %12)
br label %24
24: ; preds = %22, %4
%25 = phi i32 [ -1, %4 ], [ 0, %22 ]
ret i32 %25
}