#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 EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ stderr;
void make_hex(char *infile, char *outfile) {
  FILE *fi, *fo;
  int d, n;
  char data[8];
  fi = fopen(infile, "rb");
  if (fi == NULL) {
    fprintf(stderr, "Cannot open input file\n");
    exit(EXIT_FAILURE);
  }
  fo = fopen(outfile, "wt");
  if (fo == NULL) {
    fprintf(stderr, "Cannot open output file\n");
    exit(EXIT_FAILURE);
  }
  n = 0;
  do {
    d = fgetc(fi);
    if (d == EOF)
      break;
    sprintf(data, "%02x", (unsigned char)d);
    fputc(data[0], fo);
    fputc(data[1], fo);
    n++;
  } while (d != EOF);
  fprintf(stderr, "%d bytes read and converted\n", n);
  fclose(fi);
  fclose(fo);
}


x86 assembly (clang, -O0):
<make_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c4 <make_hex+0x54>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1208 <make_hex+0x98>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%eax
jne    1232 <make_hex+0xc2>
jmp    1281 <make_hex+0x111>
lea    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%eax
lea    0xdf7(%rip),%rsi
movzbl %al,%edx
call   1080 <sprintf@plt>
mov    -0x30(%rbp),%al
mov    -0x20(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
mov    -0x2f(%rbp),%al
mov    -0x20(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%eax
jne    120f <make_hex+0x9f>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1239 <make_hex+0xc9>
mov    %rax,%r14
lea    0xe7e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     124b <make_hex+0xdb>
mov    %rax,%r13
xor    %ebp,%ebp
mov    0x2e2e(%rip),%r15
mov    %rsp,%r12
nopl   (%rax)
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%eax
je     1202 <make_hex+0x92>
mov    %eax,%ebx
movzbl %bl,%edx
mov    %r12,%rdi
lea    0xe5b(%rip),%rsi
call   1080 <sprintf@plt>
movsbl (%rsp),%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
movsbl 0x1(%rsp),%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
add    $0x1,%ebp
cmp    (%r15),%ebx
jne    11c0 <make_hex+0x50>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
jmp    125b <make_hex+0xeb>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Cannot open input file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"wt\00"
@.str.3 = [25 x i8] c"Cannot open output file\0A\00"
@EOF = global i32 0
@.str.4 = [5 x i8] c"%02x\00"
@.str.5 = [29 x i8] c"%d bytes read and converted\0A\00"
define void @make_hex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [8 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
store i32 0, i32* %8
br label %30
30: ; preds = %52, %29
%31 = load i32*, i32** %5
%32 = call i32 @fgetc(i32* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %30
br label %56
37: ; preds = %30
%38 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%39 = load i32, i32* %7
%40 = trunc i32 %39 to i8
%41 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %40)
%42 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%43 = load i8, i8* %42
%44 = load i32*, i32** %6
%45 = call i32 @fputc(i8 signext %43, i32* %44)
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 1
%47 = load i8, i8* %46
%48 = load i32*, i32** %6
%49 = call i32 @fputc(i8 signext %47, i32* %48)
%50 = load i32, i32* %8
%51 = add nsw i32 %50, 1
store i32 %51, i32* %8
br label %52
52: ; preds = %37
%53 = load i32, i32* %7
%54 = load i32, i32* @EOF
%55 = icmp ne i32 %53, %54
br i1 %55, label %30, label %56
56: ; preds = %52, %36
%57 = load i32, i32* @stderr
%58 = load i32, i32* %8
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %58)
%60 = load i32*, i32** %5
%61 = call i32 @fclose(i32* %60)
%62 = load i32*, i32** %6
%63 = call i32 @fclose(i32* %62)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Cannot open input file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"wt\00"
@.str.3 = [25 x i8] c"Cannot open output file\0A\00"
@EOF = global i32 0
@.str.4 = [5 x i8] c"%02x\00"
@.str.5 = [29 x i8] c"%d bytes read and converted\0A\00"
define void @make_hex(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
br label %22
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %15, %27
%23 = phi i32 [ %34, %27 ], [ 0, %15 ]
%24 = tail call i32 @fgetc(i32* %5)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %37, label %27
27: ; preds = %22
%28 = trunc i32 %24 to i8
%29 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %28)
%30 = load i8, i8* %4
%31 = tail call i32 @fputc(i8 signext %30, i32* %13)
%32 = load i8, i8* %16
%33 = tail call i32 @fputc(i8 signext %32, i32* %13)
%34 = add nuw nsw i32 %23, 1
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %24, %35
br i1 %36, label %37, label %22
37: ; preds = %22, %27
%38 = phi i32 [ %23, %22 ], [ %34, %27 ]
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%41 = tail call i32 @fclose(i32* %5)
%42 = tail call i32 @fclose(i32* %13)
ret void
}