#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 SHA_DIGEST_LENGTH;
int htoi(unsigned char);
scalar_t__ malloc(int);
unsigned char *hexastr2binary(unsigned char *sha1hexadigest) {
  unsigned char *output =
      (unsigned char *)malloc(sizeof(unsigned char) * SHA_DIGEST_LENGTH);
  int i = 0;
  for (i = SHA_DIGEST_LENGTH - 1; i >= 0; i--) {
    output[i] = htoi(sha1hexadigest[i * 2 + 1]);
    output[i] |= htoi(sha1hexadigest[i * 2]) << 4;
  }
  return output;
}


x86 assembly (clang, -O0):
<hexastr2binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2eb5(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11c5 <hexastr2binary+0xa5>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <htoi@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <htoi@plt>
mov    %eax,%esi
shl    $0x4,%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    115b <hexastr2binary+0x3b>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexastr2binary>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2eb6(%rip),%rbx
mov    (%rbx),%edi
call   1030 <malloc@plt>
mov    %rax,%r15
mov    (%rbx),%eax
test   %eax,%eax
jle    118b <hexastr2binary+0x6b>
mov    %rax,%rbx
add    $0x1,%rbx
lea    (%rax,%rax,1),%ebp
add    $0xffffffff,%ebp
nop
movslq %ebp,%r12
add    $0xffffffff,%ebp
movzbl (%r14,%r12,1),%edi
call   1040 <htoi@plt>
mov    %al,-0x2(%r15,%rbx,1)
movslq %ebp,%rax
movzbl (%r14,%rax,1),%edi
call   1040 <htoi@plt>
shl    $0x4,%al
or     %al,-0x2(%r15,%rbx,1)
add    $0xffffffffffffffff,%rbx
add    $0xfffffffe,%r12d
mov    %r12d,%ebp
cmp    $0x1,%rbx
ja     1150 <hexastr2binary+0x30>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
define i8* @hexastr2binary(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @SHA_DIGEST_LENGTH
%6 = sext i32 %5 to i64
%7 = mul i64 1, %6
%8 = trunc i64 %7 to i32
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
store i32 0, i32* %4
%11 = load i32, i32* @SHA_DIGEST_LENGTH
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %4
br label %13
13: ; preds = %46, %1
%14 = load i32, i32* %4
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %49
16: ; preds = %13
%17 = load i8*, i8** %2
%18 = load i32, i32* %4
%19 = mul nsw i32 %18, 2
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %17, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @htoi(i8 zeroext %23)
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
%30 = load i8*, i8** %2
%31 = load i32, i32* %4
%32 = mul nsw i32 %31, 2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = load i8, i8* %34
%36 = call i32 @htoi(i8 zeroext %35)
%37 = shl i32 %36, 4
%38 = load i8*, i8** %3
%39 = load i32, i32* %4
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = or i32 %43, %37
%45 = trunc i32 %44 to i8
store i8 %45, i8* %41
br label %46
46: ; preds = %16
%47 = load i32, i32* %4
%48 = add nsw i32 %47, -1
store i32 %48, i32* %4
br label %13
49: ; preds = %13
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
define i8* @hexastr2binary(i8* nocapture %0) {
%2 = load i32, i32* @SHA_DIGEST_LENGTH
%3 = tail call i64 @malloc(i32 %2)
%4 = inttoptr i64 %3 to i8*
%5 = load i32, i32* @SHA_DIGEST_LENGTH
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %30
7: ; preds = %1
%8 = zext i32 %5 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ %8, %7 ], [ %11, %9 ]
%11 = add nsw i64 %10, -1
%12 = trunc i64 %11 to i32
%13 = shl nuw nsw i32 %12, 1
%14 = or i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call i32 @htoi(i8 zeroext %17)
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds i8, i8* %4, i64 %11
store i8 %19, i8* %20
%21 = sext i32 %13 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = load i8, i8* %22
%24 = tail call i32 @htoi(i8 zeroext %23)
%25 = load i8, i8* %20
%26 = trunc i32 %24 to i8
%27 = shl i8 %26, 4
%28 = or i8 %27, %25
store i8 %28, i8* %20
%29 = icmp ugt i64 %10, 1
br i1 %29, label %9, label %30
30: ; preds = %9, %1
ret i8* %4
}