#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int loadhex(int /*<<< orphan*/ *, unsigned char *, char *);
int /*<<< orphan*/ perror(char *);
int hex2hex(char *source, char *target, char *varline) {
  FILE *inf, *outf;
  int i, l;
  unsigned char buf[(256 * 1024)];
  if ((inf = fopen(source, "r")) == NULL) {
    perror(source);
    return 0;
  }
  if ((outf = fopen(target, "w")) == NULL) {
    perror(target);
    fclose(inf);
    return 0;
  }
  l = loadhex(inf, buf, source);
  if (l <= 0) {
    fclose(inf);
    fclose(outf);
    return l;
  }
  fprintf(outf, "/*\n *\t Computer generated file. Do not edit.\n */\n");
  fprintf(outf, "static int %s_len = %d;\n", varline, l);
  fprintf(outf, "static unsigned char %s[] = {\n", varline);
  for (i = 0; i < l; i++) {
    if (i)
      fprintf(outf, ",");
    if (i && !(i % 16))
      fprintf(outf, "\n");
    fprintf(outf, "0x%02x", buf[i]);
  }
  fprintf(outf, "\n};\n\n");
  fclose(inf);
  fclose(outf);
  return 1;
}


x86 assembly (clang, -O0):
<hex2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    119a <hex2hex+0x4a>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130b <hex2hex+0x1bb>
mov    -0x18(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11d6 <hex2hex+0x86>
mov    -0x18(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    130b <hex2hex+0x1bb>
mov    -0x28(%rbp),%rdi
lea    -0x40040(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <loadhex@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jg     1214 <hex2hex+0xc4>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130b <hex2hex+0x1bb>
mov    -0x30(%rbp),%rdi
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%ecx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    12e0 <hex2hex+0x190>
cmpl   $0x0,-0x34(%rbp)
je     1284 <hex2hex+0x134>
mov    -0x30(%rbp),%rdi
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x34(%rbp)
je     12b4 <hex2hex+0x164>
mov    -0x34(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12b4 <hex2hex+0x164>
mov    -0x30(%rbp),%rdi
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
movslq -0x34(%rbp),%rax
movzbl -0x40040(%rbp,%rax,1),%edx
ff
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    125c <hex2hex+0x10c>
mov    -0x30(%rbp),%rdi
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40000,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbp
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12e0 <hex2hex+0x170>
mov    %rax,%r14
lea    0xe5b(%rip),%rsi
mov    %r12,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     12ed <hex2hex+0x17d>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %r14,%rdi
mov    %rbp,%rdx
call   1030 <loadhex@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    1269 <hex2hex+0xf9>
lea    0xe29(%rip),%rdi
mov    $0x1,%ebp
mov    $0x31,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
lea    0xe3d(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
movzbl (%rsp),%edx
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x1,%r12d
jne    128c <hex2hex+0x11c>
lea    0xe36(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%r12d
jmp    1279 <hex2hex+0x109>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12d,%eax
add    $0x40000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%r12d
lea    0xdd8(%rip),%r15
jmp    12be <hex2hex+0x14e>
nopl   0x0(%rax,%rax,1)
movzbl (%rsp,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
je     1238 <hex2hex+0xc8>
mov    $0x2c,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
test   $0xf,%bpl
jne    12a0 <hex2hex+0x130>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
jmp    12a0 <hex2hex+0x130>
mov    %rbp,%rdi
call   1080 <perror@plt>
xor    %r12d,%r12d
jmp    1279 <hex2hex+0x109>
mov    %r12,%rdi
call   1080 <perror@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %r12d,%r12d
jmp    1279 <hex2hex+0x109>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [50 x i8] c"/*\0A *\09 Computer generated file. Do not edit.\0A */\0A\00"
@.str.3 = [25 x i8] c"static int %s_len = %d;\0A\00"
@.str.4 = [31 x i8] c"static unsigned char %s[] = {\0A\00"
@.str.5 = [2 x i8] c",\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [7 x i8] c"0x%02x\00"
@.str.8 = [6 x i8] c"\0A};\0A\0A\00"
define i32 @hex2hex(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [262144 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 @perror(i8* %17)
store i32 0, i32* %4
br label %89
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %9
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = call i32 @perror(i8* %24)
%26 = load i32*, i32** %8
%27 = call i32 @fclose(i32* %26)
store i32 0, i32* %4
br label %89
28: ; preds = %19
%29 = load i32*, i32** %8
%30 = getelementptr inbounds [262144 x i8], [262144 x i8]* %12, i64 0, i64 0
%31 = load i8*, i8** %5
%32 = call i32 @loadhex(i32* %29, i8* %30, i8* %31)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %28
%36 = load i32*, i32** %8
%37 = call i32 @fclose(i32* %36)
%38 = load i32*, i32** %9
%39 = call i32 @fclose(i32* %38)
%40 = load i32, i32* %11
store i32 %40, i32* %4
br label %89
41: ; preds = %28
%42 = load i32*, i32** %9
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32*, i32** %9
%45 = load i8*, i8** %7
%46 = load i32, i32* %11
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %45, i32 %46)
%48 = load i32*, i32** %9
%49 = load i8*, i8** %7
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %49)
store i32 0, i32* %10
br label %51
51: ; preds = %79, %41
%52 = load i32, i32* %10
%53 = load i32, i32* %11
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %82
55: ; preds = %51
%56 = load i32, i32* %10
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i32*, i32** %9
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %58, %55
%62 = load i32, i32* %10
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %71
64: ; preds = %61
%65 = load i32, i32* %10
%66 = srem i32 %65, 16
%67 = icmp ne i32 %66, 0
br i1 %67, label %71, label %68
68: ; preds = %64
%69 = load i32*, i32** %9
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %71
71: ; preds = %68, %64, %61
%72 = load i32*, i32** %9
%73 = load i32, i32* %10
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [262144 x i8], [262144 x i8]* %12, i64 0, i64 %74
%76 = load i8, i8* %75
%77 = zext i8 %76 to i32
%78 = call i32 (i32*, i8*, ...) @fprintf(i32* %72, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %71
%80 = load i32, i32* %10
%81 = add nsw i32 %80, 1
store i32 %81, i32* %10
br label %51
82: ; preds = %51
%83 = load i32*, i32** %9
%84 = call i32 (i32*, i8*, ...) @fprintf(i32* %83, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%85 = load i32*, i32** %8
%86 = call i32 @fclose(i32* %85)
%87 = load i32*, i32** %9
%88 = call i32 @fclose(i32* %87)
store i32 1, i32* %4
br label %89
89: ; preds = %82, %35, %23, %16
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [50 x i8] c"/*\0A *\09 Computer generated file. Do not edit.\0A */\0A\00"
@.str.3 = [25 x i8] c"static int %s_len = %d;\0A\00"
@.str.4 = [31 x i8] c"static unsigned char %s[] = {\0A\00"
@.str.7 = [7 x i8] c"0x%02x\00"
@.str.8 = [6 x i8] c"\0A};\0A\0A\00"
define i32 @hex2hex(i8* %0, i8* nocapture %1, i8* %2) {
%4 = alloca [262144 x i8]
%5 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* %0)
br label %49
10: ; preds = %3
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* %1)
%15 = tail call i32 @fclose(i32* %6)
br label %49
16: ; preds = %10
%17 = call i32 @loadhex(i32* %6, i8* %5, i8* %0)
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @fclose(i32* %6)
%21 = call i32 @fclose(i32* %11)
br label %49
22: ; preds = %16
%23 = call i64 @fwrite(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i64 49, i64 1, i32* %11)
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %2, i32 %17)
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%26 = zext i32 %17 to i64
%27 = load i8, i8* %5
%28 = zext i8 %27 to i32
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %28)
%30 = icmp eq i32 %17, 1
br i1 %30, label %45, label %31
31: ; preds = %22, %38
%32 = phi i64 [ %43, %38 ], [ 1, %22 ]
%33 = call i32 @fputc(i32 44, i32* %11)
%34 = and i64 %32, 15
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @fputc(i32 10, i32* %11)
br label %38
38: ; preds = %36, %31
%39 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 %32
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %41)
%43 = add nuw nsw i64 %32, 1
%44 = icmp eq i64 %43, %26
br i1 %44, label %45, label %31
45: ; preds = %38, %22
%46 = call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i64 5, i64 1, i32* %11)
%47 = call i32 @fclose(i32* %6)
%48 = call i32 @fclose(i32* %11)
br label %49
49: ; preds = %45, %19, %13, %8
%50 = phi i32 [ 0, %8 ], [ 0, %13 ], [ %17, %19 ], [ 1, %45 ]
ret i32 %50
}