#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 /*<<< orphan*/ sscanf(char const *, char *, unsigned int *);
int strlen(char const *);
void hex2str(char *dest, const char *src, int slen) {
  const char *p = src;
  int i;
  unsigned int v;
  if (slen == 0)
    slen = strlen(src);
  slen /= 2;
  for (i = 0; i < slen; i++) {
    sscanf(p, "%02x", &v);
    dest[i] = (char)v;
    p += 2;
  }
  dest[slen] = 0;
}


x86 assembly (clang, -O0):
<hex2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1151 <hex2str+0x31>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b0 <hex2str+0x90>
mov    -0x20(%rbp),%rdi
lea    0xe83(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <sscanf@plt>
mov    -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1166 <hex2str+0x46>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   %edx,%edx
jne    113f <hex2str+0x1f>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%edx
mov    %edx,%eax
shr    $0x1f,%eax
add    %edx,%eax
sar    %eax
mov    %eax,(%rsp)
cmp    $0x2,%edx
jl     1194 <hex2str+0x74>
mov    (%rsp),%ebp
xor    %r13d,%r13d
lea    0xea3(%rip),%r15
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <sscanf@plt>
movzbl 0x4(%rsp),%eax
mov    %al,(%r14,%r13,1)
add    $0x2,%rbx
add    $0x1,%r13
cmp    %r13,%rbp
jne    1170 <hex2str+0x50>
movslq (%rsp),%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define void @hex2str(i8* nocapture writeonly %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i32 %2, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @strlen(i8* %1)
br label %9
9: ; preds = %7, %3
%10 = phi i32 [ %8, %7 ], [ %2, %3 ]
%11 = sdiv i32 %10, 2
%12 = icmp sgt i32 %10, 1
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = zext i32 %11 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %23, %15 ]
%17 = phi i8* [ %1, %13 ], [ %22, %15 ]
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %4)
%19 = load i32, i32* %4
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 %20, i8* %21
%22 = getelementptr inbounds i8, i8* %17, i64 2
%23 = add nuw nsw i64 %16, 1
%24 = icmp eq i64 %23, %14
br i1 %24, label %25, label %15
25: ; preds = %15, %9
%26 = sext i32 %11 to i64
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 0, i8* %27
ret void
}