#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 strlen(char *);
unsigned char tolower(unsigned char);
unsigned char conv(unsigned char x) {
  int i;
  char *vals = "0123456789abcdef";
  x = tolower(x);
  for (i = 0; i < strlen(vals); i++)
    if (x == vals[i])
      return i;
  return -1;
}


x86 assembly (clang, -O0):
<conv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x2(%rbp)
lea    0xecb(%rip),%rax
mov    %rax,-0x10(%rbp)
movzbl -0x2(%rbp),%edi
call   1040 <tolower@plt>
mov    %al,-0x2(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jge    119e <conv+0x7e>
movzbl -0x2(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x8(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    118b <conv+0x6b>
mov    -0x8(%rbp),%eax
mov    %al,-0x1(%rbp)
jmp    11a2 <conv+0x82>
jmp    1190 <conv+0x70>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    114c <conv+0x2c>
movb   $0xff,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1040 <tolower@plt>
mov    %eax,%r14d
lea    0xec9(%rip),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    $0xff,%r15b
test   %eax,%eax
jle    1172 <conv+0x52>
movzbl %r14b,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
movsbl 0x0(%rbp,%r12,1),%eax
cmp    %eax,%ebx
je     116f <conv+0x4f>
add    $0x1,%rbp
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     1150 <conv+0x30>
jmp    1172 <conv+0x52>
mov    %ebp,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789abcdef\00"
define zeroext i8 @conv(i8 zeroext %0) {
%2 = alloca i8
%3 = alloca i8
%4 = alloca i32
%5 = alloca i8*
store i8 %0, i8* %3
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8, i8* %3
%7 = call zeroext i8 @tolower(i8 zeroext %6)
store i8 %7, i8* %3
store i32 0, i32* %4
br label %8
8: ; preds = %27, %1
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %30
13: ; preds = %8
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = load i8*, i8** %5
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %15, %21
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = load i32, i32* %4
%25 = trunc i32 %24 to i8
store i8 %25, i8* %2
br label %31
26: ; preds = %13
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %8
30: ; preds = %8
store i8 -1, i8* %2
br label %31
31: ; preds = %30, %23
%32 = load i8, i8* %2
ret i8 %32
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define zeroext i8 @conv(i8 zeroext %0) {
%2 = tail call zeroext i8 @tolower(i8 zeroext %0)
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %20
5: ; preds = %1
%6 = zext i8 %2 to i32
br label %7
7: ; preds = %5, %15
%8 = phi i64 [ 0, %5 ], [ %16, %15 ]
%9 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %6, %11
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = trunc i64 %8 to i8
br label %20
15: ; preds = %7
%16 = add nuw nsw i64 %8, 1
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%18 = sext i32 %17 to i64
%19 = icmp slt i64 %16, %18
br i1 %19, label %7, label %20
20: ; preds = %15, %1, %13
%21 = phi i8 [ %14, %13 ], [ -1, %1 ], [ -1, %15 ]
ret i8 %21
}