#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*/ basename(char const *);
char *error();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy_s(char *, int, char *);
int /*<<< orphan*/ stripext(char *, int /*<<< orphan*/);
int strlen(char *);
int main(int argc, char *argv[]) {
  const char *binario = argv[1];
  char simbolo[144];
  FILE *f;
  size_t col;
  int coma = 0;
  if (argc < 2 || argc > 3) {
    printf("USO: bin2h <fichero binario> [nombre del s¡mbolo]\n");
    return 1;
  }
  if (argc > 2) {
    strcpy_s(simbolo, 144, argv[2]);
  } else {
    stripext(simbolo, basename(binario));
  }
  f = fopen(binario, "rb");
  if (!f) {
    fprintf(stderr, "Error al leer %s: %s\n", binario, error());
  }
  printf("static const char %s[] = {", simbolo);
  col = 18 + 6 + strlen(simbolo);
  while (!feof(f)) {
    unsigned char c = (unsigned char)fgetc(f);
    int addcols = coma;
    if (c > 99) {
      addcols += 3;
    } else if (c > 9) {
      addcols += 2;
    } else {
      addcols += 1;
    }
    if (coma)
      printf(",");
    if (col + addcols >= 80) {
      printf("\n");
      col = addcols - coma;
    } else {
      col += addcols;
    }
    printf("%d", c);
    if (!coma)
      coma = 1;
  }
  fclose(f);
  if (col + 2 >= 80)
    printf("\n");
  printf("};\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc4(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11f3 <main+0x43>
cmpl   $0x3,-0x8(%rbp)
jle    120d <main+0x5d>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    146c <main+0x2bc>
cmpl   $0x2,-0x8(%rbp)
jle    1235 <main+0x85>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
mov    $0x90,%esi
call   10d0 <strcpy_s@plt>
jmp    125a <main+0xaa>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <basename@plt>
mov    -0xd8(%rbp),%rdi
mov    %eax,%esi
call   1050 <stripext@plt>
mov    -0x18(%rbp),%rdi
lea    0xdcf(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0xb8(%rbp)
cmpq   $0x0,-0xb8(%rbp)
jne    12bc <main+0x10c>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    $0x0,%al
call   10b0 <error@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rdx
mov    %rax,%rcx
lea    0xd80(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0xb0(%rbp),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0xb0(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x18,%eax
cltq
mov    %rax,-0xc0(%rbp)
mov    -0xb8(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    130a <main+0x15a>
jmp    1428 <main+0x278>
mov    -0xb8(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0xc5(%rbp)
mov    -0xc4(%rbp),%eax
mov    %eax,-0xcc(%rbp)
movzbl -0xc5(%rbp),%eax
cmp    $0x63,%eax
jle    134c <main+0x19c>
mov    -0xcc(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0xcc(%rbp)
jmp    1384 <main+0x1d4>
movzbl -0xc5(%rbp),%eax
cmp    $0x9,%eax
jle    1370 <main+0x1c0>
mov    -0xcc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xcc(%rbp)
jmp    137f <main+0x1cf>
mov    -0xcc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xcc(%rbp)
jmp    1384 <main+0x1d4>
cmpl   $0x0,-0xc4(%rbp)
je     139f <main+0x1ef>
lea    0xcd0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc0(%rbp),%rax
movslq -0xcc(%rbp),%rcx
add    %rcx,%rax
cmp    $0x50,%rax
jb     13e2 <main+0x232>
lea    0xcae(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xcc(%rbp),%eax
sub    -0xc4(%rbp),%eax
cltq
mov    %rax,-0xc0(%rbp)
jmp    13f7 <main+0x247>
movslq -0xcc(%rbp),%rax
add    -0xc0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
movzbl -0xc5(%rbp),%esi
lea    0xc65(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0xc4(%rbp)
jne    1423 <main+0x273>
movl   $0x1,-0xc4(%rbp)
jmp    12e9 <main+0x139>
mov    -0xb8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc0(%rbp),%rax
add    $0x2,%rax
cmp    $0x50,%rax
jb     1457 <main+0x2a7>
lea    0xc1f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xc0f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    -0x4(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     11ff <main+0x2f>
lea    0xe4a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12c0 <main+0xf0>
mov    0x8(%rsi),%r14
cmp    $0x3,%edi
jb     121b <main+0x4b>
mov    0x10(%rsi),%rdx
mov    %rsp,%rdi
mov    $0x90,%esi
call   10f0 <strcpy_s@plt>
jmp    122d <main+0x5d>
mov    %r14,%rdi
call   10b0 <basename@plt>
mov    %rsp,%rdi
mov    %eax,%esi
call   1070 <stripext@plt>
lea    0xdcc(%rip),%rsi
mov    %r14,%rdi
call   10e0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1268 <main+0x98>
mov    0x2d95(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   10d0 <error@plt>
lea    0xda8(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   10a0 <fprintf@plt>
lea    0xdaa(%rip),%rdi
mov    %rsp,%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
add    $0x18,%eax
movslq %eax,%rbx
mov    %r15,%rdi
call   10c0 <feof@plt>
test   %eax,%eax
je     12d2 <main+0x102>
mov    %r15,%rdi
call   1050 <fclose@plt>
add    $0xffffffffffffffb2,%rbx
cmp    $0xffffffffffffffaf,%rbx
ja     12b2 <main+0xe2>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xd7e(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
jmp    1308 <main+0x138>
nopw   0x0(%rax,%rax,1)
lea    0xd4d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1080 <printf@plt>
test   %r12d,%r12d
mov    $0x1,%eax
cmove  %eax,%r12d
mov    %r15,%rdi
call   10c0 <feof@plt>
test   %eax,%eax
jne    1296 <main+0xc6>
mov    %r15,%rdi
call   1090 <fgetc@plt>
movzbl %al,%ebp
cmp    $0xa,%ebp
mov    $0x1,%r13d
sbb    $0xffffffff,%r13d
cmp    $0x64,%ebp
mov    $0x3,%eax
cmovae %eax,%r13d
lea    (%r12,%r13,1),%r14d
test   %r12d,%r12d
je     133f <main+0x16f>
mov    $0x2c,%edi
call   1030 <putchar@plt>
mov    %r14d,%eax
add    %rax,%rbx
cmp    $0x50,%rbx
jb     12e0 <main+0x110>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r13d,%ebx
jmp    12e0 <main+0x110>

LLVM IR (-O0):
@.str = [52 x i8] c"USO: bin2h <fichero binario> [nombre del s\C2\A1mbolo]\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Error al leer %s: %s\0A\00"
@.str.3 = [27 x i8] c"static const char %s[] = {\00"
@.str.4 = [2 x i8] c",\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [4 x i8] c"};\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [144 x i8]
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %6
store i32 0, i32* %10
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 3
br i1 %20, label %21, label %23
21: ; preds = %18, %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %121
23: ; preds = %18
%24 = load i32, i32* %4
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = getelementptr inbounds [144 x i8], [144 x i8]* %7, i64 0, i64 0
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @strcpy_s(i8* %27, i32 144, i8* %30)
br label %37
32: ; preds = %23
%33 = getelementptr inbounds [144 x i8], [144 x i8]* %7, i64 0, i64 0
%34 = load i8*, i8** %6
%35 = call i32 @basename(i8* %34)
%36 = call i32 @stripext(i8* %33, i32 %35)
br label %37
37: ; preds = %32, %26
%38 = load i8*, i8** %6
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %39, i32** %8
%40 = load i32*, i32** %8
%41 = icmp ne i32* %40, null
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %6
%45 = call i8* (...) @error()
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %45)
br label %47
47: ; preds = %42, %37
%48 = getelementptr inbounds [144 x i8], [144 x i8]* %7, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds [144 x i8], [144 x i8]* %7, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = add nsw i32 24, %51
%53 = sext i32 %52 to i64
store i64 %53, i64* %9
br label %54
54: ; preds = %110, %47
%55 = load i32*, i32** %8
%56 = call i32 @feof(i32* %55)
%57 = icmp ne i32 %56, 0
%58 = xor i1 %57, true
br i1 %58, label %59, label %111
59: ; preds = %54
%60 = load i32*, i32** %8
%61 = call i64 @fgetc(i32* %60)
%62 = trunc i64 %61 to i8
store i8 %62, i8* %11
%63 = load i32, i32* %10
store i32 %63, i32* %12
%64 = load i8, i8* %11
%65 = zext i8 %64 to i32
%66 = icmp sgt i32 %65, 99
br i1 %66, label %67, label %70
67: ; preds = %59
%68 = load i32, i32* %12
%69 = add nsw i32 %68, 3
store i32 %69, i32* %12
br label %81
70: ; preds = %59
%71 = load i8, i8* %11
%72 = zext i8 %71 to i32
%73 = icmp sgt i32 %72, 9
br i1 %73, label %74, label %77
74: ; preds = %70
%75 = load i32, i32* %12
%76 = add nsw i32 %75, 2
store i32 %76, i32* %12
br label %80
77: ; preds = %70
%78 = load i32, i32* %12
%79 = add nsw i32 %78, 1
store i32 %79, i32* %12
br label %80
80: ; preds = %77, %74
br label %81
81: ; preds = %80, %67
%82 = load i32, i32* %10
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %81
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %86
86: ; preds = %84, %81
%87 = load i64, i64* %9
%88 = load i32, i32* %12
%89 = sext i32 %88 to i64
%90 = add i64 %87, %89
%91 = icmp uge i64 %90, 80
br i1 %91, label %92, label %98
92: ; preds = %86
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%94 = load i32, i32* %12
%95 = load i32, i32* %10
%96 = sub nsw i32 %94, %95
%97 = sext i32 %96 to i64
store i64 %97, i64* %9
br label %103
98: ; preds = %86
%99 = load i32, i32* %12
%100 = sext i32 %99 to i64
%101 = load i64, i64* %9
%102 = add i64 %101, %100
store i64 %102, i64* %9
br label %103
103: ; preds = %98, %92
%104 = load i8, i8* %11
%105 = zext i8 %104 to i32
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %105)
%107 = load i32, i32* %10
%108 = icmp ne i32 %107, 0
br i1 %108, label %110, label %109
109: ; preds = %103
store i32 1, i32* %10
br label %110
110: ; preds = %109, %103
br label %54
111: ; preds = %54
%112 = load i32*, i32** %8
%113 = call i32 @fclose(i32* %112)
%114 = load i64, i64* %9
%115 = add i64 %114, 2
%116 = icmp uge i64 %115, 80
br i1 %116, label %117, label %119
117: ; preds = %111
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %119
119: ; preds = %117, %111
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %121
121: ; preds = %119, %21
%122 = load i32, i32* %3
ret i32 %122
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Error al leer %s: %s\0A\00"
@.str.3 = [27 x i8] c"static const char %s[] = {\00"
@.str.6 = [3 x i8] c"%d\00"
@str = [3 x i8] c"};\00"
@str.8 = [51 x i8] c"USO: bin2h <fichero binario> [nombre del s\C2\A1mbolo]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [144 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = getelementptr inbounds [144 x i8], [144 x i8]* %3, i64 0, i64 0
%7 = add i32 %0, -4
%8 = icmp ult i32 %7, -2
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.8, i64 0, i64 0))
br label %70
11: ; preds = %2
%12 = icmp ugt i32 %0, 2
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = call i32 @strcpy_s(i8* %6, i32 144, i8* %15)
br label %20
17: ; preds = %11
%18 = tail call i32 @basename(i8* %5)
%19 = call i32 @stripext(i8* %6, i32 %18)
br label %20
20: ; preds = %17, %13
%21 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = call i8* (...) @error()
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %25)
br label %27
27: ; preds = %23, %20
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%29 = call i32 @strlen(i8* %6)
%30 = add nsw i32 %29, 24
%31 = sext i32 %30 to i64
%32 = call i32 @feof(i32* %21)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %61
34: ; preds = %27, %55
%35 = phi i32 [ %58, %55 ], [ 0, %27 ]
%36 = phi i64 [ %56, %55 ], [ %31, %27 ]
%37 = call i64 @fgetc(i32* %21)
%38 = trunc i64 %37 to i32
%39 = and i32 %38, 255
%40 = icmp ugt i32 %39, 99
%41 = icmp ugt i32 %39, 9
%42 = select i1 %41, i32 2, i32 1
%43 = select i1 %40, i32 3, i32 %42
%44 = add nsw i32 %35, %43
%45 = icmp eq i32 %35, 0
br i1 %45, label %48, label %46
46: ; preds = %34
%47 = call i32 @putchar(i32 44)
br label %48
48: ; preds = %46, %34
%49 = zext i32 %44 to i64
%50 = add i64 %36, %49
%51 = icmp ugt i64 %50, 79
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = call i32 @putchar(i32 10)
%54 = zext i32 %43 to i64
br label %55
55: ; preds = %48, %52
%56 = phi i64 [ %54, %52 ], [ %50, %48 ]
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %39)
%58 = select i1 %45, i32 1, i32 %35
%59 = call i32 @feof(i32* %21)
%60 = icmp eq i32 %59, 0
br i1 %60, label %34, label %61
61: ; preds = %55, %27
%62 = phi i64 [ %31, %27 ], [ %56, %55 ]
%63 = call i32 @fclose(i32* %21)
%64 = add nsw i64 %62, -78
%65 = icmp ult i64 %64, -80
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @putchar(i32 10)
br label %68
68: ; preds = %66, %61
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %70
70: ; preds = %68, %9
%71 = phi i32 [ 1, %9 ], [ 0, %68 ]
ret i32 %71
}