#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 */
unsigned char *malloc(int);
int nibbleFromChar(unsigned char);
int strlen(char const *);
unsigned char *Util_string_hex(const char *in) {
  unsigned char *retval;
  unsigned char *p;
  int len, i;
  len = strlen(in) / 2;
  retval = malloc(len + 1);
  for (i = 0, p = (unsigned char *)in; i < len; ++i) {
    retval[i] = (nibbleFromChar(*p) << 4) | nibbleFromChar(*(p + 1));
    p += 2;
  }
  retval[len] = 0;
  return retval;
}


x86 assembly (clang, -O0):
<Util_string_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11c7 <Util_string_hex+0x97>
mov    -0x18(%rbp),%rax
movzbl (%rax),%edi
call   1030 <nibbleFromChar@plt>
shl    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0x1(%rax),%edi
call   1030 <nibbleFromChar@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
or     %ecx,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116e <Util_string_hex+0x3e>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Util_string_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %eax,%r15d
shr    $0x1f,%r15d
add    %eax,%r15d
sar    %r15d
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
cmp    $0x2,%ebp
jl     119a <Util_string_hex+0x6a>
mov    %r15d,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movzbl 0x0(%r13,%rbx,2),%edi
call   1030 <nibbleFromChar@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movzbl 0x1(%r13,%rbx,2),%edi
call   1030 <nibbleFromChar@plt>
or     %ebp,%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1170 <Util_string_hex+0x40>
movslq %r15d,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @Util_string_hex(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = sdiv i32 %8, 2
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 1
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %3
store i32 0, i32* %6
%13 = load i8*, i8** %2
store i8* %13, i8** %4
br label %14
14: ; preds = %35, %1
%15 = load i32, i32* %6
%16 = load i32, i32* %5
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %38
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = call i32 @nibbleFromChar(i8 zeroext %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 @nibbleFromChar(i8 zeroext %25)
%27 = or i32 %22, %26
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %3
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 2
store i8* %34, i8** %4
br label %35
35: ; preds = %18
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %14
38: ; preds = %14
%39 = load i8*, i8** %3
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %3
ret i8* %43
}

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