#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 uint;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ sscanf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Usage: hextofile <hex-encoded data>\n");
    exit(1);
  }
  argv++;
  uint offset = 0;
  uint size = strlen(*argv);
  size -= size % 2;
  while (offset < size) {
    char snippet[2];
    char byte;
    strncpy(snippet, *argv + offset, 2);
    sscanf(snippet, "%X", &byte);
    putchar(byte);
    offset += 2;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1196 <main+0x36>
lea    0xe79(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    121e <main+0xbe>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x2,%edx
call   1040 <strncpy@plt>
lea    -0x1a(%rbp),%rdi
lea    0xe27(%rip),%rsi
lea    -0x1b(%rbp),%rdx
call   1070 <sscanf@plt>
movsbl -0x1b(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    11cb <main+0x6b>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11e5 <main+0x85>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
and    $0xfffffffe,%ebp
jle    11d4 <main+0x74>
xor    %ebx,%ebx
lea    0x6(%rsp),%r13
lea    0xe6a(%rip),%r15
lea    0x5(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    0x8(%r14),%rsi
add    %rbx,%rsi
mov    %r13,%rdi
mov    $0x2,%edx
call   1040 <strncpy@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <sscanf@plt>
movsbl 0x5(%rsp),%edi
call   1030 <putchar@plt>
add    $0x2,%rbx
cmp    %ebx,%ebp
jg     11a0 <main+0x40>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Usage: hextofile <hex-encoded data>\0A\00"
@.str.1 = [3 x i8] c"%X\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i8]
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i32 1
store i8** %17, i8*** %5
store i32 0, i32* %6
%18 = load i8**, i8*** %5
%19 = load i8*, i8** %18
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = srem i32 %21, 2
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, %22
store i32 %24, i32* %7
br label %25
25: ; preds = %29, %15
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%31 = load i8**, i8*** %5
%32 = load i8*, i8** %31
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = call i32 @strncpy(i8* %30, i8* %35, i32 2)
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%38 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%39 = load i8, i8* %9
%40 = call i32 @putchar(i8 signext %39)
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 2
store i32 %42, i32* %6
br label %25
43: ; preds = %25
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%X\00"
@str = [36 x i8] c"Usage: hextofile <hex-encoded data>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i8]
%4 = alloca i8
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @strlen(i8* %11)
%13 = srem i32 %12, 2
%14 = sub nsw i32 %12, %13
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %9
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %26, %18 ]
%20 = load i8*, i8** %10
%21 = getelementptr inbounds i8, i8* %20, i64 %19
%22 = call i32 @strncpy(i8* %17, i8* %21, i32 2)
%23 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%24 = load i8, i8* %4
%25 = call i32 @putchar(i8 signext %24)
%26 = add nuw i64 %19, 2
%27 = trunc i64 %26 to i32
%28 = icmp sgt i32 %14, %27
br i1 %28, label %18, label %29
29: ; preds = %18, %9
ret i32 0
}