#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_val(char);
int /*<<< orphan*/ is_hex_char(char);
unsigned short hex2int(char *str, unsigned short dfl) {
  int res = 0;
  while (*str) {
    if (!is_hex_char(*str)) {
      return dfl;
    }
    res = (res << 4) | hex_val(*str);
    str++;
  }
  return res;
}


x86 assembly (clang, -O0):
<hex2int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1199 <hex2int+0x79>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <is_hex_char@plt>
cmp    $0x0,%eax
jne    1169 <hex2int+0x49>
mov    -0x12(%rbp),%ax
mov    %ax,-0x2(%rbp)
jmp    11a0 <hex2int+0x80>
mov    -0x18(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <hex_val@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    113a <hex2int+0x1a>
mov    -0x18(%rbp),%eax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
xor    %ebp,%ebp
test   %al,%al
je     1171 <hex2int+0x51>
mov    %esi,%r14d
mov    %rdi,%rbx
add    $0x1,%rbx
nopl   0x0(%rax)
movsbl %al,%edi
call   1030 <is_hex_char@plt>
test   %eax,%eax
je     1174 <hex2int+0x54>
mov    %ebp,%r15d
shl    $0x4,%r15d
movsbl -0x1(%rbx),%edi
call   1040 <hex_val@plt>
mov    %eax,%ebp
or     %r15d,%ebp
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1140 <hex2int+0x20>
mov    %ebp,%r14d
jmp    1174 <hex2int+0x54>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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