#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 hex_to_val(char);
scalar_t__ isxdigit(char);
int parse_hex(char **input) {
  int rval = 0;
  if (isxdigit(**input)) {
    rval = hex_to_val(**input);
    (*input)++;
  } else {
    return -1;
  }
  if (isxdigit(**input)) {
    rval = (rval << 4) | hex_to_val(**input);
    (*input)++;
  }
  return rval & 0xFF;
}


x86 assembly (clang, -O0):
<parse_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%edi
call   1040 <isxdigit@plt>
cmp    $0x0,%rax
je     1171 <parse_hex+0x51>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%edi
call   1030 <hex_to_val@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    117d <parse_hex+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <parse_hex+0xb1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%edi
call   1040 <isxdigit@plt>
cmp    $0x0,%rax
je     11c6 <parse_hex+0xa6>
mov    -0x14(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%edi
call   1030 <hex_to_val@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hex>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%rax
movsbl (%rax),%edi
call   1040 <isxdigit@plt>
test   %rax,%rax
je     1175 <parse_hex+0x55>
mov    (%rbx),%rax
movsbl (%rax),%edi
call   1030 <hex_to_val@plt>
mov    %eax,%ebp
mov    (%rbx),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%rbx)
movsbl 0x1(%rax),%edi
call   1040 <isxdigit@plt>
test   %rax,%rax
je     116f <parse_hex+0x4f>
shl    $0x4,%ebp
mov    (%rbx),%rax
movsbl (%rax),%edi
call   1030 <hex_to_val@plt>
or     %eax,%ebp
addq   $0x1,(%rbx)
movzbl %bpl,%eax
jmp    117a <parse_hex+0x5a>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @parse_hex(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = load i8, i8* %2
%4 = tail call i64 @isxdigit(i8 signext %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = load i8, i8* %7
%9 = tail call i32 @hex_to_val(i8 signext %8)
%10 = load i8*, i8** %0
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8* %11, i8** %0
%12 = load i8, i8* %11
%13 = tail call i64 @isxdigit(i8 signext %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %6
%16 = shl i32 %9, 4
%17 = load i8*, i8** %0
%18 = load i8, i8* %17
%19 = tail call i32 @hex_to_val(i8 signext %18)
%20 = or i32 %19, %16
%21 = load i8*, i8** %0
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** %0
br label %23
23: ; preds = %15, %6
%24 = phi i32 [ %20, %15 ], [ %9, %6 ]
%25 = and i32 %24, 255
br label %26
26: ; preds = %1, %23
%27 = phi i32 [ %25, %23 ], [ -1, %1 ]
ret i32 %27
}