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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ check_endianess(void *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ find_text_section_from_ph(void *, struct elf_header *, int *,
                                             int *);
int /*<<< orphan*/ find_text_section_from_sh(void *, struct elf_header *, int *,
                                             int *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
scalar_t__ munmap(void *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_opcode(int);
int /*<<< orphan*/ printf(char *, ...);
struct elf_header *read_elf_header(void *);
scalar_t__ stat(char *, struct stat *);
int main(int argc, char **argv) {
  int i, fd;
  void *map;
  struct stat s;
  struct elf_header *ehdr;
  uint32_t codeoff, codesz;
  if (argc < 2) {
    printf("Usage: %s module.ko\n", argv[0]);
    exit(1);
  }
  if (stat(argv[1], &s)) {
    perror("stat()");
    exit(1);
  }
  if ((fd = open(argv[1], O_RDWR)) == -1) {
    perror("open()");
    exit(1);
  }
  map = mmap(NULL, s.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED) {
    perror("mmap()");
    exit(1);
  }
  check_endianess(map);
  ehdr = read_elf_header(map);
  if (!find_text_section_from_ph(map, ehdr, &codeoff, &codesz) &&
      !find_text_section_from_sh(map, ehdr, &codeoff, &codesz)) {
    printf("Unable to determine begin of code section\n");
    exit(2);
  }
  for (i = 0; i < codesz; i += sizeof(uint32_t)) {
    printf("%08x: ", codeoff + i);
    print_opcode(map + codeoff + i);
  }
  if (munmap(map, s.st_size)) {
    perror("munmap()");
    exit(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    120f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     1240 <main+0x70>
lea    0xde4(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1278 <main+0xa8>
lea    0xdb3(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x28(%rbp),%esi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    0x2d3d(%rip),%rax
or     (%rax),%edx
mov    0x2d54(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d0b(%rip),%rcx
cmp    (%rcx),%rax
jne    12d4 <main+0x104>
lea    0xd5e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   10d0 <check_endianess@plt>
mov    -0x20(%rbp),%rdi
call   1030 <read_elf_header@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1040 <find_text_section_from_ph@plt>
cmp    $0x0,%eax
jne    133e <main+0x16e>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <find_text_section_from_sh@plt>
cmp    $0x0,%eax
jne    133e <main+0x16e>
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    138e <main+0x1be>
mov    -0x34(%rbp),%esi
add    -0x14(%rbp),%esi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
add    %rcx,%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %eax,%edi
call   10f0 <print_opcode@plt>
movslq -0x14(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x14(%rbp)
jmp    1345 <main+0x175>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10a0 <munmap@plt>
cmp    $0x0,%rax
je     13ba <main+0x1ea>
lea    0xcb1(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jle    131c <main+0x13c>
mov    0x8(%rbx),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <stat@plt>
test   %rax,%rax
jne    1337 <main+0x157>
mov    0x8(%rbx),%rdi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1340 <main+0x160>
mov    %eax,%r14d
mov    0x10(%rsp),%esi
mov    0x2dc2(%rip),%rax
mov    0x2d8b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da0(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2d5f(%rip),%rax
cmp    (%rax),%rbx
je     1349 <main+0x169>
mov    %rbx,%rdi
call   10e0 <check_endianess@plt>
mov    %rbx,%rdi
call   1030 <read_elf_header@plt>
mov    %rax,%rbp
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <find_text_section_from_ph@plt>
test   %eax,%eax
jne    12b3 <main+0xd3>
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <find_text_section_from_sh@plt>
test   %eax,%eax
je     1368 <main+0x188>
cmpl   $0x0,0x8(%rsp)
jle    12f6 <main+0x116>
lea    0xd69(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%esi
add    %ebp,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0xc(%rsp),%edi
add    %ebx,%edi
add    %ebp,%edi
call   1100 <print_opcode@plt>
add    $0x4,%ebp
cmp    0x8(%rsp),%ebp
jl     12d0 <main+0xf0>
mov    0x10(%rsp),%esi
mov    %rbx,%rdi
call   10b0 <munmap@plt>
test   %rax,%rax
jne    1352 <main+0x172>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xcda(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xcd7(%rip),%rdi
jmp    1359 <main+0x179>
lea    0xcd5(%rip),%rdi
jmp    1359 <main+0x179>
lea    0xcd3(%rip),%rdi
jmp    1359 <main+0x179>
lea    0xcd8(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xccb(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.elf_header = type { i32 }
@.str = [21 x i8] c"Usage: %s module.ko\0A\00"
@.str.1 = [7 x i8] c"stat()\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [7 x i8] c"mmap()\00"
@.str.4 = [43 x i8] c"Unable to determine begin of code section\0A\00"
@.str.5 = [7 x i8] c"%08x: \00"
@.str.6 = [9 x i8] c"munmap()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca %struct.elf_header*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i64 @stat(i8* %24, %struct.stat* %9)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* @PROT_READ
%44 = load i32, i32* @PROT_WRITE
%45 = or i32 %43, %44
%46 = load i32, i32* @MAP_SHARED
%47 = load i32, i32* %7
%48 = call i8* @mmap(i32* null, i32 %42, i32 %45, i32 %46, i32 %47, i32 0)
store i8* %48, i8** %8
%49 = load i8*, i8** %8
%50 = load i8*, i8** @MAP_FAILED
%51 = icmp eq i8* %49, %50
br i1 %51, label %52, label %55
52: ; preds = %40
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %40
%56 = load i8*, i8** %8
%57 = call i32 @check_endianess(i8* %56)
%58 = load i8*, i8** %8
%59 = call %struct.elf_header* @read_elf_header(i8* %58)
store %struct.elf_header* %59, %struct.elf_header** %10
%60 = load i8*, i8** %8
%61 = load %struct.elf_header*, %struct.elf_header** %10
%62 = call i32 @find_text_section_from_ph(i8* %60, %struct.elf_header* %61, i32* %11, i32* %12)
%63 = icmp ne i32 %62, 0
br i1 %63, label %72, label %64
64: ; preds = %55
%65 = load i8*, i8** %8
%66 = load %struct.elf_header*, %struct.elf_header** %10
%67 = call i32 @find_text_section_from_sh(i8* %65, %struct.elf_header* %66, i32* %11, i32* %12)
%68 = icmp ne i32 %67, 0
br i1 %68, label %72, label %69
69: ; preds = %64
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
%71 = call i32 @exit(i32 2)
unreachable
72: ; preds = %64, %55
store i32 0, i32* %6
br label %73
73: ; preds = %91, %72
%74 = load i32, i32* %6
%75 = load i32, i32* %12
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %96
77: ; preds = %73
%78 = load i32, i32* %11
%79 = load i32, i32* %6
%80 = add nsw i32 %78, %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %80)
%82 = load i8*, i8** %8
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = getelementptr i8, i8* %82, i64 %84
%86 = load i32, i32* %6
%87 = sext i32 %86 to i64
%88 = getelementptr i8, i8* %85, i64 %87
%89 = ptrtoint i8* %88 to i32
%90 = call i32 @print_opcode(i32 %89)
br label %91
91: ; preds = %77
%92 = load i32, i32* %6
%93 = sext i32 %92 to i64
%94 = add i64 %93, 4
%95 = trunc i64 %94 to i32
store i32 %95, i32* %6
br label %73
96: ; preds = %73
%97 = load i8*, i8** %8
%98 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%99 = load i32, i32* %98
%100 = call i64 @munmap(i8* %97, i32 %99)
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %105
102: ; preds = %96
%103 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%104 = call i32 @exit(i32 1)
unreachable
105: ; preds = %96
%106 = load i32, i32* %7
%107 = call i32 @close(i32 %106)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.elf_header = type { i32 }
@.str = [21 x i8] c"Usage: %s module.ko\0A\00"
@.str.1 = [7 x i8] c"stat()\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [7 x i8] c"mmap()\00"
@.str.5 = [7 x i8] c"%08x: \00"
@.str.6 = [9 x i8] c"munmap()\00"
@str = [42 x i8] c"Unable to determine begin of code section\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i64 @stat(i8* %16, %struct.stat* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = load i8*, i8** %15
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = call i8* @mmap(i32* null, i32 %32, i32 %35, i32 %36, i32 %25, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %30
%44 = call i32 @check_endianess(i8* %37)
%45 = call %struct.elf_header* @read_elf_header(i8* %37)
%46 = call i32 @find_text_section_from_ph(i8* %37, %struct.elf_header* %45, i32* %4, i32* %5)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %43
%49 = call i32 @find_text_section_from_sh(i8* %37, %struct.elf_header* %45, i32* %4, i32* %5)
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%53 = call i32 @exit(i32 2)
unreachable
54: ; preds = %48, %43
%55 = load i32, i32* %5
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %73
57: ; preds = %54, %57
%58 = phi i32 [ %70, %57 ], [ 0, %54 ]
%59 = load i32, i32* %4
%60 = add nsw i32 %59, %58
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %4
%63 = sext i32 %62 to i64
%64 = getelementptr i8, i8* %37, i64 %63
%65 = sext i32 %58 to i64
%66 = getelementptr i8, i8* %64, i64 %65
%67 = ptrtoint i8* %66 to i64
%68 = trunc i64 %67 to i32
%69 = call i32 @print_opcode(i32 %68)
%70 = add i32 %58, 4
%71 = load i32, i32* %5
%72 = icmp slt i32 %70, %71
br i1 %72, label %57, label %73
73: ; preds = %57, %54
%74 = load i32, i32* %31
%75 = call i64 @munmap(i8* %37, i32 %74)
%76 = icmp eq i64 %75, 0
br i1 %76, label %80, label %77
77: ; preds = %73
%78 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %73
%81 = call i32 @close(i32 %25)
ret i32 0
}