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

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
char *to_base(unsigned long long n, int radix) {
  static char str[256];
  static char dstr[256];
  int pos = sizeof(str) - 1;
  do {
    int digit = n % radix;
    n /= radix;
    sprintf(dstr, "[%d]", digit);
    int dlen = strlen(dstr);
    pos -= dlen;
    memcpy(str + pos, dstr, dlen);
  } while (n);
  return str + pos;
}


x86 assembly (clang, -O0):
<to_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xff,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    %eax,-0x14(%rbp)
movslq -0xc(%rbp),%rcx
mov    -0x8(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%edx
lea    0x2fdd(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1050 <sprintf@plt>
lea    0x2fca(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
lea    0x2eac(%rip),%rdi
add    %rax,%rdi
mov    -0x18(%rbp),%edx
lea    0x2f9f(%rip),%rsi
call   1040 <memcpy@plt>
cmpq   $0x0,-0x8(%rbp)
jne    1146 <to_base+0x16>
movslq -0x10(%rbp),%rcx
lea    0x2e84(%rip),%rax
add    %rcx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_base>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %esi,0x4(%rsp)
movslq %esi,%r14
mov    $0xff,%r15d
lea    0x2ffe(%rip),%r12
jmp    11a7 <to_base+0x77>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
mov    %rax,%r13
mov    %r12,%rdi
lea    0xe8b(%rip),%rsi
call   1050 <sprintf@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
sub    %eax,%r15d
movslq %r15d,%rbp
lea    0x2ec1(%rip),%rcx
add    %rcx,%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
cmp    %r14,%rbx
mov    %r13,%rbx
jb     11c0 <to_base+0x90>
mov    %rbx,%rax
or     %r14,%rax
shr    $0x20,%rax
jne    1160 <to_base+0x30>
mov    %ebx,%eax
xor    %edx,%edx
divl   0x4(%rsp)
mov    %eax,%r13d
jmp    116b <to_base+0x3b>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@to_base.str = internal global [256 x i8] zeroinitializer
@to_base.dstr = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c"[%d]\00"
define i8* @to_base(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
store i32 255, i32* %5
br label %8
8: ; preds = %29, %2
%9 = load i64, i64* %3
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = urem i64 %9, %11
%13 = trunc i64 %12 to i32
store i32 %13, i32* %6
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = load i64, i64* %3
%17 = udiv i64 %16, %15
store i64 %17, i64* %3
%18 = load i32, i32* %6
%19 = call i32 @sprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.dstr, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 @strlen(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.dstr, i64 0, i64 0))
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = load i32, i32* %5
%23 = sub nsw i32 %22, %21
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.str, i64 0, i64 0), i64 %25
%27 = load i32, i32* %7
%28 = call i32 @memcpy(i8* %26, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.dstr, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %8
%30 = load i64, i64* %3
%31 = icmp ne i64 %30, 0
br i1 %31, label %8, label %32
32: ; preds = %29
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.str, i64 0, i64 0), i64 %34
ret i8* %35
}

LLVM IR (-O2):
@to_base.str = internal global [256 x i8] zeroinitializer
@to_base.dstr = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c"[%d]\00"
define i8* @to_base(i64 %0, i32 %1) {
%3 = sext i32 %1 to i64
br label %4
4: ; preds = %4, %2
%5 = phi i32 [ 255, %2 ], [ %12, %4 ]
%6 = phi i64 [ %0, %2 ], [ %9, %4 ]
%7 = urem i64 %6, %3
%8 = trunc i64 %7 to i32
%9 = udiv i64 %6, %3
%10 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.dstr, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %8)
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.dstr, i64 0, i64 0))
%12 = sub nsw i32 %5, %11
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [256 x i8], [256 x i8]* @to_base.str, i64 0, i64 %13
%15 = tail call i32 @memcpy(i8* %14, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @to_base.dstr, i64 0, i64 0), i32 %11)
%16 = icmp ult i64 %6, %3
br i1 %16, label %17, label %4
17: ; preds = %4
ret i8* %14
}