#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 */
scalar_t__ xtoi(char);
int get_16bit_hexnum(char *s) {
  int i;
  unsigned int hexdigit;
  int out = 0;
  for (i = 0; i < 4; i++) {
    if ((hexdigit = (unsigned int)xtoi(*(s + i))) == -1)
      return -1;
    out |= (int)hexdigit << (12 - (4 * i));
  }
  return out;
}


x86 assembly (clang, -O0):
<get_16bit_hexnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    1183 <get_16bit_hexnum+0x73>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <xtoi@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    115d <get_16bit_hexnum+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1189 <get_16bit_hexnum+0x79>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%edx
shl    $0x2,%edx
mov    $0xc,%ecx
sub    %edx,%ecx
shl    %cl,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    112a <get_16bit_hexnum+0x1a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_16bit_hexnum>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
movsbl (%rdi),%edi
call   1030 <xtoi@plt>
mov    %rax,%rbx
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%ebx
je     1179 <get_16bit_hexnum+0x69>
movsbl 0x1(%rbp),%edi
call   1030 <xtoi@plt>
mov    %rax,%r14
cmp    $0xffffffff,%r14d
je     1179 <get_16bit_hexnum+0x69>
movsbl 0x2(%rbp),%edi
call   1030 <xtoi@plt>
mov    %rax,%r12
cmp    $0xffffffff,%r12d
je     1179 <get_16bit_hexnum+0x69>
movsbl 0x3(%rbp),%edi
call   1030 <xtoi@plt>
cmp    $0xffffffff,%eax
je     1179 <get_16bit_hexnum+0x69>
shl    $0x4,%r12d
shl    $0x8,%r14d
shl    $0xc,%ebx
or     %r14d,%ebx
or     %r12d,%ebx
or     %eax,%ebx
mov    %ebx,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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