#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 */
scalar_t__ smalloc(int);
char *mkhexbuf(unsigned char *buffer, int buflen) {
  static char *hexstr = "0123456789abcdef";
  char *str = (char *)smalloc((buflen << 1) + 1);
  int i;
  for (i = 0; i < buflen; i++) {
    str[i << 1] = hexstr[(int)(buffer[i] >> 4)];
    str[(i << 1) + 1] = hexstr[(int)(buffer[i] & 0x0f)];
  }
  str[i << 1] = '\0';
  return str;
}


x86 assembly (clang, -O0):
<mkhexbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1030 <smalloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ad <mkhexbuf+0x9d>
mov    0x2edd(%rip),%rax
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
movzbl (%rcx,%rdx,1),%ecx
sar    $0x4,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2eb1(%rip),%rax
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
movzbl (%rcx,%rdx,1),%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1138 <mkhexbuf+0x28>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkhexbuf>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r15
lea    (%r14,%r14,1),%edi
add    $0x1,%edi
call   1030 <smalloc@plt>
test   %r14d,%r14d
jle    1192 <mkhexbuf+0x82>
mov    %r14d,%r9d
mov    $0x1,%edx
xor    %esi,%esi
lea    0xec3(%rip),%r10
movabs $0x200000000,%r8
xor    %ecx,%ecx
nopl   0x0(%rax)
movzbl (%r15,%rcx,1),%edi
shr    $0x4,%rdi
movzbl (%rdi,%r10,1),%ebx
mov    %rsi,%rdi
sar    $0x20,%rdi
mov    %bl,(%rax,%rdi,1)
movzbl (%r15,%rcx,1),%edi
and    $0xf,%edi
movzbl (%rdi,%r10,1),%ebx
movslq %edx,%rdx
mov    %bl,(%rax,%rdx,1)
add    $0x1,%rcx
add    $0x2,%edx
add    %r8,%rsi
cmp    %rcx,%r9
jne    1150 <mkhexbuf+0x40>
add    %r14d,%r14d
movslq %r14d,%rcx
jmp    1194 <mkhexbuf+0x84>
xor    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@mkhexbuf.hexstr = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @mkhexbuf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = shl i32 %7, 1
%9 = add nsw i32 %8, 1
%10 = call i64 @smalloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
store i32 0, i32* %6
br label %12
12: ; preds = %50, %2
%13 = load i32, i32* %6
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %53
16: ; preds = %12
%17 = load i8*, i8** @mkhexbuf.hexstr
%18 = load i8*, i8** %3
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = ashr i32 %23, 4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %17, i64 %25
%27 = load i8, i8* %26
%28 = load i8*, i8** %5
%29 = load i32, i32* %6
%30 = shl i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 %27, i8* %32
%33 = load i8*, i8** @mkhexbuf.hexstr
%34 = load i8*, i8** %3
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = and i32 %39, 15
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %33, i64 %41
%43 = load i8, i8* %42
%44 = load i8*, i8** %5
%45 = load i32, i32* %6
%46 = shl i32 %45, 1
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
store i8 %43, i8* %49
br label %50
50: ; preds = %16
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %12
53: ; preds = %12
%54 = load i8*, i8** %5
%55 = load i32, i32* %6
%56 = shl i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %5
ret i8* %59
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @mkhexbuf(i8* nocapture %0, i32 %1) {
%3 = shl i32 %1, 1
%4 = or i32 %3, 1
%5 = tail call i64 @smalloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %36
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %31, %10 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = lshr i8 %13, 4
%15 = zext i8 %14 to i64
%16 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = shl i64 %11, 33
%19 = ashr exact i64 %18, 32
%20 = getelementptr inbounds i8, i8* %6, i64 %19
store i8 %17, i8* %20
%21 = load i8, i8* %12
%22 = and i8 %21, 15
%23 = zext i8 %22 to i64
%24 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = trunc i64 %11 to i32
%27 = shl i32 %26, 1
%28 = or i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %6, i64 %29
store i8 %25, i8* %30
%31 = add nuw nsw i64 %11, 1
%32 = icmp eq i64 %31, %9
br i1 %32, label %33, label %10
33: ; preds = %10
%34 = shl i32 %1, 1
%35 = sext i32 %34 to i64
br label %36
36: ; preds = %2, %33
%37 = phi i64 [ %35, %33 ], [ 0, %2 ]
%38 = getelementptr inbounds i8, i8* %6, i64 %37
store i8 0, i8* %38
ret i8* %6
}