#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*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int bin2hex(char *path, char *target, char *varname) {
  int fd;
  int count;
  char c;
  int i = 0;
  if ((fd = open(path, 0)) > 0) {
    FILE *sf = fopen(target, "w");
    fprintf(sf, "/* automatically generated by configure */\n");
    fprintf(sf, "static unsigned char %s[] = {\n", varname);
    while (1) {
      count = read(fd, &c, 1);
      if (count == 0)
        break;
      if (i != 0 && (i % 10) == 0)
        fprintf(sf, "\n");
      fprintf(sf, "0x%02lx,", c & 0xFFL);
      i++;
    }
    fprintf(sf,
            "};\n"
            "#define %sLen %d\n",
            varname, i);
    fclose(sf);
    close(fd);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1283 <bin2hex+0x123>
mov    -0x18(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11f1 <bin2hex+0x91>
jmp    124d <bin2hex+0xed>
cmpl   $0x0,-0x30(%rbp)
je     1221 <bin2hex+0xc1>
mov    -0x30(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1221 <bin2hex+0xc1>
mov    -0x38(%rbp),%rdi
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
movsbq -0x29(%rbp),%rdx
and    $0xff,%rdx
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ce <bin2hex+0x6e>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%ecx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    128a <bin2hex+0x12a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
xor    %r15d,%r15d
xor    %esi,%esi
call   1080 <open@plt>
test   %eax,%eax
jle    12be <bin2hex+0x13e>
mov    %eax,%r12d
lea    0xe50(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xe40(%rip),%rdi
mov    $0x2b,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
lea    0xe53(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xf(%rsp),%rsi
mov    $0x1,%r15d
mov    %r12d,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1298 <bin2hex+0x118>
mov    %r14,0x10(%rsp)
lea    0xe37(%rip),%r13
lea    0xf(%rsp),%r14
xor    %ebp,%ebp
xor    %r15d,%r15d
jmp    125d <bin2hex+0xdd>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0xf(%rsp),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x1,%r15d
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
add    $0xffffffff,%ebp
test   %eax,%eax
je     128b <bin2hex+0x10b>
test   %ebp,%ebp
je     1230 <bin2hex+0xb0>
mov    %r15d,%eax
mov    $0xcccccccd,%ecx
imul   %rcx,%rax
shr    $0x23,%rax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
neg    %eax
cmp    %ebp,%eax
jne    1230 <bin2hex+0xb0>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
jmp    1230 <bin2hex+0xb0>
neg    %ebp
mov    0x10(%rsp),%r14
mov    $0x1,%r15d
lea    0xdb7(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [44 x i8] c"/* automatically generated by configure */\0A\00"
@.str.2 = [31 x i8] c"static unsigned char %s[] = {\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [9 x i8] c"0x%02lx,\00"
@.str.5 = [21 x i8] c"};\0A#define %sLen %d\0A\00"
define i32 @bin2hex(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 i8
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %11
%13 = load i8*, i8** %5
%14 = call i32 @open(i8* %13, i32 0)
store i32 %14, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %57
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %12
%19 = load i32*, i32** %12
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32*, i32** %12
%22 = load i8*, i8** %7
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %16, %40
%25 = load i32, i32* %8
%26 = call i32 @read(i32 %25, i8* %10, i32 1)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %48
30: ; preds = %24
%31 = load i32, i32* %11
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i32, i32* %11
%35 = srem i32 %34, 10
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32*, i32** %12
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %37, %33, %30
%41 = load i32*, i32** %12
%42 = load i8, i8* %10
%43 = sext i8 %42 to i64
%44 = and i64 %43, 255
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %41, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %44)
%46 = load i32, i32* %11
%47 = add nsw i32 %46, 1
store i32 %47, i32* %11
br label %24
48: ; preds = %29
%49 = load i32*, i32** %12
%50 = load i8*, i8** %7
%51 = load i32, i32* %11
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %50, i32 %51)
%53 = load i32*, i32** %12
%54 = call i32 @fclose(i32* %53)
%55 = load i32, i32* %8
%56 = call i32 @close(i32 %55)
store i32 1, i32* %4
br label %58
57: ; preds = %3
store i32 0, i32* %4
br label %58
58: ; preds = %57, %48
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [44 x i8] c"/* automatically generated by configure */\0A\00"
@.str.2 = [31 x i8] c"static unsigned char %s[] = {\0A\00"
@.str.4 = [9 x i8] c"0x%02lx,\00"
@.str.5 = [21 x i8] c"};\0A#define %sLen %d\0A\00"
define i32 @bin2hex(i8* nocapture %0, i8* nocapture %1, i8* %2) {
%4 = alloca i8
%5 = tail call i32 @open(i8* %0, i32 0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %33
7: ; preds = %3
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 43, i64 1, i32* %8)
%10 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%11 = call i32 @read(i32 %5, i8* %4, i32 1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %7, %21
%14 = phi i32 [ %25, %21 ], [ 0, %7 ]
%15 = icmp ne i32 %14, 0
%16 = urem i32 %14, 10
%17 = icmp eq i32 %16, 0
%18 = and i1 %15, %17
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @fputc(i32 10, i32* %8)
br label %21
21: ; preds = %19, %13
%22 = load i8, i8* %4
%23 = zext i8 %22 to i64
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %23)
%25 = add nuw nsw i32 %14, 1
%26 = call i32 @read(i32 %5, i8* %4, i32 1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %13
28: ; preds = %21, %7
%29 = phi i32 [ 0, %7 ], [ %25, %21 ]
%30 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %2, i32 %29)
%31 = tail call i32 @fclose(i32* %8)
%32 = tail call i32 @close(i32 %5)
br label %33
33: ; preds = %3, %28
%34 = phi i32 [ 1, %28 ], [ 0, %3 ]
ret i32 %34
}