#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 hexval(char const);
int get_sha1_hex(const char *hex, unsigned char *sha1) {
  int i;
  for (i = 0; i < 20; i++) {
    unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
    if (val & ~0xff)
      return -1;
    *sha1++ = val;
    hex += 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_sha1_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x14,-0x1c(%rbp)
jge    11a7 <get_sha1_hex+0x97>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <hexval@plt>
shl    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1030 <hexval@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xffffff00,%eax
cmp    $0x0,%eax
je     1177 <get_sha1_hex+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <get_sha1_hex+0x9e>
mov    -0x20(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1127 <get_sha1_hex+0x17>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sha1_hex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
nop
movsbl (%r15,%rbx,2),%edi
call   1030 <hexval@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movsbl 0x1(%r15,%rbx,2),%edi
call   1030 <hexval@plt>
or     %ebp,%eax
cmp    $0xff,%eax
ja     1154 <get_sha1_hex+0x44>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0x14,%ebx
jne    1120 <get_sha1_hex+0x10>
xor    %eax,%eax
jmp    1159 <get_sha1_hex+0x49>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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