#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 */
scalar_t__ EOF;
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*/ *);
scalar_t__ malloc(int);
char *strcat(char *, char *);
char *strcpy(char *, char *);
int strlen(char *);
int showFileHexData(char *filename) {
  FILE *file = NULL;
  FILE *output = NULL;
  char *outputname = (char *)malloc((strlen(filename) + 5) * sizeof(char));
  int bytesread = 0;
  unsigned char c;
  outputname = strcpy(outputname, filename);
  outputname = strcat(outputname, ".txt");
  output = fopen(outputname, "w");
  if ((file = fopen(filename, "rb")) == NULL)
    return 0;
  fprintf(output, "[%s]\n", filename);
  while (fread(&c, 1, 1, file) != 0) {
    fprintf(output, "[%.4d][%.4X] %.2X %.3u %c\n", bytesread, bytesread, c, c,
            c);
    bytesread++;
  }
  fprintf(output, "[%d]\n", bytesread);
  fclose(output);
  if (fclose(file) == EOF)
    return 0;
  return bytesread;
}


x86 assembly (clang, -O0):
<showFileHexData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x5,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1223 <showFileHexData+0xa3>
movl   $0x0,-0x4(%rbp)
jmp    12d7 <showFileHexData+0x157>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rcx
lea    -0x2d(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
je     128e <showFileHexData+0x10e>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x2c(%rbp),%ecx
movzbl -0x2d(%rbp),%r8d
movzbl -0x2d(%rbp),%r9d
movzbl -0x2d(%rbp),%eax
lea    0xd9a(%rip),%rsi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1239 <showFileHexData+0xb9>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
jne    12d1 <showFileHexData+0x151>
movl   $0x0,-0x4(%rbp)
jmp    12d7 <showFileHexData+0x157>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<showFileHexData>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
call   1060 <strlen@plt>
lea    0x5(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
movl   $0x7478742e,(%rbx,%rax,1)
movb   $0x0,0x4(%rbx,%rax,1)
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%r14
lea    0xe3e(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     129e <showFileHexData+0x12e>
mov    %rax,%rbx
lea    0xe26(%rip),%rsi
xor    %r13d,%r13d
mov    %r14,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0x17(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    $0x0,%ebp
test   %rax,%rax
je     126b <showFileHexData+0xfb>
lea    0xdf4(%rip),%r15
lea    0x17(%rsp),%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl 0x17(%rsp),%r8d
mov    %r8d,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %ebp,%ecx
mov    %r8d,%r9d
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x1,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    1230 <showFileHexData+0xc0>
lea    0xdb9(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
cmove  %r13d,%ebp
jmp    12a0 <showFileHexData+0x130>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c".txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@.str.4 = [27 x i8] c"[%.4d][%.4X] %.2X %.3u %c\0A\00"
@.str.5 = [6 x i8] c"[%d]\0A\00"
@EOF = global i64 0
define i32 @showFileHexData(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %3
store i32* null, i32** %4
store i32* null, i32** %5
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 5
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 1
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
store i32 0, i32* %7
%17 = load i8*, i8** %6
%18 = load i8*, i8** %3
%19 = call i8* @strcpy(i8* %17, i8* %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = call i8* @strcat(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %5
%24 = load i8*, i8** %3
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %4
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %28
27: ; preds = %1
store i32 0, i32* %2
br label %62
28: ; preds = %1
%29 = load i32*, i32** %5
%30 = load i8*, i8** %3
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %36, %28
%33 = load i32*, i32** %4
%34 = call i64 @fread(i8* %8, i32 1, i32 1, i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %49
36: ; preds = %32
%37 = load i32*, i32** %5
%38 = load i32, i32* %7
%39 = load i32, i32* %7
%40 = load i8, i8* %8
%41 = zext i8 %40 to i32
%42 = load i8, i8* %8
%43 = zext i8 %42 to i32
%44 = load i8, i8* %8
%45 = zext i8 %44 to i32
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %38, i32 %39, i32 %41, i32 %43, i32 %45)
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %32
49: ; preds = %32
%50 = load i32*, i32** %5
%51 = load i32, i32* %7
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%53 = load i32*, i32** %5
%54 = call i64 @fclose(i32* %53)
%55 = load i32*, i32** %4
%56 = call i64 @fclose(i32* %55)
%57 = load i64, i64* @EOF
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %49
store i32 0, i32* %2
br label %62
60: ; preds = %49
%61 = load i32, i32* %7
store i32 %61, i32* %2
br label %62
62: ; preds = %60, %59, %27
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@.str = [5 x i8] c".txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@.str.4 = [27 x i8] c"[%.4d][%.4X] %.2X %.3u %c\0A\00"
@.str.5 = [6 x i8] c"[%d]\0A\00"
@EOF = global i64 0
define i32 @showFileHexData(i8* %0) {
%2 = alloca i8
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 5
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i8* @strcpy(i8* dereferenceable(1) %6, i8* dereferenceable(1) %0)
%8 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %6)
%9 = getelementptr i8, i8* %6, i64 %8
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(5) %9, i8* align 1 dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 5, i1 false)
%10 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %33, label %13
13: ; preds = %1
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%15 = call i64 @fread(i8* %2, i32 1, i32 1, i32* %11)
%16 = icmp eq i64 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %13, %17
%18 = phi i32 [ %22, %17 ], [ 0, %13 ]
%19 = load i8, i8* %2
%20 = zext i8 %19 to i32
%21 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %18, i32 %18, i32 %20, i32 %20, i32 %20)
%22 = add nuw nsw i32 %18, 1
%23 = call i64 @fread(i8* %2, i32 1, i32 1, i32* %11)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %17
25: ; preds = %17, %13
%26 = phi i32 [ 0, %13 ], [ %22, %17 ]
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = tail call i64 @fclose(i32* %10)
%29 = tail call i64 @fclose(i32* %11)
%30 = load i64, i64* @EOF
%31 = icmp eq i64 %29, %30
%32 = select i1 %31, i32 0, i32 %26
br label %33
33: ; preds = %25, %1
%34 = phi i32 [ 0, %1 ], [ %32, %25 ]
ret i32 %34
}