#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 */
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *single_digit_hex_to_bin(char);
char *strcat(char *, char *);
int strlen(char *);
char *hex_to_bin(char *hex) {
  char *bin = malloc(strlen(hex) * 4 + 1);
  int i = 0;
  bin[strlen(hex) * 4] = '\0';
  if (false)
    printf("-   `hex_to_bin(%s)`\n", hex);
  for (i = 0; i < strlen(hex); i++) {
    if (false)
      printf("\t-   `hex[%i] = %c -> %s`\n", i, hex[i],
             single_digit_hex_to_bin(hex[i]));
    bin = strcat(bin, single_digit_hex_to_bin(hex[i]));
  }
  return bin;
}


x86 assembly (clang, -O0):
<hex_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
shl    $0x2,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
shl    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    11e8 <hex_to_bin+0xa8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <single_digit_hex_to_bin@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
call   1050 <strcat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1195 <hex_to_bin+0x55>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_bin>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
lea    0x1(,%rax,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,(%r14,%rax,4)
mov    %r15,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11a8 <hex_to_bin+0x68>
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%r15,%rbx,1),%edi
call   1060 <single_digit_hex_to_bin@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <strcat@plt>
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1180 <hex_to_bin+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @hex_to_bin(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl nsw i32 %2, 2
%4 = or i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 @strlen(i8* %0)
%7 = shl nsw i32 %6, 2
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %5, i64 %8
store i8 0, i8* %9
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %1, %12
%13 = phi i64 [ %18, %12 ], [ 0, %1 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = tail call i8* @single_digit_hex_to_bin(i8 signext %15)
%17 = tail call i8* @strcat(i8* dereferenceable(1) %5, i8* dereferenceable(1) %16)
%18 = add nuw nsw i64 %13, 1
%19 = tail call i32 @strlen(i8* %0)
%20 = sext i32 %19 to i64
%21 = icmp slt i64 %18, %20
br i1 %21, label %12, label %22
22: ; preds = %12, %1
ret i8* %5
}