#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, unsigned char);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int hex2bin(void) {
  char hex[3];
  int d = 0;
  unsigned char c;
  int stdin_fd = fileno(stdin);
  int n_read;
  while ((n_read = read(stdin_fd, hex, 2)) > 0) {
    if (n_read == 1) {
      if (hex[0] != '\n') {
        fprintf(stderr, "input parse error, odd digits in hex file\n");
        exit(1);
      } else
        exit(1);
    }
    hex[2] = '\0';
    sscanf(hex, "%x", &d);
    c = (unsigned char)d;
    printf("%c", c);
  }
  return 0;
}


x86 assembly (clang, -O0):
<hex2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x3(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1210 <hex2bin+0xb0>
cmpl   $0x1,-0x14(%rbp)
jne    11dd <hex2bin+0x7d>
movsbl -0x3(%rbp),%eax
cmp    $0xa,%eax
je     11d3 <hex2bin+0x73>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movb   $0x0,-0x1(%rbp)
lea    -0x3(%rbp),%rdi
lea    0xe3f(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x8(%rbp),%eax
mov    %al,-0x9(%rbp)
lea    0xe2c(%rip),%rdi
movzbl -0x9(%rbp),%esi
call   1030 <printf@plt>
jmp    1180 <hex2bin+0x20>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2bin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%ebp
lea    0x9(%rsp),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11f8 <hex2bin+0x98>
lea    0xe8b(%rip),%r14
lea    0x9(%rsp),%rbx
lea    0xc(%rsp),%r15
lea    0xe7d(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x1,%eax
je     1207 <hex2bin+0xa7>
movb   $0x0,0xb(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <sscanf@plt>
movzbl 0xc(%rsp),%esi
mov    %r12,%rdi
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <read@plt>
test   %eax,%eax
jg     11c0 <hex2bin+0x60>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmpb   $0xa,0x9(%rsp)
je     1223 <hex2bin+0xc3>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@stderr = global i32 0
@.str = [43 x i8] c"input parse error, odd digits in hex file\0A\00"
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [3 x i8] c"%c\00"
define i32 @hex2bin() {
%1 = alloca [3 x i8]
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %2
%6 = load i32, i32* @stdin
%7 = call i32 @fileno(i32 %6)
store i32 %7, i32* %4
br label %8
8: ; preds = %27, %0
%9 = load i32, i32* %4
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 2)
store i32 %11, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %35
13: ; preds = %8
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 10
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %13
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 2
store i8 0, i8* %28
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%31 = load i32, i32* %2
%32 = trunc i32 %31 to i8
store i8 %32, i8* %3
%33 = load i8, i8* %3
%34 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %33)
br label %8
35: ; preds = %8
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str = [43 x i8] c"input parse error, odd digits in hex file\0A\00"
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [3 x i8] c"%c\00"
define i32 @hex2bin() {
%1 = alloca [3 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = load i32, i32* @stdin
%6 = tail call i32 @fileno(i32 %5)
%7 = call i32 @read(i32 %6, i8* %3, i32 2)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %0
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 2
br label %11
11: ; preds = %9, %23
%12 = phi i32 [ %7, %9 ], [ %28, %23 ]
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i8, i8* %3
%16 = icmp eq i8 %15, 10
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %11
store i8 0, i8* %10
%24 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%25 = load i32, i32* %2
%26 = trunc i32 %25 to i8
%27 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %26)
%28 = call i32 @read(i32 %6, i8* %3, i32 2)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %11, label %30
30: ; preds = %23, %0
ret i32 0
}