#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int hex2int(int);
int /*<<< orphan*/ stderr;
int hexfile2binfile(FILE *in, FILE *out) {
  int b;
  int h1, h2;
  int c1, c2;
  for (;;) {
    h1 = fgetc(in);
    h2 = fgetc(in);
    if (feof(in)) {
      break;
    }
    c1 = hex2int(h1);
    c2 = hex2int(h2);
    if (c1 == -1 || c2 == -1) {
      fprintf(stderr, "non-hex input\n");
      return 1;
    }
    b = (c1 * 16) + c2;
    fputc(b, out);
  }
  return 0;
}


x86 assembly (clang, -O0):
<hexfile2binfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     1190 <hexfile2binfile+0x40>
jmp    11f8 <hexfile2binfile+0xa8>
mov    -0x20(%rbp),%edi
call   1030 <hex2int@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <hex2int@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     11ba <hexfile2binfile+0x6a>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11db <hexfile2binfile+0x8b>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ff <hexfile2binfile+0xaf>
mov    -0x28(%rbp),%eax
shl    $0x4,%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <fputc@plt>
jmp    1160 <hexfile2binfile+0x10>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexfile2binfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1070 <feof@plt>
xor    %r15d,%r15d
test   %rax,%rax
jne    11e7 <hexfile2binfile+0x97>
nop
mov    %ebp,%edi
call   1030 <hex2int@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <hex2int@plt>
cmp    $0xffffffff,%ebp
je     11cc <hexfile2binfile+0x7c>
cmp    $0xffffffff,%eax
je     11cc <hexfile2binfile+0x7c>
shl    $0x4,%ebp
add    %eax,%ebp
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %r12,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     1180 <hexfile2binfile+0x30>
jmp    11e7 <hexfile2binfile+0x97>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"non-hex input\0A\00"
define i32 @hexfile2binfile(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
br label %11
11: ; preds = %33, %2
%12 = load i32*, i32** %4
%13 = call i32 @fgetc(i32* %12)
store i32 %13, i32* %7
%14 = load i32*, i32** %4
%15 = call i32 @fgetc(i32* %14)
store i32 %15, i32* %8
%16 = load i32*, i32** %4
%17 = call i64 @feof(i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
br label %41
20: ; preds = %11
%21 = load i32, i32* %7
%22 = call i32 @hex2int(i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %8
%24 = call i32 @hex2int(i32 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %20
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27, %20
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %42
33: ; preds = %27
%34 = load i32, i32* %9
%35 = mul nsw i32 %34, 16
%36 = load i32, i32* %10
%37 = add nsw i32 %35, %36
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = load i32*, i32** %5
%40 = call i32 @fputc(i32 %38, i32* %39)
br label %11
41: ; preds = %19
store i32 0, i32* %3
br label %42
42: ; preds = %41, %30
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"non-hex input\0A\00"
define i32 @hexfile2binfile(i32* nocapture %0, i32* nocapture %1) {
%3 = tail call i32 @fgetc(i32* %0)
%4 = tail call i32 @fgetc(i32* %0)
%5 = tail call i64 @feof(i32* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %2, %18
%8 = phi i32 [ %23, %18 ], [ %4, %2 ]
%9 = phi i32 [ %22, %18 ], [ %3, %2 ]
%10 = tail call i32 @hex2int(i32 %9)
%11 = tail call i32 @hex2int(i32 %8)
%12 = icmp eq i32 %10, -1
%13 = icmp eq i32 %11, -1
%14 = select i1 %12, i1 true, i1 %13
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
18: ; preds = %7
%19 = shl nsw i32 %10, 4
%20 = add nsw i32 %19, %11
%21 = tail call i32 @fputc(i32 %20, i32* %1)
%22 = tail call i32 @fgetc(i32* %0)
%23 = tail call i32 @fgetc(i32* %0)
%24 = tail call i64 @feof(i32* %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %7, label %26
26: ; preds = %18, %2, %15
%27 = phi i32 [ 1, %15 ], [ 0, %2 ], [ 0, %18 ]
ret i32 %27
}