#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int hex2int(char);
int /*<<< orphan*/ stderr;
int hexarg2binfile(char *arg, FILE *out) {
  int i;
  int b;
  int c1, c2;
  for (i = 0; arg[i] != '\0' && arg[i + 1] != '\0'; i += 2) {
    c1 = hex2int(arg[i]);
    c2 = hex2int(arg[i + 1]);
    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):
<hexarg2binfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
je     117b <hexarg2binfile+0x4b>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    118b <hexarg2binfile+0x5b>
jmp    1213 <hexarg2binfile+0xe3>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <hex2int@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <hex2int@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     11cc <hexarg2binfile+0x9c>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11ed <hexarg2binfile+0xbd>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121a <hexarg2binfile+0xea>
mov    -0x24(%rbp),%eax
shl    $0x4,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <fputc@plt>
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1147 <hexarg2binfile+0x17>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexarg2binfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
xor    %r14d,%r14d
test   %al,%al
je     11ab <hexarg2binfile+0x7b>
mov    %rsi,%r15
mov    %rdi,%rbp
add    $0x2,%rbp
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,-0x1(%rbp)
je     11ab <hexarg2binfile+0x7b>
movsbl %al,%edi
call   1030 <hex2int@plt>
mov    %eax,%ebx
movsbl -0x1(%rbp),%edi
call   1030 <hex2int@plt>
cmp    $0xffffffff,%ebx
je     1190 <hexarg2binfile+0x60>
cmp    $0xffffffff,%eax
je     1190 <hexarg2binfile+0x60>
shl    $0x4,%ebx
add    %ebx,%eax
mov    %eax,%edi
mov    %r15,%rsi
call   1040 <fputc@plt>
movzbl 0x0(%rbp),%eax
add    $0x2,%rbp
test   %al,%al
jne    1150 <hexarg2binfile+0x20>
jmp    11ab <hexarg2binfile+0x7b>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
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 @hexarg2binfile(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %6
br label %10
10: ; preds = %59, %2
%11 = load i8*, i8** %4
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %10
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br label %27
27: ; preds = %18, %10
%28 = phi i1 [ false, %10 ], [ %26, %18 ]
br i1 %28, label %29, label %62
29: ; preds = %27
%30 = load i8*, i8** %4
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @hex2int(i8 signext %34)
store i32 %35, i32* %8
%36 = load i8*, i8** %4
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = load i8, i8* %40
%42 = call i32 @hex2int(i8 signext %41)
store i32 %42, i32* %9
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, -1
br i1 %44, label %48, label %45
45: ; preds = %29
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %45, %29
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %63
51: ; preds = %45
%52 = load i32, i32* %8
%53 = mul nsw i32 %52, 16
%54 = load i32, i32* %9
%55 = add nsw i32 %53, %54
store i32 %55, i32* %7
%56 = load i32, i32* %7
%57 = load i32*, i32** %5
%58 = call i32 @fputc(i32 %56, i32* %57)
br label %59
59: ; preds = %51
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 2
store i32 %61, i32* %6
br label %10
62: ; preds = %27
store i32 0, i32* %3
br label %63
63: ; preds = %62, %48
%64 = load i32, i32* %3
ret i32 %64
}

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