#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 atoi(char *);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
scalar_t__ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *stderr;
int main(int argc, char *argv[]) {
  int i;
  int pad;
  FILE *fdi, *fdo;
  unsigned char w[4];
  if (argc != 4) {
    fprintf(stderr, "Usage: bin2hex <infile> <outfile> <size>\n");
    return 1;
  }
  pad = atoi(argv[3]);
  if (pad <= 0) {
    fprintf(stderr, "Incorrect size\n");
    return 1;
  }
  fdi = fopen(argv[1], "rb");
  if (!fdi) {
    perror("Unable to open input file");
    return 1;
  }
  fdo = fopen(argv[2], "w");
  if (!fdo) {
    perror("Unable to open output file");
    fclose(fdi);
    return 1;
  }
  while (1) {
    if (fread(w, 4, 1, fdi) <= 0)
      break;
    fprintf(fdo, "%02hhx%02hhx%02hhx%02hhx\n", w[0], w[1], w[2], w[3]);
    pad--;
  }
  fclose(fdi);
  if (pad < 0)
    fprintf(stderr, "Warning: Input binary is larger than specified size\n");
  for (i = 0; i < pad; i++)
    fprintf(fdo, "00000000\n");
  if (fclose(fdo) != 0) {
    perror("Unable to close output file");
    return 1;
  }
  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   $0x4,-0x8(%rbp)
je     11a4 <main+0x44>
mov    0x2e59(%rip),%rax
mov    (%rax),%rdi
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x1ee>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11e2 <main+0x82>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rdi
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x1ee>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe49(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121d <main+0xbd>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x1ee>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe2b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1261 <main+0x101>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x1ee>
jmp    1266 <main+0x106>
lea    -0x2c(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
jg     128c <main+0x12c>
jmp    12be <main+0x15e>
mov    -0x28(%rbp),%rdi
movzbl -0x2c(%rbp),%edx
movzbl -0x2b(%rbp),%ecx
movzbl -0x2a(%rbp),%r8d
movzbl -0x29(%rbp),%r9d
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1266 <main+0x106>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x18(%rbp)
jge    12e9 <main+0x189>
mov    0x2d08(%rip),%rax
mov    (%rax),%rdi
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    131c <main+0x1bc>
mov    -0x28(%rbp),%rdi
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12f0 <main+0x190>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1347 <main+0x1e7>
lea    0xd97(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x1ee>
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   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    126c <main+0xfc>
mov    %rsi,%rbx
mov    0x18(%rsi),%rdi
call   1080 <atoi@plt>
test   %eax,%eax
jle    1284 <main+0x114>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     131d <main+0x1ad>
mov    %rax,%r13
mov    0x10(%rbx),%rdi
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1326 <main+0x1b6>
mov    %rax,%r14
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jle    12ab <main+0x13b>
mov    %ebp,%ebx
lea    0xe79(%rip),%r15
lea    0x4(%rsp),%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x4(%rsp),%edx
movzbl 0x5(%rsp),%ecx
movzbl 0x6(%rsp),%r8d
movzbl 0x7(%rsp),%r9d
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
add    $0x1,%ebp
test   %rax,%rax
jg     1210 <main+0xa0>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %ebx,%ecx
sub    %ebp,%ebx
lea    0x1(%rbx),%eax
test   %eax,%eax
jle    12dc <main+0x16c>
cmp    %ebp,%ecx
jne    12b5 <main+0x145>
jmp    12fc <main+0x18c>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rcx
lea    0xd83(%rip),%rdi
mov    $0x29,%esi
jmp    129a <main+0x12a>
mov    0x2d55(%rip),%rax
mov    (%rax),%rcx
lea    0xd95(%rip),%rdi
mov    $0xf,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    $0x1,%eax
jmp    130e <main+0x19e>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %ebp,%ebx
lea    0xe07(%rip),%rbp
nopl   0x0(%rax)
mov    $0x9,%esi
mov    $0x1,%edx
mov    %rbp,%rdi
mov    %r14,%rcx
call   1090 <fwrite@plt>
add    $0xffffffff,%ebx
jne    12c0 <main+0x150>
jmp    12fc <main+0x18c>
mov    0x2cfd(%rip),%rax
mov    (%rax),%rcx
lea    0xda1(%rip),%rdi
mov    $0x34,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    133f <main+0x1cf>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd19(%rip),%rdi
jmp    1346 <main+0x1d6>
lea    0xd2c(%rip),%rdi
call   1070 <perror@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
jmp    12a4 <main+0x134>
lea    0xd87(%rip),%rdi
call   1070 <perror@plt>
jmp    12a4 <main+0x134>

LLVM IR (-O0):
@stderr = global i32* null
@.str = [42 x i8] c"Usage: bin2hex <infile> <outfile> <size>\0A\00"
@.str.1 = [16 x i8] c"Incorrect size\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [26 x i8] c"Unable to open input file\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [27 x i8] c"Unable to open output file\00"
@.str.6 = [26 x i8] c"%02hhx%02hhx%02hhx%02hhx\0A\00"
@.str.7 = [53 x i8] c"Warning: Input binary is larger than specified size\0A\00"
@.str.8 = [10 x i8] c"00000000\0A\00"
@.str.9 = [28 x i8] c"Unable to close output file\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 i32*
%9 = alloca i32*
%10 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32*, i32** @stderr
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %96
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 3
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32*, i32** @stderr
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %96
26: ; preds = %16
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %8
%31 = load i32*, i32** %8
%32 = icmp ne i32* %31, null
br i1 %32, label %35, label %33
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %96
35: ; preds = %26
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %39, i32** %9
%40 = load i32*, i32** %9
%41 = icmp ne i32* %40, null
br i1 %41, label %46, label %42
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%44 = load i32*, i32** %8
%45 = call i64 @fclose(i32* %44)
store i32 1, i32* %3
br label %96
46: ; preds = %35
br label %47
47: ; preds = %46, %53
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%49 = load i32*, i32** %8
%50 = call i64 @fread(i8* %48, i32 4, i32 1, i32* %49)
%51 = icmp sle i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
br label %70
53: ; preds = %47
%54 = load i32*, i32** %9
%55 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 3
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = call i32 (i32*, i8*, ...) @fprintf(i32* %54, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %57, i32 %60, i32 %63, i32 %66)
%68 = load i32, i32* %7
%69 = add nsw i32 %68, -1
store i32 %69, i32* %7
br label %47
70: ; preds = %52
%71 = load i32*, i32** %8
%72 = call i64 @fclose(i32* %71)
%73 = load i32, i32* %7
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %70
%76 = load i32*, i32** @stderr
%77 = call i32 (i32*, i8*, ...) @fprintf(i32* %76, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
br label %78
78: ; preds = %75, %70
store i32 0, i32* %6
br label %79
79: ; preds = %86, %78
%80 = load i32, i32* %6
%81 = load i32, i32* %7
%82 = icmp slt i32 %80, %81
br i1 %82, label %83, label %89
83: ; preds = %79
%84 = load i32*, i32** %9
%85 = call i32 (i32*, i8*, ...) @fprintf(i32* %84, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %86
86: ; preds = %83
%87 = load i32, i32* %6
%88 = add nsw i32 %87, 1
store i32 %88, i32* %6
br label %79
89: ; preds = %79
%90 = load i32*, i32** %9
%91 = call i64 @fclose(i32* %90)
%92 = icmp ne i64 %91, 0
br i1 %92, label %93, label %95
93: ; preds = %89
%94 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %96
95: ; preds = %89
store i32 0, i32* %3
br label %96
96: ; preds = %95, %93, %42, %33, %23, %13
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [42 x i8] c"Usage: bin2hex <infile> <outfile> <size>\0A\00"
@.str.1 = [16 x i8] c"Incorrect size\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [26 x i8] c"Unable to open input file\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [27 x i8] c"Unable to open output file\00"
@.str.6 = [26 x i8] c"%02hhx%02hhx%02hhx%02hhx\0A\00"
@.str.7 = [53 x i8] c"Warning: Input binary is larger than specified size\0A\00"
@.str.8 = [10 x i8] c"00000000\0A\00"
@.str.9 = [28 x i8] c"Unable to close output file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 4
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32*, i32** @stderr
%8 = tail call i64 @fwrite(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i64 41, i64 1, i32* %7)
br label %75
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 3
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32*, i32** @stderr
%16 = tail call i64 @fwrite(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 15, i64 1, i32* %15)
br label %75
17: ; preds = %9
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %75
24: ; preds = %17
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %38, label %29
29: ; preds = %24
%30 = call i64 @fread(i8* %4, i32 4, i32 1, i32* %20)
%31 = icmp slt i64 %30, 1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i64 @fclose(i32* %20)
br label %63
34: ; preds = %29
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
br label %41
38: ; preds = %24
%39 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%40 = tail call i64 @fclose(i32* %20)
br label %75
41: ; preds = %34, %41
%42 = phi i32 [ %12, %34 ], [ %52, %41 ]
%43 = load i8, i8* %4
%44 = zext i8 %43 to i32
%45 = load i8, i8* %35
%46 = zext i8 %45 to i32
%47 = load i8, i8* %36
%48 = zext i8 %47 to i32
%49 = load i8, i8* %37
%50 = zext i8 %49 to i32
%51 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %44, i32 %46, i32 %48, i32 %50)
%52 = add nsw i32 %42, -1
%53 = call i64 @fread(i8* %4, i32 4, i32 1, i32* %20)
%54 = icmp slt i64 %53, 1
br i1 %54, label %55, label %41
55: ; preds = %41
%56 = tail call i64 @fclose(i32* %20)
%57 = icmp slt i32 %42, 1
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i32*, i32** @stderr
%60 = tail call i64 @fwrite(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0), i64 52, i64 1, i32* %59)
br label %70
61: ; preds = %55
%62 = icmp eq i32 %52, 0
br i1 %62, label %70, label %63
63: ; preds = %32, %61
%64 = phi i32 [ %12, %32 ], [ %52, %61 ]
br label %65
65: ; preds = %63, %65
%66 = phi i32 [ %68, %65 ], [ 0, %63 ]
%67 = tail call i64 @fwrite(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i64 9, i64 1, i32* %27)
%68 = add nuw nsw i32 %66, 1
%69 = icmp eq i32 %68, %64
br i1 %69, label %70, label %65
70: ; preds = %65, %58, %61
%71 = tail call i64 @fclose(i32* %27)
%72 = icmp eq i64 %71, 0
br i1 %72, label %75, label %73
73: ; preds = %70
%74 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %75
75: ; preds = %70, %73, %38, %22, %14, %6
%76 = phi i32 [ 1, %6 ], [ 1, %14 ], [ 1, %73 ], [ 1, %38 ], [ 1, %22 ], [ 0, %70 ]
ret i32 %76
}