#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 */
unsigned int EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
unsigned int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int,
                           unsigned int);
int /*<<< orphan*/ printf(char *, ...);
unsigned int strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  unsigned int address, c, val, cnt;
  FILE *fp_dmp = NULL, *fp_txt = NULL;
  if (argc < 4) {
    printf("usage: format_byte_dump <dump_file> <txt> <start_address>\n");
    exit(-1);
  }
  address = strtoul(argv[3], NULL, 0);
  fp_dmp = fopen(argv[1], "r");
  if (!fp_dmp) {
    printf("Unable to open %s for read\n", argv[1]);
    exit(-1);
  }
  fp_txt = fopen(argv[2], "w");
  if (!fp_dmp) {
    printf("Unable to open %s for write\n", argv[2]);
    fclose(fp_txt);
    exit(-1);
  }
  cnt = 4;
  val = 0;
  while ((c = fgetc(fp_dmp)) != EOF) {
    val >>= 8;
    val |= (c & 0xFF) << 24;
    if (--cnt == 0) {
      fprintf(fp_txt, "0x%08X:0x%08X\n", address, val);
      cnt = 4;
      val = 0;
      address += 4;
    }
  }
  fflush(fp_txt);
  fclose(fp_txt);
  fclose(fp_dmp);
}


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)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11c8 <main+0x48>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1221 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe29(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126d <main+0xed>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
movl   $0x4,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d5a(%rip),%rcx
cmp    (%rcx),%eax
je     12f4 <main+0x174>
mov    -0x1c(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
shl    $0x18,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    12ef <main+0x16f>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xda5(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x4,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x14(%rbp)
jmp    127b <main+0xfb>
mov    -0x30(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
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    $0x3,%edi
jle    126c <main+0xdc>
mov    %rsi,%r14
mov    0x18(%rsi),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %eax,%r13d
mov    0x8(%r14),%rdi
lea    0xe3e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1282 <main+0xf2>
mov    %rax,%rbx
mov    0x10(%r14),%rdi
lea    0xe40(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    0x2df3(%rip),%r12
cmp    (%r12),%eax
jne    1224 <main+0x94>
mov    %r14,%rdi
call   1080 <fflush@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
mov    $0x4,%ebp
jmp    1241 <main+0xb1>
xchg   %ax,%ax
mov    %eax,%r15d
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r12),%eax
je     11fb <main+0x6b>
shld   $0x18,%r15d,%eax
add    $0xffffffff,%ebp
jne    1230 <main+0xa0>
mov    %r14,%rdi
lea    0xdcb(%rip),%rsi
mov    %r13d,%edx
mov    %eax,%ecx
call   1070 <fprintf@plt>
add    $0x4,%r13d
mov    $0x4,%ebp
xor    %eax,%eax
jmp    1230 <main+0xa0>
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x8(%r14),%rsi
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [59 x i8] c"usage: format_byte_dump <dump_file> <txt> <start_address>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"Unable to open %s for read\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [29 x i8] c"Unable to open %s for write\0A\00"
@EOF = global i32 0
@.str.5 = [15 x i8] c"0x%08X:0x%08X\0A\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 i32*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %10
store i32* null, i32** %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 4
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 3
%20 = load i8*, i8** %19
%21 = call i32 @strtoul(i8* %20, i32* null, i32 0)
store i32 %21, i32* %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %10
%26 = load i32*, i32** %10
%27 = icmp ne i32* %26, null
br i1 %27, label %34, label %28
28: ; preds = %17
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %17
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %38, i32** %11
%39 = load i32*, i32** %10
%40 = icmp ne i32* %39, null
br i1 %40, label %49, label %41
41: ; preds = %34
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %44)
%46 = load i32*, i32** %11
%47 = call i32 @fclose(i32* %46)
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %34
store i32 4, i32* %9
store i32 0, i32* %8
br label %50
50: ; preds = %73, %49
%51 = load i32*, i32** %10
%52 = call i32 @fgetc(i32* %51)
store i32 %52, i32* %7
%53 = load i32, i32* @EOF
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %74
55: ; preds = %50
%56 = load i32, i32* %8
%57 = lshr i32 %56, 8
store i32 %57, i32* %8
%58 = load i32, i32* %7
%59 = and i32 %58, 255
%60 = shl i32 %59, 24
%61 = load i32, i32* %8
%62 = or i32 %61, %60
store i32 %62, i32* %8
%63 = load i32, i32* %9
%64 = add i32 %63, -1
store i32 %64, i32* %9
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %55
%67 = load i32*, i32** %11
%68 = load i32, i32* %6
%69 = load i32, i32* %8
%70 = call i32 @fprintf(i32* %67, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %68, i32 %69)
store i32 4, i32* %9
store i32 0, i32* %8
%71 = load i32, i32* %6
%72 = add i32 %71, 4
store i32 %72, i32* %6
br label %73
73: ; preds = %66, %55
br label %50
74: ; preds = %50
%75 = load i32*, i32** %11
%76 = call i32 @fflush(i32* %75)
%77 = load i32*, i32** %11
%78 = call i32 @fclose(i32* %77)
%79 = load i32*, i32** %10
%80 = call i32 @fclose(i32* %79)
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"Unable to open %s for read\0A\00"
@.str.3 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.5 = [15 x i8] c"0x%08X:0x%08X\0A\00"
@str = [58 x i8] c"usage: format_byte_dump <dump_file> <txt> <start_address>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 3
%9 = load i8*, i8** %8
%10 = tail call i32 @strtoul(i8* nocapture %9, i32* null, i32 0)
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %7
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %7
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @fgetc(i32* %13)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %44, label %26
26: ; preds = %19, %37
%27 = phi i32 [ %41, %37 ], [ %23, %19 ]
%28 = phi i32 [ %40, %37 ], [ 4, %19 ]
%29 = phi i32 [ %39, %37 ], [ 0, %19 ]
%30 = phi i32 [ %38, %37 ], [ %10, %19 ]
%31 = tail call i32 @llvm.fshl.i32(i32 %27, i32 %29, i32 24)
%32 = add i32 %28, -1
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = tail call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %30, i32 %31)
%36 = add i32 %30, 4
br label %37
37: ; preds = %34, %26
%38 = phi i32 [ %36, %34 ], [ %30, %26 ]
%39 = phi i32 [ 0, %34 ], [ %31, %26 ]
%40 = phi i32 [ 4, %34 ], [ %32, %26 ]
%41 = tail call i32 @fgetc(i32* %13)
%42 = load i32, i32* @EOF
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %26
44: ; preds = %37, %19
%45 = tail call i32 @fflush(i32* %22)
%46 = tail call i32 @fclose(i32* %22)
%47 = tail call i32 @fclose(i32* %13)
ret i32 0
}