#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 uint8_t;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ bitconvert(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getopt(int, char **, char *);
int open(char *, int, ...);
int optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, int **, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int emit_binary = 0;
  uint8_t buffer[4096];
  char out_buffer[80];
  ssize_t bytes_read;
  int offset;
  int fd_in, fd_out;
  int option;
  while ((option = getopt(argc, argv, "b")) != -1) {
    switch (option) {
    case 'b':
      emit_binary = 1;
      break;
    default:
      printf("Usage: bin2mif [-b] infile outfile\n\n");
      exit(EXIT_FAILURE);
    }
  }
  if (optind + 2 != argc) {
    fprintf(stderr, "Input and output file name must be specified\n");
    exit(EXIT_FAILURE);
  }
  fd_in = open(argv[optind], O_RDONLY);
  if (fd_in < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  fd_out = open(argv[optind + 1], O_WRONLY | O_CREAT, 0666);
  if (fd_out < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  while ((bytes_read = read(fd_in, &buffer, 4096)) > 0) {
    offset = 0;
    while (offset < bytes_read) {
      if (emit_binary) {
        bitconvert(out_buffer, buffer[offset]);
      } else {
        sprintf(out_buffer, "%02X\n", buffer[offset]);
      }
      offset++;
      write(fd_out, out_buffer, strlen(out_buffer));
    }
  }
  if (bytes_read == -1) {
    perror("read");
  }
  close(fd_in);
  close(fd_out);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe12(%rip),%rdx
call   10b0 <getopt@plt>
mov    %eax,-0x4084(%rbp)
cmp    $0xffffffff,%eax
je     1241 <main+0x81>
mov    -0x4084(%rbp),%eax
sub    $0x62,%eax
jne    1222 <main+0x62>
jmp    1216 <main+0x56>
movl   $0x1,-0x14(%rbp)
jmp    123c <main+0x7c>
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    11e0 <main+0x20>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
add    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     1279 <main+0xb9>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2d54(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x407c(%rbp)
cmpl   $0x0,-0x407c(%rbp)
jge    12c8 <main+0x108>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2d05(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
mov    0x2d09(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x4080(%rbp)
cmpl   $0x0,-0x4080(%rbp)
jge    132a <main+0x16a>
lea    0xd3e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    132f <main+0x16f>
mov    -0x407c(%rbp),%edi
lea    -0x4020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x4074(%rbp)
cmp    $0x0,%eax
jle    140e <main+0x24e>
movl   $0x0,-0x4078(%rbp)
mov    -0x4078(%rbp),%eax
cmp    -0x4074(%rbp),%eax
jge    1409 <main+0x249>
cmpl   $0x0,-0x14(%rbp)
je     139a <main+0x1da>
lea    -0x4070(%rbp),%rdi
movslq -0x4078(%rbp),%rax
mov    -0x4020(%rbp,%rax,4),%esi
call   10c0 <bitconvert@plt>
jmp    13bb <main+0x1fb>
lea    -0x4070(%rbp),%rdi
movslq -0x4078(%rbp),%rax
mov    -0x4020(%rbp,%rax,4),%edx
lea    0xca4(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x4078(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4078(%rbp)
mov    -0x4080(%rbp),%eax
mov    %eax,-0x4094(%rbp)
lea    -0x4070(%rbp),%rax
mov    %rax,-0x4090(%rbp)
lea    -0x4070(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4094(%rbp),%edi
mov    -0x4090(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    135f <main+0x19f>
jmp    132f <main+0x16f>
cmpl   $0xffffffff,-0x4074(%rbp)
jne    1427 <main+0x267>
lea    0xc3e(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x407c(%rbp),%edi
call   1060 <close@plt>
mov    -0x4080(%rbp),%edi
call   1060 <close@plt>
mov    0x2b74(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4068,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1,%cl
lea    0xe21(%rip),%r14
nop
mov    %ecx,%r13d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10b0 <getopt@plt>
xor    %ecx,%ecx
cmp    $0x62,%eax
je     11e0 <main+0x20>
cmp    $0xffffffff,%eax
jne    1260 <main+0xa0>
mov    0x2dd5(%rip),%r14
movslq (%r14),%rax
lea    0x2(%rax),%ecx
cmp    %ebp,%ecx
jne    126e <main+0xae>
mov    (%rbx,%rax,8),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1252 <main+0x92>
mov    %eax,%r12d
movslq (%r14),%rax
mov    0x8(%rbx,%rax,8),%rdi
mov    0x2d71(%rip),%rax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
jns    1291 <main+0xd1>
lea    0xdd7(%rip),%rdi
call   10a0 <perror@plt>
jmp    1283 <main+0xc3>
lea    0xdd9(%rip),%rdi
call   1030 <puts@plt>
jmp    1283 <main+0xc3>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    %eax,%ebp
lea    0x60(%rsp),%rsi
mov    %r12d,%edi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    132c <main+0x16c>
lea    0xd81(%rip),%r15
lea    0x10(%rsp),%rbx
mov    %r12d,0xc(%rsp)
jmp    12db <main+0x11b>
mov    0xc(%rsp),%r12d
mov    %r12d,%edi
lea    0x60(%rsp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    132c <main+0x16c>
mov    %eax,%r12d
xor    %r14d,%r14d
jmp    1315 <main+0x155>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rsi
call   10d0 <sprintf@plt>
add    $0x1,%r14
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %r14,%r12
je     12c0 <main+0x100>
mov    0x60(%rsp,%r14,4),%edx
mov    %rbx,%rdi
test   $0x1,%r13b
jne    12f0 <main+0x130>
mov    %edx,%esi
call   10c0 <bitconvert@plt>
jmp    12f8 <main+0x138>
cmp    $0xffffffff,%eax
jne    133d <main+0x17d>
lea    0xd03(%rip),%rdi
call   10a0 <perror@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"b\00"
@.str.1 = [37 x i8] c"Usage: bin2mif [-b] infile outfile\0A\0A\00"
@EXIT_FAILURE = global i32 0
@optind = global i32 0
@stderr = global i32 0
@.str.2 = [46 x i8] c"Input and output file name must be specified\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.4 = [6 x i8] c"%02X\0A\00"
@.str.5 = [5 x i8] c"read\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i32]
%8 = alloca [80 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %14
14: ; preds = %26, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call i32 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %13
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load i32, i32* %13
switch i32 %20, label %22 [
i32 98, label %21
]
21: ; preds = %19
store i32 1, i32* %6
br label %26
22: ; preds = %19
%23 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %21
br label %14
27: ; preds = %14
%28 = load i32, i32* @optind
%29 = add nsw i32 %28, 2
%30 = load i32, i32* %4
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %27
%38 = load i8**, i8*** %5
%39 = load i32, i32* @optind
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = load i32, i32* @O_RDONLY
%44 = call i32 (i8*, i32, ...) @open(i8* %42, i32 %43)
store i32 %44, i32* %11
%45 = load i32, i32* %11
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %37
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %37
%52 = load i8**, i8*** %5
%53 = load i32, i32* @optind
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %52, i64 %55
%57 = load i8*, i8** %56
%58 = load i32, i32* @O_WRONLY
%59 = load i32, i32* @O_CREAT
%60 = or i32 %58, %59
%61 = call i32 (i8*, i32, ...) @open(i8* %57, i32 %60, i32 438)
store i32 %61, i32* %12
%62 = load i32, i32* %12
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %51
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %51
br label %69
69: ; preds = %104, %68
%70 = load i32, i32* %11
%71 = bitcast [4096 x i32]* %7 to i32**
%72 = call i32 @read(i32 %70, i32** %71, i32 4096)
store i32 %72, i32* %9
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %105
74: ; preds = %69
store i32 0, i32* %10
br label %75
75: ; preds = %96, %74
%76 = load i32, i32* %10
%77 = load i32, i32* %9
%78 = icmp slt i32 %76, %77
br i1 %78, label %79, label %104
79: ; preds = %75
%80 = load i32, i32* %6
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %89
82: ; preds = %79
%83 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%84 = load i32, i32* %10
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [4096 x i32], [4096 x i32]* %7, i64 0, i64 %85
%87 = load i32, i32* %86
%88 = call i32 @bitconvert(i8* %83, i32 %87)
br label %96
89: ; preds = %79
%90 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%91 = load i32, i32* %10
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [4096 x i32], [4096 x i32]* %7, i64 0, i64 %92
%94 = load i32, i32* %93
%95 = call i32 @sprintf(i8* %90, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %94)
br label %96
96: ; preds = %89, %82
%97 = load i32, i32* %10
%98 = add nsw i32 %97, 1
store i32 %98, i32* %10
%99 = load i32, i32* %12
%100 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%101 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%102 = call i32 @strlen(i8* %101)
%103 = call i32 @write(i32 %99, i8* %100, i32 %102)
br label %75
104: ; preds = %75
br label %69
105: ; preds = %69
%106 = load i32, i32* %9
%107 = icmp eq i32 %106, -1
br i1 %107, label %108, label %110
108: ; preds = %105
%109 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %110
110: ; preds = %108, %105
%111 = load i32, i32* %11
%112 = call i32 @close(i32 %111)
%113 = load i32, i32* %12
%114 = call i32 @close(i32 %113)
%115 = load i32, i32* @EXIT_SUCCESS
%116 = call i32 @exit(i32 %115)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c"b\00"
@EXIT_FAILURE = global i32 0
@optind = global i32 0
@stderr = global i32 0
@.str.2 = [46 x i8] c"Input and output file name must be specified\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.4 = [6 x i8] c"%02X\0A\00"
@.str.5 = [5 x i8] c"read\00"
@EXIT_SUCCESS = global i32 0
@str = [36 x i8] c"Usage: bin2mif [-b] infile outfile\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [4096 x i32]
%4 = alloca [80 x i8]
%5 = bitcast [4096 x i32]* %3 to i8*
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
br label %7
7: ; preds = %7, %2
%8 = phi i1 [ true, %2 ], [ false, %7 ]
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %9, label %10 [
i32 -1, label %14
i32 98, label %7
]
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %7
%15 = load i32, i32* @optind
%16 = add nsw i32 %15, 2
%17 = icmp eq i32 %16, %0
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = sext i32 %15 to i64
%25 = getelementptr inbounds i8*, i8** %1, i64 %24
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 (i8*, i32, ...) @open(i8* %26, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %23
%35 = load i32, i32* @optind
%36 = add nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %1, i64 %37
%39 = load i8*, i8** %38
%40 = load i32, i32* @O_WRONLY
%41 = load i32, i32* @O_CREAT
%42 = or i32 %41, %40
%43 = tail call i32 (i8*, i32, ...) @open(i8* %39, i32 %42, i32 438)
%44 = icmp slt i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %34
%46 = bitcast [4096 x i32]* %3 to i32**
%47 = call i32 @read(i32 %28, i32** %46, i32 4096)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %56, label %72
49: ; preds = %34
%50 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %67
%54 = call i32 @read(i32 %28, i32** %46, i32 4096)
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %72
56: ; preds = %45, %53
%57 = phi i32 [ %54, %53 ], [ %47, %45 ]
%58 = zext i32 %57 to i64
br label %59
59: ; preds = %56, %67
%60 = phi i64 [ 0, %56 ], [ %68, %67 ]
%61 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 %60
%62 = load i32, i32* %61
br i1 %8, label %65, label %63
63: ; preds = %59
%64 = call i32 @bitconvert(i8* %6, i32 %62)
br label %67
65: ; preds = %59
%66 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %62)
br label %67
67: ; preds = %65, %63
%68 = add nuw nsw i64 %60, 1
%69 = call i32 @strlen(i8* %6)
%70 = call i32 @write(i32 %43, i8* %6, i32 %69)
%71 = icmp eq i64 %68, %58
br i1 %71, label %53, label %59
72: ; preds = %53, %45
%73 = phi i32 [ %47, %45 ], [ %54, %53 ]
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %77
77: ; preds = %75, %72
%78 = call i32 @close(i32 %28)
%79 = call i32 @close(i32 %43)
%80 = load i32, i32* @EXIT_SUCCESS
%81 = call i32 @exit(i32 %80)
unreachable
}