#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fstat(int, struct stat *);
unsigned int length_disasm(void *);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  void *addr, *op;
  struct stat buf;
  int fd;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    return -1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("open");
    return -2;
  }
  if (fstat(fd, &buf) < 0) {
    perror("fstat");
    return -3;
  }
  op = addr = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    return -4;
  }
  while (op < addr + buf.st_size) {
    unsigned int len = length_disasm(op);
    printf("%d\n", len);
    op += len;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x48>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <main+0x15e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11f3 <main+0x83>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12ce <main+0x15e>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    1221 <main+0xb1>
lea    0xe09(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12ce <main+0x15e>
mov    -0x28(%rbp),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
mov    0x2d94(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
jne    127a <main+0x10a>
lea    0xdb6(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12ce <main+0x15e>
jmp    127f <main+0x10f>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x28(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    12c7 <main+0x157>
mov    -0x20(%rbp),%rdi
call   1050 <length_disasm@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xd77(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    127f <main+0x10f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     11a4 <main+0x34>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe6c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%r14d
jmp    123e <main+0xce>
mov    0x8(%rsi),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     124e <main+0xde>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     1262 <main+0xf2>
mov    0x8(%rsp),%ebp
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2db9(%rip),%rax
cmp    (%rax),%rbx
je     1276 <main+0x106>
movslq %ebp,%r12
add    %rbx,%r12
cmp    %r12,%rbx
jae    123e <main+0xce>
lea    0xe06(%rip),%r15
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <length_disasm@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebp,%eax
add    %rax,%rbx
cmp    %r12,%rbx
jb     1220 <main+0xb0>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffe,%r14d
jmp    123e <main+0xce>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffd,%r14d
jmp    123e <main+0xce>
lea    0xda2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffc,%r14d
jmp    123e <main+0xce>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %66
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %66
29: ; preds = %19
%30 = load i32, i32* %9
%31 = call i64 @fstat(i32 %30, %struct.stat* %8)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %3
br label %66
35: ; preds = %29
%36 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @MAP_PRIVATE
%40 = load i32, i32* %9
%41 = call i8* @mmap(i32* null, i32 %37, i32 %38, i32 %39, i32 %40, i32 0)
store i8* %41, i8** %6
store i8* %41, i8** %7
%42 = load i8*, i8** %6
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -4, i32* %3
br label %66
47: ; preds = %35
br label %48
48: ; preds = %56, %47
%49 = load i8*, i8** %7
%50 = load i8*, i8** %6
%51 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%52 = load i32, i32* %51
%53 = sext i32 %52 to i64
%54 = getelementptr i8, i8* %50, i64 %53
%55 = icmp ult i8* %49, %54
br i1 %55, label %56, label %65
56: ; preds = %48
%57 = load i8*, i8** %7
%58 = call i32 @length_disasm(i8* %57)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %10
%62 = load i8*, i8** %7
%63 = zext i32 %61 to i64
%64 = getelementptr i8, i8* %62, i64 %63
store i8* %64, i8** %7
br label %48
65: ; preds = %48
store i32 0, i32* %3
br label %66
66: ; preds = %65, %45, %33, %27, %13
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %44
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
18: ; preds = %10
%19 = call i64 @fstat(i32 %14, %struct.stat* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %44
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_PRIVATE
%28 = tail call i8* @mmap(i32* null, i32 %25, i32 %26, i32 %27, i32 %14, i32 0)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %35, label %31
31: ; preds = %23
%32 = sext i32 %25 to i64
%33 = getelementptr i8, i8* %28, i64 %32
%34 = icmp ult i8* %28, %33
br i1 %34, label %37, label %44
35: ; preds = %23
%36 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %44
37: ; preds = %31, %37
%38 = phi i8* [ %42, %37 ], [ %28, %31 ]
%39 = tail call i32 @length_disasm(i8* %38)
%40 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = zext i32 %39 to i64
%42 = getelementptr i8, i8* %38, i64 %41
%43 = icmp ult i8* %42, %33
br i1 %43, label %37, label %44
44: ; preds = %37, %31, %35, %21, %16, %6
%45 = phi i32 [ -1, %6 ], [ -2, %16 ], [ -3, %21 ], [ -4, %35 ], [ 0, %31 ], [ 0, %37 ]
ret i32 %45
}