#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ remove_zeros(char *);
int /*<<< orphan*/ strcpy(char *, char *);
void num_to_str(unsigned int n, unsigned int base, char *output) {
  char buf[33];
  memset(buf, '0', sizeof(buf));
  *(buf + 32) = '\0';
  unsigned int ind = 31;
  do {
    char c = n % base;
    if (c > 9) {
      c = c - 10 + 'A';
    } else {
      c = c + '0';
    }
    buf[ind--] = c;
    n = n / base;
  } while (n > 0);
  remove_zeros(buf);
  strcpy(output, buf);
}


x86 assembly (clang, -O0):
<num_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x30,%esi
mov    $0x21,%edx
call   1050 <memset@plt>
movb   $0x0,-0x20(%rbp)
movl   $0x1f,-0x44(%rbp)
mov    -0x4(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %dl,%al
mov    %al,-0x45(%rbp)
movsbl -0x45(%rbp),%eax
cmp    $0x9,%eax
jle    118c <num_to_str+0x5c>
movsbl -0x45(%rbp),%eax
sub    $0xa,%eax
add    $0x41,%eax
mov    %al,-0x45(%rbp)
jmp    1196 <num_to_str+0x66>
movsbl -0x45(%rbp),%eax
add    $0x30,%eax
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%cl
mov    -0x44(%rbp),%eax
mov    %eax,%edx
add    $0xffffffff,%edx
mov    %edx,-0x44(%rbp)
mov    %eax,%eax
mov    %cl,-0x40(%rbp,%rax,1)
mov    -0x4(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
ja     1160 <num_to_str+0x30>
lea    -0x40(%rbp),%rdi
call   1040 <remove_zeros@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_to_str>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rdi
mov    $0x30,%esi
mov    $0x21,%edx
call   1050 <memset@plt>
movb   $0x0,0x20(%rsp)
mov    $0x1f,%ecx
mov    $0x37,%esi
mov    %ebx,%eax
xor    %edx,%edx
div    %ebp
cmp    $0xa,%dl
mov    $0x30,%edi
cmovge %esi,%edi
add    %dl,%dil
mov    %ecx,%edx
add    $0xffffffff,%ecx
mov    %dil,(%rsp,%rdx,1)
cmp    %ebp,%ebx
mov    %eax,%ebx
jae    1160 <num_to_str+0x30>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <remove_zeros@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @num_to_str(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [33 x i8]
%8 = alloca i32
%9 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 48, i32 33)
%12 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%13 = getelementptr inbounds i8, i8* %12, i64 32
store i8 0, i8* %13
store i32 31, i32* %8
br label %14
14: ; preds = %42, %3
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = urem i32 %15, %16
%18 = trunc i32 %17 to i8
store i8 %18, i8* %9
%19 = load i8, i8* %9
%20 = sext i8 %19 to i32
%21 = icmp sgt i32 %20, 9
br i1 %21, label %22, label %28
22: ; preds = %14
%23 = load i8, i8* %9
%24 = sext i8 %23 to i32
%25 = sub nsw i32 %24, 10
%26 = add nsw i32 %25, 65
%27 = trunc i32 %26 to i8
store i8 %27, i8* %9
br label %33
28: ; preds = %14
%29 = load i8, i8* %9
%30 = sext i8 %29 to i32
%31 = add nsw i32 %30, 48
%32 = trunc i32 %31 to i8
store i8 %32, i8* %9
br label %33
33: ; preds = %28, %22
%34 = load i8, i8* %9
%35 = load i32, i32* %8
%36 = add i32 %35, -1
store i32 %36, i32* %8
%37 = zext i32 %35 to i64
%38 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 %37
store i8 %34, i8* %38
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = udiv i32 %39, %40
store i32 %41, i32* %4
br label %42
42: ; preds = %33
%43 = load i32, i32* %4
%44 = icmp ugt i32 %43, 0
br i1 %44, label %14, label %45
45: ; preds = %42
%46 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%47 = call i32 @remove_zeros(i8* %46)
%48 = load i8*, i8** %6
%49 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%50 = call i32 @strcpy(i8* %48, i8* %49)
ret void
}

LLVM IR (-O2):
define void @num_to_str(i32 %0, i32 %1, i8* %2) {
%4 = alloca [33 x i8]
%5 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 48, i32 33)
%7 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 32
store i8 0, i8* %7
br label %8
8: ; preds = %8, %3
%9 = phi i32 [ %0, %3 ], [ %19, %8 ]
%10 = phi i32 [ 31, %3 ], [ %16, %8 ]
%11 = urem i32 %9, %1
%12 = trunc i32 %11 to i8
%13 = icmp sgt i8 %12, 9
%14 = select i1 %13, i8 55, i8 48
%15 = add i8 %14, %12
%16 = add i32 %10, -1
%17 = zext i32 %10 to i64
%18 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 %17
store i8 %15, i8* %18
%19 = udiv i32 %9, %1
%20 = icmp ult i32 %9, %1
br i1 %20, label %21, label %8
21: ; preds = %8
%22 = call i32 @remove_zeros(i8* %5)
%23 = call i32 @strcpy(i8* %2, i8* %5)
ret void
}