#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 char uint8_t;
typedef scalar_t__ uint32_t;
typedef char int8_t;
typedef scalar_t__ int32_t;

/* Variables and functions */
char *ece391_strrev(char *);
uint8_t *ece391_itoa(uint32_t value, uint8_t *buf, int32_t radix) {
  static int8_t lookup[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  uint8_t *newbuf = buf;
  int32_t i;
  uint32_t newval = value;
  if (value == 0) {
    buf[0] = '0';
    buf[1] = '\0';
    return buf;
  }
  while (newval > 0) {
    i = newval % radix;
    *newbuf = lookup[i];
    newbuf++;
    newval /= radix;
  }
  *newbuf = '\0';
  return ece391_strrev(buf);
}


x86 assembly (clang, -O0):
<ece391_itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <ece391_itoa+0x4b>
mov    -0x18(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c3 <ece391_itoa+0xb3>
jmp    1160 <ece391_itoa+0x50>
cmpq   $0x0,-0x38(%rbp)
jle    11af <ece391_itoa+0x9f>
mov    -0x38(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %rdx,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
lea    0x2eac(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x38(%rbp),%rax
cqto
idiv   %rcx
mov    %rax,-0x38(%rbp)
jmp    1160 <ece391_itoa+0x50>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <ece391_strrev@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece391_itoa>:
test   %rdi,%rdi
je     1162 <ece391_itoa+0x52>
mov    %rdi,%rax
mov    %rsi,%rdi
jle    1157 <ece391_itoa+0x47>
mov    %rdx,%rcx
lea    0xed9(%rip),%r8
mov    %rsi,%rdi
jmp    1145 <ece391_itoa+0x35>
nopl   0x0(%rax)
cqto
idiv   %rcx
movzbl (%rdx,%r8,1),%edx
mov    %dl,(%rdi)
add    $0x1,%rdi
test   %rax,%rax
jle    1157 <ece391_itoa+0x47>
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    1130 <ece391_itoa+0x20>
xor    %edx,%edx
div    %ecx
jmp    1135 <ece391_itoa+0x25>
movb   $0x0,(%rdi)
mov    %rsi,%rdi
jmp    1030 <ece391_strrev@plt>
movw   $0x30,(%rsi)
mov    %rsi,%rax
ret

LLVM IR (-O0):
@ece391_itoa.lookup = internal global [37 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define i8* @ece391_itoa(i64 %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i64 %0, i64* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
%12 = load i64, i64* %5
store i64 %12, i64* %10
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 48, i8* %17
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 0, i8* %19
%20 = load i8*, i8** %6
store i8* %20, i8** %4
br label %42
21: ; preds = %3
br label %22
22: ; preds = %25, %21
%23 = load i64, i64* %10
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %22
%26 = load i64, i64* %10
%27 = load i64, i64* %7
%28 = srem i64 %26, %27
store i64 %28, i64* %9
%29 = load i64, i64* %9
%30 = getelementptr inbounds [37 x i8], [37 x i8]* @ece391_itoa.lookup, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = load i8*, i8** %8
store i8 %31, i8* %32
%33 = load i8*, i8** %8
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %8
%35 = load i64, i64* %7
%36 = load i64, i64* %10
%37 = sdiv i64 %36, %35
store i64 %37, i64* %10
br label %22
38: ; preds = %22
%39 = load i8*, i8** %8
store i8 0, i8* %39
%40 = load i8*, i8** %6
%41 = call i8* @ece391_strrev(i8* %40)
store i8* %41, i8** %4
br label %42
42: ; preds = %38, %15
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
@ece391_itoa.lookup = internal unnamed_addr constant [37 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define i8* @ece391_itoa(i64 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %0, 0
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = icmp sgt i64 %0, 0
br i1 %6, label %9, label %18
7: ; preds = %3
store i8 48, i8* %1
%8 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %8
br label %21
9: ; preds = %5, %9
%10 = phi i64 [ %16, %9 ], [ %0, %5 ]
%11 = phi i8* [ %15, %9 ], [ %1, %5 ]
%12 = srem i64 %10, %2
%13 = getelementptr inbounds [37 x i8], [37 x i8]* @ece391_itoa.lookup, i64 0, i64 %12
%14 = load i8, i8* %13
store i8 %14, i8* %11
%15 = getelementptr inbounds i8, i8* %11, i64 1
%16 = sdiv i64 %10, %2
%17 = icmp sgt i64 %16, 0
br i1 %17, label %9, label %18
18: ; preds = %9, %5
%19 = phi i8* [ %1, %5 ], [ %15, %9 ]
store i8 0, i8* %19
%20 = tail call i8* @ece391_strrev(i8* %1)
br label %21
21: ; preds = %18, %7
%22 = phi i8* [ %1, %7 ], [ %20, %18 ]
ret i8* %22
}