#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
long bin2asm(FILE *in, FILE *out, long length) {
  long i = 0;
  int ch = fgetc(in);
  while ((ch != -1) && ((length == -1) || (i < length))) {
    int m = (i % 16);
    if (m == 0)
      fwrite(".db ", 1, 4, out);
    else
      fputc(',', out);
    fprintf(out, "$%.2X", ch);
    if (m == 15)
      fputc('\n', out);
    ch = fgetc(in);
    ++i;
  }
  fputc('\n', out);
  return i;
}


x86 assembly (clang, -O0):
<bin2asm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x29(%rbp)
je     119b <bin2asm+0x5b>
mov    $0x1,%al
cmpq   $0xffffffffffffffff,-0x18(%rbp)
mov    %al,-0x2a(%rbp)
je     1195 <bin2asm+0x55>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
setl   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11ab <bin2asm+0x6b>
jmp    123d <bin2asm+0xfd>
mov    -0x20(%rbp),%rax
mov    $0x10,%ecx
cqto
idiv   %rcx
mov    %edx,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11e7 <bin2asm+0xa7>
mov    -0x10(%rbp),%rcx
lea    0xe2d(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1060 <fwrite@plt>
jmp    11f5 <bin2asm+0xb5>
mov    -0x10(%rbp),%rsi
mov    $0x2c,%edi
call   1040 <fputc@plt>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe02(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0xf,-0x28(%rbp)
jne    1220 <bin2asm+0xe0>
mov    -0x10(%rbp),%rsi
mov    $0xa,%edi
call   1040 <fputc@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1168 <bin2asm+0x28>
mov    -0x10(%rbp),%rsi
mov    $0xa,%edi
call   1040 <fputc@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2asm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <fgetc@plt>
cmp    $0xffffffff,%eax
je     11f4 <bin2asm+0xb4>
mov    %eax,%r13d
xor    %ebx,%ebx
jmp    11af <bin2asm+0x6f>
nopl   0x0(%rax)
lea    0xe89(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
mov    %r14,%rdi
lea    0xe72(%rip),%rsi
mov    %r13d,%edx
call   1050 <fprintf@plt>
mov    %r12,%rdi
call   1030 <fgetc@plt>
mov    %eax,%r13d
add    $0x1,%rbx
cmp    $0xffffffff,%eax
je     11f6 <bin2asm+0xb6>
cmp    $0xffffffffffffffff,%r15
je     11ba <bin2asm+0x7a>
cmp    %r15,%rbx
jge    11f6 <bin2asm+0xb6>
mov    %ebx,%ebp
and    $0xf,%ebp
je     1170 <bin2asm+0x30>
mov    $0x2c,%edi
mov    %r14,%rsi
call   1040 <fputc@plt>
mov    %r14,%rdi
lea    0xe2d(%rip),%rsi
mov    %r13d,%edx
call   1050 <fprintf@plt>
cmp    $0xf,%ebp
jne    119b <bin2asm+0x5b>
mov    $0xa,%edi
mov    %r14,%rsi
call   1040 <fputc@plt>
jmp    119b <bin2asm+0x5b>
xor    %ebx,%ebx
mov    $0xa,%edi
mov    %r14,%rsi
call   1040 <fputc@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c".db \00"
@.str.1 = [6 x i8] c"$%.2X\00"
define i64 @bin2asm(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
store i64 0, i64* %7
%10 = load i32*, i32** %4
%11 = call i32 @fgetc(i32* %10)
store i32 %11, i32* %8
br label %12
12: ; preds = %47, %3
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i64, i64* %7
%20 = load i64, i64* %6
%21 = icmp slt i64 %19, %20
br label %22
22: ; preds = %18, %15
%23 = phi i1 [ true, %15 ], [ %21, %18 ]
br label %24
24: ; preds = %22, %12
%25 = phi i1 [ false, %12 ], [ %23, %22 ]
br i1 %25, label %26, label %52
26: ; preds = %24
%27 = load i64, i64* %7
%28 = srem i64 %27, 16
%29 = trunc i64 %28 to i32
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32*, i32** %5
%34 = call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1, i32 4, i32* %33)
br label %38
35: ; preds = %26
%36 = load i32*, i32** %5
%37 = call i32 @fputc(i8 signext 44, i32* %36)
br label %38
38: ; preds = %35, %32
%39 = load i32*, i32** %5
%40 = load i32, i32* %8
%41 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 15
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i32*, i32** %5
%46 = call i32 @fputc(i8 signext 10, i32* %45)
br label %47
47: ; preds = %44, %38
%48 = load i32*, i32** %4
%49 = call i32 @fgetc(i32* %48)
store i32 %49, i32* %8
%50 = load i64, i64* %7
%51 = add nsw i64 %50, 1
store i64 %51, i64* %7
br label %12
52: ; preds = %24
%53 = load i32*, i32** %5
%54 = call i32 @fputc(i8 signext 10, i32* %53)
%55 = load i64, i64* %7
ret i64 %55
}

LLVM IR (-O2):
@.str = [5 x i8] c".db \00"
@.str.1 = [6 x i8] c"$%.2X\00"
define i64 @bin2asm(i32* nocapture %0, i32* nocapture %1, i64 %2) {
%4 = tail call i32 @fgetc(i32* %0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %30, label %6
6: ; preds = %3
%7 = icmp eq i64 %2, -1
br label %8
8: ; preds = %6, %26
%9 = phi i64 [ 0, %6 ], [ %28, %26 ]
%10 = phi i32 [ %4, %6 ], [ %27, %26 ]
%11 = icmp slt i64 %9, %2
%12 = select i1 %7, i1 true, i1 %11
br i1 %12, label %13, label %30
13: ; preds = %8
%14 = trunc i64 %9 to i32
%15 = and i32 %14, 15
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1, i32 4, i32* %1)
%19 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %26
20: ; preds = %13
%21 = tail call i32 @fputc(i8 signext 44, i32* %1)
%22 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%23 = icmp eq i32 %15, 15
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @fputc(i8 signext 10, i32* %1)
br label %26
26: ; preds = %17, %24, %20
%27 = tail call i32 @fgetc(i32* %0)
%28 = add nuw nsw i64 %9, 1
%29 = icmp eq i32 %27, -1
br i1 %29, label %30, label %8
30: ; preds = %8, %26, %3
%31 = phi i64 [ 0, %3 ], [ %28, %26 ], [ %9, %8 ]
%32 = tail call i32 @fputc(i8 signext 10, i32* %1)
ret i64 %31
}