#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 */
typedef int /*<<< orphan*/ hex;

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
unsigned char *char2hex(char *chr, int nLen) {
  int i;
  unsigned char hex[2];
  unsigned char *retHex;
  unsigned int hex3;
  retHex = (unsigned char *)malloc(nLen);
  memset(retHex, 0, nLen);
  for (i = 0; i < nLen; i++) {
    memset(hex, 0, sizeof(hex));
    memcpy(hex, chr + i, 2);
    sscanf((char *)hex, "%2x", &hex3);
    retHex[i / 2] = hex3;
    i++;
  }
  return retHex;
}


x86 assembly (clang, -O0):
<char2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f8 <char2hex+0xb8>
lea    -0x12(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
lea    -0x12(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x12(%rbp),%rdi
lea    0xe50(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x24(%rbp),%eax
mov    %al,-0x31(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x31(%rbp),%dl
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1170 <char2hex+0x30>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<char2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
call   1050 <malloc@plt>
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <memset@plt>
test   %ebp,%ebp
jle    11d8 <char2hex+0x98>
xor    %r15d,%r15d
lea    0xe(%rsp),%rbx
lea    0x8(%rsp),%r13
mov    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
lea    (%r14,%r15,1),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
lea    0xe46(%rip),%rsi
mov    %r13,%rdx
call   1060 <sscanf@plt>
movzbl 0x8(%rsp),%eax
mov    %al,(%r12)
add    $0x2,%r15
add    $0x1,%r12
cmp    %ebp,%r15d
jl     1190 <char2hex+0x50>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%2x\00"
define i8* @char2hex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i8]
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = load i32, i32* %4
%14 = call i32 @memset(i8* %12, i32 0, i32 %13)
store i32 0, i32* %5
br label %15
15: ; preds = %39, %2
%16 = load i32, i32* %5
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %42
19: ; preds = %15
%20 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 2)
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = call i32 @memcpy(i8* %22, i8* %26, i32 2)
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %8)
%30 = load i32, i32* %8
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %7
%33 = load i32, i32* %5
%34 = sdiv i32 %33, 2
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 %31, i8* %36
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %39
39: ; preds = %19
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %15
42: ; preds = %15
%43 = load i8*, i8** %7
ret i8* %43
}

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
define i8* @char2hex(i8* %0, i32 %1) {
%3 = alloca [2 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = tail call i64 @malloc(i32 %1)
%8 = inttoptr i64 %7 to i8*
%9 = tail call i32 @memset(i8* %8, i32 0, i32 %1)
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %24
11: ; preds = %2, %11
%12 = phi i64 [ %21, %11 ], [ 0, %2 ]
%13 = call i32 @memset(i8* %5, i32 0, i32 2)
%14 = getelementptr inbounds i8, i8* %0, i64 %12
%15 = call i32 @memcpy(i8* %5, i8* %14, i32 2)
%16 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%17 = load i32, i32* %4
%18 = trunc i32 %17 to i8
%19 = lshr exact i64 %12, 1
%20 = getelementptr inbounds i8, i8* %8, i64 %19
store i8 %18, i8* %20
%21 = add nuw i64 %12, 2
%22 = trunc i64 %21 to i32
%23 = icmp slt i32 %22, %1
br i1 %23, label %11, label %24
24: ; preds = %11, %2
ret i8* %8
}