#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 int32_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
char *intToBaseString(int32_t number, int base) {
  char baseDigits[17] = "0123456789abcdef";
  char *buffer = (char *)calloc(sizeof(char), 33);
  if (buffer == NULL) {
    fprintf(stderr, "Memory alloc error\n");
    exit(EXIT_FAILURE);
  }
  int i = 31;
  do {
    *(buffer + i) = baseDigits[number % base];
    number = number / base;
    i--;
  } while (number != 0);
  return buffer + i + 1;
}


x86 assembly (clang, -O0):
<intToBaseString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0xebb(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeb8(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeb6(%rip),%al
mov    %al,-0x10(%rbp)
mov    $0x1,%edi
mov    $0x21,%esi
call   1030 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119e <intToBaseString+0x6e>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
movl   $0x1f,-0x2c(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x8(%rbp)
movslq %edx,%rax
mov    -0x20(%rbp,%rax,1),%dl
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11a5 <intToBaseString+0x75>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intToBaseString>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0x1,%edi
mov    $0x21,%esi
call   1030 <calloc@plt>
test   %rax,%rax
je     119f <intToBaseString+0x6f>
mov    %rax,%r8
lea    0x1f(%rax),%rdi
movabs $0x1f00000000,%rsi
lea    0xe9d(%rip),%r9
movabs $0xffffffff00000000,%rcx
ff ff ff
nopl   (%rax)
mov    %ebp,%eax
cltd
idiv   %ebx
movslq %edx,%rdx
movzbl (%rdx,%r9,1),%edx
mov    %dl,(%rdi)
add    $0xffffffffffffffff,%rdi
add    %rcx,%rsi
mov    %eax,%ebp
test   %eax,%eax
jne    1170 <intToBaseString+0x40>
sar    $0x20,%rsi
lea    (%r8,%rsi,1),%rax
add    $0x1,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@__const.intToBaseString.baseDigits = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32 0
@.str = [20 x i8] c"Memory alloc error\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @intToBaseString(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [17 x i8]
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = bitcast [17 x i8]* %5 to i8*
%9 = call i64 @calloc(i32 1, i32 33)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
store i32 31, i32* %7
br label %19
19: ; preds = %35, %18
%20 = load i32, i32* %3
%21 = load i32, i32* %4
%22 = srem i32 %20, %21
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
%30 = load i32, i32* %3
%31 = load i32, i32* %4
%32 = sdiv i32 %30, %31
store i32 %32, i32* %3
%33 = load i32, i32* %7
%34 = add nsw i32 %33, -1
store i32 %34, i32* %7
br label %35
35: ; preds = %19
%36 = load i32, i32* %3
%37 = icmp ne i32 %36, 0
br i1 %37, label %19, label %38
38: ; preds = %35
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = getelementptr inbounds i8, i8* %42, i64 1
ret i8* %43
}

LLVM IR (-O2):
@__const.intToBaseString.baseDigits = [17 x i8] c"0123456789abcdef\00"
@stderr = global i32 0
@.str = [20 x i8] c"Memory alloc error\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @intToBaseString(i32 %0, i32 %1) {
%3 = tail call i64 @calloc(i32 1, i32 33)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2, %11
%12 = phi i64 [ %20, %11 ], [ 31, %2 ]
%13 = phi i32 [ %19, %11 ], [ %0, %2 ]
%14 = srem i32 %13, %1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.intToBaseString.baseDigits, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = getelementptr inbounds i8, i8* %4, i64 %12
store i8 %17, i8* %18
%19 = sdiv i32 %13, %1
%20 = add i64 %12, -1
%21 = icmp eq i32 %19, 0
br i1 %21, label %22, label %11
22: ; preds = %11
%23 = shl i64 %20, 32
%24 = ashr exact i64 %23, 32
%25 = getelementptr inbounds i8, i8* %4, i64 %24
%26 = getelementptr inbounds i8, i8* %25, i64 1
ret i8* %26
}