#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*/ abort();
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, ...);
const char *hexbuf(unsigned char buf[], unsigned int buflen) {
  static char msg[50];
  switch (buflen) {
  case 1:
    sprintf(msg, "0x%02X", buf[0]);
    break;
  case 2:
    sprintf(msg, "0x%02X%02X", buf[0], buf[1]);
    break;
  case 3:
    sprintf(msg, "0x%02X%02X%02X", buf[0], buf[1], buf[2]);
    break;
  case 4:
    sprintf(msg, "0x%02X%02X%02X%02X", buf[0], buf[1], buf[2], buf[3]);
    break;
  default:
    abort();
  }
  return msg;
}


x86 assembly (clang, -O0):
<hexbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x3,%eax
ja     1215 <hexbuf+0xf5>
mov    -0x18(%rbp),%rax
lea    0xeb1(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%rax
lea    0x2edd(%rip),%rdi
lea    0xea6(%rip),%rsi
movzbl (%rax),%edx
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    121c <hexbuf+0xfc>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
lea    0x2eb2(%rip),%rdi
lea    0xe82(%rip),%rsi
movzbl %al,%edx
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    121c <hexbuf+0xfc>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
mov    -0x8(%rbp),%rdx
movzbl 0x2(%rdx),%r8d
lea    0x2e7e(%rip),%rdi
lea    0xe59(%rip),%rsi
movzbl %al,%edx
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    121c <hexbuf+0xfc>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
mov    -0x8(%rbp),%rdx
movzbl 0x2(%rdx),%r8d
mov    -0x8(%rbp),%rdx
movzbl 0x3(%rdx),%r9d
lea    0x2e41(%rip),%rdi
lea    0xe2b(%rip),%rsi
movzbl %al,%edx
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    121c <hexbuf+0xfc>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0x2e1d(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexbuf>:
push   %rax
add    $0xffffffff,%esi
cmp    $0x3,%esi
ja     11c7 <hexbuf+0xa7>
lea    0xecc(%rip),%rax
movslq (%rax,%rsi,4),%rcx
add    %rax,%rcx
jmp    *%rcx
movzbl (%rdi),%edx
lea    0x2ef9(%rip),%rdi
lea    0xec2(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    11be <hexbuf+0x9e>
movzbl (%rdi),%edx
movzbl 0x1(%rdi),%ecx
movzbl 0x2(%rdi),%r8d
lea    0x2ed6(%rip),%rdi
lea    0xeb1(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    11be <hexbuf+0x9e>
movzbl (%rdi),%edx
movzbl 0x1(%rdi),%ecx
lea    0x2eb8(%rip),%rdi
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    11be <hexbuf+0x9e>
movzbl (%rdi),%edx
movzbl 0x1(%rdi),%ecx
movzbl 0x2(%rdi),%r8d
movzbl 0x3(%rdi),%r9d
lea    0x2e90(%rip),%rdi
lea    0xe7a(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
lea    0x2e7b(%rip),%rax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@hexbuf.msg = internal global [50 x i8] zeroinitializer
@.str = [7 x i8] c"0x%02X\00"
@.str.1 = [11 x i8] c"0x%02X%02X\00"
@.str.2 = [15 x i8] c"0x%02X%02X%02X\00"
@.str.3 = [19 x i8] c"0x%02X%02X%02X%02X\00"
define i8* @hexbuf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
switch i32 %5, label %50 [
i32 1, label %6
i32 2, label %11
i32 3, label %20
i32 4, label %33
]
6: ; preds = %2
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %9)
br label %52
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %14, i32 %18)
br label %52
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 2
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %23, i32 %27, i32 %31)
br label %52
33: ; preds = %2
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i64 2
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i64 3
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %36, i32 %40, i32 %44, i32 %48)
br label %52
50: ; preds = %2
%51 = call i32 (...) @abort()
unreachable
52: ; preds = %33, %20, %11, %6
ret i8* getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0)
}

LLVM IR (-O2):
@hexbuf.msg = internal global [50 x i8] zeroinitializer
@.str = [7 x i8] c"0x%02X\00"
@.str.1 = [11 x i8] c"0x%02X%02X\00"
@.str.2 = [15 x i8] c"0x%02X%02X%02X\00"
@.str.3 = [19 x i8] c"0x%02X%02X%02X%02X\00"
define i8* @hexbuf(i8* nocapture %0, i32 %1) {
switch i32 %1, label %33 [
i32 1, label %3
i32 2, label %6
i32 3, label %12
i32 4, label %21
]
3: ; preds = %2
%4 = load i8, i8* %0
%5 = tail call i32 (i8*, i8*, i8, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %4)
br label %35
6: ; preds = %2
%7 = load i8, i8* %0
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = tail call i32 (i8*, i8*, i8, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %7, i32 %10)
br label %35
12: ; preds = %2
%13 = load i8, i8* %0
%14 = getelementptr inbounds i8, i8* %0, i64 1
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = getelementptr inbounds i8, i8* %0, i64 2
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = tail call i32 (i8*, i8*, i8, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %13, i32 %16, i32 %19)
br label %35
21: ; preds = %2
%22 = load i8, i8* %0
%23 = getelementptr inbounds i8, i8* %0, i64 1
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = getelementptr inbounds i8, i8* %0, i64 2
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = getelementptr inbounds i8, i8* %0, i64 3
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = tail call i32 (i8*, i8*, i8, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %22, i32 %25, i32 %28, i32 %31)
br label %35
33: ; preds = %2
%34 = tail call i32 (...) @abort()
unreachable
35: ; preds = %21, %12, %6, %3
ret i8* getelementptr inbounds ([50 x i8], [50 x i8]* @hexbuf.msg, i64 0, i64 0)
}