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

/* Variables and functions */
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ sscanf(char *, char *, int /*<<< orphan*/ *);
int strlen(int /*<<< orphan*/);
int from_hex(uint8_t *bytes, char *hex) {
  int len = strlen(optarg);
  if (len % 2 != 0) {
    return -1;
  }
  int text_size = len / 2;
  for (int i = 0; i < text_size; i++) {
    sscanf(hex + i * 2, "%2hhx", bytes + i);
  }
  return text_size;
}


x86 assembly (clang, -O0):
<from_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ec1(%rip),%rax
mov    (%rax),%edi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1161 <from_hex+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <from_hex+0xa0>
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11ba <from_hex+0x9a>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe59(%rip),%rsi
call   1040 <sscanf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1176 <from_hex+0x56>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<from_hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ec3(%rip),%rax
mov    (%rax),%edi
call   1030 <strlen@plt>
mov    $0xffffffff,%r15d
test   $0x1,%al
jne    118c <from_hex+0x6c>
mov    %eax,%r15d
shr    $0x1f,%r15d
add    %eax,%r15d
sar    %r15d
cmp    $0x2,%eax
jl     118c <from_hex+0x6c>
mov    %r15d,%ebp
lea    0xe9e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <sscanf@plt>
add    $0x2,%r14
add    $0x4,%rbx
add    $0xffffffffffffffff,%rbp
jne    1170 <from_hex+0x50>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@optarg = global i32 0
@.str = [6 x i8] c"%2hhx\00"
define i32 @from_hex(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = load i32, i32* @optarg
%10 = call i32 @strlen(i32 %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 -1, i32* %3
br label %38
15: ; preds = %2
%16 = load i32, i32* %6
%17 = sdiv i32 %16, 2
store i32 %17, i32* %7
store i32 0, i32* %8
br label %18
18: ; preds = %33, %15
%19 = load i32, i32* %8
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i32, i32* %8
%25 = mul nsw i32 %24, 2
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i32*, i32** %4
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %31)
br label %33
33: ; preds = %22
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %18
36: ; preds = %18
%37 = load i32, i32* %7
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@optarg = global i32 0
@.str = [6 x i8] c"%2hhx\00"
define i32 @from_hex(i32* %0, i8* nocapture %1) {
%3 = load i32, i32* @optarg
%4 = tail call i32 @strlen(i32 %3)
%5 = and i32 %4, 1
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %2
%8 = sdiv i32 %4, 2
%9 = icmp sgt i32 %4, 1
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = zext i32 %8 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %18, %12 ]
%14 = shl nuw nsw i64 %13, 1
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = getelementptr inbounds i32, i32* %0, i64 %13
%17 = tail call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %16)
%18 = add nuw nsw i64 %13, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %20, label %12
20: ; preds = %12, %7, %2
%21 = phi i32 [ -1, %2 ], [ %8, %7 ], [ %8, %12 ]
ret i32 %21
}