#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 */
typedef int u_int8_t;
typedef int u_int16_t;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, ...);
void int2asciihex(int val, char *str, int width) {
  if (width == 1)
    sprintf(str, "%.1X", (u_int8_t)val);
  else if (width == 2)
    sprintf(str, "%.2X", (u_int8_t)val);
  else if (width == 4)
    sprintf(str, "%.4X", (u_int16_t)val);
  else
    sprintf(str, "Err");
}


x86 assembly (clang, -O0):
<int2asciihex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1146 <int2asciihex+0x36>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xec6(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11aa <int2asciihex+0x9a>
cmpl   $0x2,-0x14(%rbp)
jne    116a <int2asciihex+0x5a>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xea7(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11a5 <int2asciihex+0x95>
cmpl   $0x4,-0x14(%rbp)
jne    118e <int2asciihex+0x7e>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe88(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11a0 <int2asciihex+0x90>
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11a5 <int2asciihex+0x95>
jmp    11aa <int2asciihex+0x9a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int2asciihex>:
mov    %edi,%eax
cmp    $0x4,%edx
je     112a <int2asciihex+0x1a>
cmp    $0x2,%edx
je     1133 <int2asciihex+0x23>
cmp    $0x1,%edx
jne    1149 <int2asciihex+0x39>
lea    0xed8(%rip),%rcx
jmp    113a <int2asciihex+0x2a>
lea    0xed9(%rip),%rcx
jmp    113a <int2asciihex+0x2a>
lea    0xecb(%rip),%rcx
mov    %rsi,%rdi
mov    %rcx,%rsi
mov    %eax,%edx
xor    %eax,%eax
jmp    1030 <sprintf@plt>
movl   $0x727245,(%rsi)
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%.1X\00"
@.str.1 = [5 x i8] c"%.2X\00"
@.str.2 = [5 x i8] c"%.4X\00"
@.str.3 = [4 x i8] c"Err\00"
define void @int2asciihex(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i8*, i8** %5
%11 = load i32, i32* %4
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %32
13: ; preds = %3
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i8*, i8** %5
%18 = load i32, i32* %4
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %31
20: ; preds = %13
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 4
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = load i32, i32* %4
%26 = call i32 (i8*, i8*, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %30
27: ; preds = %20
%28 = load i8*, i8** %5
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %27, %23
br label %31
31: ; preds = %30, %16
br label %32
32: ; preds = %31, %9
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%.1X\00"
@.str.1 = [5 x i8] c"%.2X\00"
@.str.2 = [5 x i8] c"%.4X\00"
define void @int2asciihex(i32 %0, i8* nocapture writeonly %1, i32 %2) {
switch i32 %2, label %10 [
i32 1, label %4
i32 2, label %6
i32 4, label %8
]
4: ; preds = %3
%5 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %12
6: ; preds = %3
%7 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %12
8: ; preds = %3
%9 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %12
10: ; preds = %3
%11 = bitcast i8* %1 to i32*
store i32 7500357, i32* %11
br label %12
12: ; preds = %6, %10, %8, %4
ret void
}