#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;
    if (!hex[0])
      return -1;
    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    11c0 <get_sha1_hex+0xb0>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    114a <get_sha1_hex+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <get_sha1_hex+0xb7>
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     1190 <get_sha1_hex+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <get_sha1_hex+0xb7>
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   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
mov    $0xffffffff,%r12d
cs nopw 0x0(%rax,%rax,1)
movzbl (%r15,%rbp,2),%eax
test   %al,%al
je     116a <get_sha1_hex+0x5a>
movsbl %al,%edi
call   1030 <hexval@plt>
mov    %eax,%ebx
shl    $0x4,%ebx
movsbl 0x1(%r15,%rbp,2),%edi
call   1030 <hexval@plt>
or     %ebx,%eax
cmp    $0xff,%eax
ja     116a <get_sha1_hex+0x5a>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    $0x14,%ebp
jne    1130 <get_sha1_hex+0x20>
xor    %r12d,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
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 = %39, %2
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 20
br i1 %10, label %11, label %42
11: ; preds = %8
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %11
store i32 -1, i32* %3
br label %43
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = call i32 @hexval(i8 signext %20)
%22 = shl i32 %21, 4
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = load i8, i8* %24
%26 = call i32 @hexval(i8 signext %25)
%27 = or i32 %22, %26
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = and i32 %28, -256
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %17
store i32 -1, i32* %3
br label %43
32: ; preds = %17
%33 = load i32, i32* %7
%34 = trunc i32 %33 to i8
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %5
store i8 %34, i8* %35
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 2
store i8* %38, i8** %4
br label %39
39: ; preds = %32
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %8
42: ; preds = %8
store i32 0, i32* %3
br label %43
43: ; preds = %42, %31, %16
%44 = load i32, i32* %3
ret i32 %44
}

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