#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*/ sscanf(char const *, char *, int *);
int strlen(char const *);
int hex_decode(char *out, const char *in) {
  int len = strlen(in);
  int i, x;
  if (len % 2 != 0)
    return 0;
  for (i = 0; i < len; i += 2) {
    if (!sscanf(&in[i], "%02x", &x))
      return 0;
    out[i / 2] = x;
  }
  return i / 2;
}


x86 assembly (clang, -O0):
<hex_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     115c <hex_decode+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <hex_decode+0xc3>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d5 <hex_decode+0xb5>
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
lea    0xe7f(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <sscanf@plt>
cmp    $0x0,%eax
jne    119f <hex_decode+0x7f>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <hex_decode+0xc3>
mov    -0x24(%rbp),%eax
mov    %al,-0x31(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x31(%rbp),%dl
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
jmp    1163 <hex_decode+0x43>
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
xor    %eax,%eax
test   $0x1,%bpl
jne    1191 <hex_decode+0x71>
test   %ebp,%ebp
jle    1191 <hex_decode+0x71>
lea    0xeb2(%rip),%r12
lea    0x4(%rsp),%r13
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
lea    (%r15,%r14,1),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <sscanf@plt>
test   %eax,%eax
je     118f <hex_decode+0x6f>
movzbl 0x4(%rsp),%eax
mov    %al,(%rbx)
add    $0x2,%r14
add    $0x1,%rbx
cmp    %r14d,%ebp
jg     1160 <hex_decode+0x40>
shr    %r14d
mov    %r14d,%eax
jmp    1191 <hex_decode+0x71>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i32 @hex_decode(i8* nocapture writeonly %0, i8* %1) {
%3 = alloca i32
%4 = tail call i32 @strlen(i8* %1)
%5 = bitcast i32* %3 to i8*
%6 = and i32 %4, 1
%7 = icmp eq i32 %6, 0
%8 = icmp sgt i32 %4, 0
%9 = and i1 %7, %8
br i1 %9, label %10, label %26
10: ; preds = %2, %15
%11 = phi i64 [ %20, %15 ], [ 0, %2 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %10
%16 = load i32, i32* %3
%17 = trunc i32 %16 to i8
%18 = lshr exact i64 %11, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 %17, i8* %19
%20 = add nuw i64 %11, 2
%21 = trunc i64 %20 to i32
%22 = icmp sgt i32 %4, %21
br i1 %22, label %10, label %23
23: ; preds = %15
%24 = trunc i64 %20 to i32
%25 = lshr exact i32 %24, 1
br label %26
26: ; preds = %10, %23, %2
%27 = phi i32 [ 0, %2 ], [ %25, %23 ], [ 0, %10 ]
ret i32 %27
}