#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 gdb_atoi_hex(char);
int gdb_gethexword(char **s, unsigned int *val) {
  int count = 0;
  *val = 0;
  while (**s) {
    int hexval = gdb_atoi_hex(**s);
    if (hexval < 0)
      break;
    (*s)++;
    count++;
    *val = (*val << 4) | (hexval & 0xF);
  }
  return count;
}


x86 assembly (clang, -O0):
<gdb_gethexword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
cmpb   $0x0,(%rax)
je     1195 <gdb_gethexword+0x85>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%edi
call   1030 <gdb_atoi_hex@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1162 <gdb_gethexword+0x52>
jmp    1195 <gdb_gethexword+0x85>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
shl    $0x4,%ecx
mov    -0x18(%rbp),%eax
and    $0xf,%eax
or     %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1131 <gdb_gethexword+0x21>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_gethexword>:
push   %rbp
push   %r14
push   %rbx
movl   $0x0,(%rsi)
mov    (%rdi),%rax
mov    (%rax),%al
test   %al,%al
je     1161 <gdb_gethexword+0x51>
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <gdb_atoi_hex@plt>
test   %eax,%eax
js     1163 <gdb_gethexword+0x53>
mov    (%rbx),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%rbx)
add    $0x1,%ebp
mov    (%r14),%edx
shl    $0x4,%edx
and    $0xf,%eax
or     %edx,%eax
mov    %eax,(%r14)
movzbl 0x1(%rcx),%eax
test   %al,%al
jne    1130 <gdb_gethexword+0x20>
jmp    1163 <gdb_gethexword+0x53>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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