#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 *calloc(int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strcat(char *, char *);
int strlen(char *);
char *int2strhex(int num, int n) {
  char *buffer, *tmp;
  int i, len, max = 20;
  buffer = calloc(max, sizeof(char));
  snprintf(buffer, max * sizeof(char), "%X", num);
  len = strlen(buffer);
  if (len < n) {
    tmp = calloc(n + 1, sizeof(char));
    for (i = 0; i < n - len; i++)
      tmp[i] = '0';
    buffer = strcat(tmp, buffer);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<int2strhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x14,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xe82(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11f2 <int2strhex+0xb2>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jge    11e1 <int2strhex+0xa1>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x30,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b6 <int2strhex+0x76>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int2strhex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebx
mov    $0x14,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
lea    0xe8a(%rip),%rdx
mov    %rax,%rdi
mov    $0x14,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    %r14d,%eax
jge    11cc <int2strhex+0x7c>
mov    %eax,%ebx
lea    0x1(%r14),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
not    %ebx
add    %r14d,%ebx
add    $0x1,%rbx
mov    %rax,%rdi
mov    $0x30,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    %r12,%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%X\00"
define i8* @int2strhex(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 20, i32* %9
%10 = load i32, i32* %9
%11 = call i8* @calloc(i32 %10, i32 1)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = load i32, i32* %9
%14 = sext i32 %13 to i64
%15 = mul i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = load i32, i32* %3
%18 = call i32 @snprintf(i8* %12, i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %46
24: ; preds = %2
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
%27 = call i8* @calloc(i32 %26, i32 1)
store i8* %27, i8** %6
store i32 0, i32* %7
br label %28
28: ; preds = %39, %24
%29 = load i32, i32* %7
%30 = load i32, i32* %4
%31 = load i32, i32* %8
%32 = sub nsw i32 %30, %31
%33 = icmp slt i32 %29, %32
br i1 %33, label %34, label %42
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 48, i8* %38
br label %39
39: ; preds = %34
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %28
42: ; preds = %28
%43 = load i8*, i8** %6
%44 = load i8*, i8** %5
%45 = call i8* @strcat(i8* %43, i8* %44)
store i8* %45, i8** %5
br label %46
46: ; preds = %42, %2
%47 = load i8*, i8** %5
ret i8* %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"%X\00"
define i8* @int2strhex(i32 %0, i32 %1) {
%3 = tail call dereferenceable_or_null(20) i8* @calloc(i32 20, i32 1)
%4 = tail call i32 @snprintf(i8* %3, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = tail call i32 @strlen(i8* %3)
%6 = icmp slt i32 %5, %1
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = add nsw i32 %1, 1
%9 = tail call i8* @calloc(i32 %8, i32 1)
%10 = xor i32 %5, -1
%11 = add i32 %10, %1
%12 = zext i32 %11 to i64
%13 = add nuw nsw i64 %12, 1
%14 = tail call i8* @strcat(i8* dereferenceable(1) %9, i8* dereferenceable(1) %3)
br label %15
15: ; preds = %7, %2
%16 = phi i8* [ %9, %7 ], [ %3, %2 ]
ret i8* %16
}