#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*/ printf(char *, ...);
void printHex(unsigned char msg[], unsigned char length) {
  printf("0x");
  for (unsigned char i = 0; i < length; i++) {
    if (msg[i] > 0x0f) {
      printf("%x", msg[i]);
    } else {
      printf("0%x", msg[i]);
    }
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<printHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    0xed7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movb   $0x0,-0xa(%rbp)
movzbl -0xa(%rbp),%eax
movzbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jge    11a4 <printHex+0x94>
mov    -0x8(%rbp),%rax
movzbl -0xa(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0xf,%eax
jle    1178 <printHex+0x68>
mov    -0x8(%rbp),%rax
movzbl -0xa(%rbp),%ecx
movzbl (%rax,%rcx,1),%esi
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1192 <printHex+0x82>
mov    -0x8(%rbp),%rax
movzbl -0xa(%rbp),%ecx
movzbl (%rax,%rcx,1),%esi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1197 <printHex+0x87>
mov    -0xa(%rbp),%al
add    $0x1,%al
mov    %al,-0xa(%rbp)
jmp    1134 <printHex+0x24>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printHex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r12
lea    0xecc(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
je     117f <printHex+0x5f>
movzbl %bpl,%ebp
lea    0xeba(%rip),%r14
lea    0xeb2(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%r12,%rbx,1),%esi
cmp    $0x10,%esi
mov    %r15,%rdi
cmovae %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1160 <printHex+0x40>
mov    $0xa,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [4 x i8] c"0%x\00"
define void @printHex(i8* nocapture %0, i8 zeroext %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8 %1, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = zext i8 %1 to i64
br label %9
7: ; preds = %9, %2
%8 = tail call i32 @putchar(i32 10)
ret void
9: ; preds = %5, %9
%10 = phi i64 [ 0, %5 ], [ %17, %9 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = icmp ugt i8 %12, 15
%15 = select i1 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %15, i32 %13)
%17 = add nuw nsw i64 %10, 1
%18 = icmp eq i64 %17, %6
br i1 %18, label %7, label %9
}