#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 _get_hex_char(unsigned char, unsigned char);
int HEX64_writeHEX(char *str, unsigned char *buffer, int len,
                   unsigned char caps) {
  int k;
  for (k = 0; k < len; k++) {
    *str = _get_hex_char(buffer[k] >> 4, caps);
    str++;
    *str = _get_hex_char(buffer[k] & 0x0f, caps);
    str++;
  }
  *str = '\0';
  return k * 2;
}


x86 assembly (clang, -O0):
<HEX64_writeHEX>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %al,-0x15(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a7 <HEX64_writeHEX+0x97>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
sar    $0x4,%eax
movzbl %al,%edi
movzbl -0x15(%rbp),%esi
call   1030 <_get_hex_char@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
movzbl %al,%edi
movzbl -0x15(%rbp),%esi
call   1030 <_get_hex_char@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112f <HEX64_writeHEX+0x1f>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HEX64_writeHEX>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
test   %edx,%edx
jle    1182 <HEX64_writeHEX+0x72>
mov    %rsi,%r12
mov    %edx,0x4(%rsp)
mov    %edx,%r13d
xor    %ebp,%ebp
movzbl %cl,%ebx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%r12,%r14,1),%eax
shr    $0x4,%al
movzbl %al,%edi
mov    %ebx,%esi
call   1030 <_get_hex_char@plt>
mov    %al,(%r15,%r14,2)
movzbl (%r12,%r14,1),%edi
and    $0xf,%edi
mov    %ebx,%esi
call   1030 <_get_hex_char@plt>
mov    %al,0x1(%r15,%r14,2)
add    $0x1,%r14
add    $0xfffffffffffffffe,%rbp
cmp    %r14,%r13
jne    1140 <HEX64_writeHEX+0x30>
mov    0x4(%rsp),%eax
add    %eax,%eax
sub    %rbp,%r15
jmp    1184 <HEX64_writeHEX+0x74>
xor    %eax,%eax
movb   $0x0,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @HEX64_writeHEX(i8* %0, i8* %1, i32 %2, i8 zeroext %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8 %3, i8* %8
store i32 0, i32* %9
br label %10
10: ; preds = %41, %4
%11 = load i32, i32* %9
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %44
14: ; preds = %10
%15 = load i8*, i8** %6
%16 = load i32, i32* %9
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = ashr i32 %20, 4
%22 = trunc i32 %21 to i8
%23 = load i8, i8* %8
%24 = call signext i8 @_get_hex_char(i8 zeroext %22, i8 zeroext %23)
%25 = load i8*, i8** %5
store i8 %24, i8* %25
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
%28 = load i8*, i8** %6
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = and i32 %33, 15
%35 = trunc i32 %34 to i8
%36 = load i8, i8* %8
%37 = call signext i8 @_get_hex_char(i8 zeroext %35, i8 zeroext %36)
%38 = load i8*, i8** %5
store i8 %37, i8* %38
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %5
br label %41
41: ; preds = %14
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %10
44: ; preds = %10
%45 = load i8*, i8** %5
store i8 0, i8* %45
%46 = load i32, i32* %9
%47 = mul nsw i32 %46, 2
ret i32 %47
}

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