#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 util_hex_to_nib(char);
unsigned char util_hex_to_byte(char *p) {
  int val;
  unsigned char retval;
  val = util_hex_to_nib(*p);
  if (val < 0)
    return 0;
  retval = (unsigned char)val;
  val = util_hex_to_nib(*(++p));
  if (val < 0)
    return retval;
  retval <<= 4;
  retval |= (unsigned char)(val & 0x0f);
  return retval;
}


x86 assembly (clang, -O0):
<util_hex_to_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <util_hex_to_nib@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    113e <util_hex_to_byte+0x2e>
movb   $0x0,-0x1(%rbp)
jmp    1196 <util_hex_to_byte+0x86>
mov    -0x14(%rbp),%eax
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl 0x1(%rax),%edi
call   1030 <util_hex_to_nib@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1174 <util_hex_to_byte+0x64>
mov    -0x15(%rbp),%al
mov    %al,-0x1(%rbp)
jmp    1196 <util_hex_to_byte+0x86>
movzbl -0x15(%rbp),%eax
shl    $0x4,%eax
mov    %al,-0x15(%rbp)
mov    -0x14(%rbp),%eax
and    $0xf,%eax
movzbl %al,%ecx
movzbl -0x15(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_hex_to_byte>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movsbl (%rdi),%edi
call   1030 <util_hex_to_nib@plt>
test   %eax,%eax
js     113f <util_hex_to_byte+0x2f>
mov    %eax,%ebx
movsbl 0x1(%r14),%edi
call   1030 <util_hex_to_nib@plt>
test   %eax,%eax
js     1141 <util_hex_to_byte+0x31>
shl    $0x4,%ebx
and    $0xf,%eax
or     %ebx,%eax
mov    %eax,%ebx
jmp    1141 <util_hex_to_byte+0x31>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define zeroext i8 @util_hex_to_byte(i8* %0) {
%2 = alloca i8
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = call i32 @util_hex_to_nib(i8 signext %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i8 0, i8* %2
br label %37
12: ; preds = %1
%13 = load i32, i32* %4
%14 = trunc i32 %13 to i8
store i8 %14, i8* %5
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %3
%17 = load i8, i8* %16
%18 = call i32 @util_hex_to_nib(i8 signext %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = load i8, i8* %5
store i8 %22, i8* %2
br label %37
23: ; preds = %12
%24 = load i8, i8* %5
%25 = zext i8 %24 to i32
%26 = shl i32 %25, 4
%27 = trunc i32 %26 to i8
store i8 %27, i8* %5
%28 = load i32, i32* %4
%29 = and i32 %28, 15
%30 = trunc i32 %29 to i8
%31 = zext i8 %30 to i32
%32 = load i8, i8* %5
%33 = zext i8 %32 to i32
%34 = or i32 %33, %31
%35 = trunc i32 %34 to i8
store i8 %35, i8* %5
%36 = load i8, i8* %5
store i8 %36, i8* %2
br label %37
37: ; preds = %23, %21, %11
%38 = load i8, i8* %2
ret i8 %38
}

LLVM IR (-O2):
define zeroext i8 @util_hex_to_byte(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = tail call i32 @util_hex_to_nib(i8 signext %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = getelementptr inbounds i8, i8* %0, i64 1
%7 = load i8, i8* %6
%8 = tail call i32 @util_hex_to_nib(i8 signext %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = trunc i32 %3 to i8
br label %17
12: ; preds = %5
%13 = shl i32 %3, 4
%14 = and i32 %8, 15
%15 = or i32 %14, %13
%16 = trunc i32 %15 to i8
br label %17
17: ; preds = %1, %12, %10
%18 = phi i8 [ %11, %10 ], [ %16, %12 ], [ 0, %1 ]
ret i8 %18
}