#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 */
int CHAR_BIT;
char *convbase(char *dest, unsigned long n, int base) {
  char *ret = dest + sizeof n * CHAR_BIT;
  if (base < 2 || base > (int)sizeof "0123456789abcdef" - 1)
    return NULL;
  *ret = '\0';
  do {
    *--ret = "0123456789abcdef"[n % base];
    n /= base;
  } while (n > 0);
  return ret;
}


x86 assembly (clang, -O0):
<convbase>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2ec6(%rip),%rcx
movslq (%rcx),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jl     113c <convbase+0x3c>
cmpl   $0x10,-0x1c(%rbp)
jle    1149 <convbase+0x49>
movq   $0x0,-0x8(%rbp)
jmp    119d <convbase+0x9d>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
xor    %edx,%edx
div    %rcx
lea    0xe9c(%rip),%rax
mov    (%rax,%rdx,1),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,-0x1(%rax)
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
ja     1150 <convbase+0x50>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<convbase>:
lea    -0x11(%rdx),%eax
cmp    $0xfffffff1,%eax
jae    110e <convbase+0xe>
xor    %ecx,%ecx
mov    %rcx,%rax
ret
mov    0x2ecb(%rip),%rax
movslq (%rax),%rax
lea    (%rdi,%rax,8),%rcx
movb   $0x0,(%rcx)
mov    %edx,%edi
lea    0xed8(%rip),%r8
jmp    114c <convbase+0x4c>
nopw   0x0(%rax,%rax,1)
mov    %rsi,%rax
xor    %edx,%edx
div    %rdi
movzbl (%rdx,%r8,1),%edx
mov    %dl,-0x1(%rcx)
add    $0xffffffffffffffff,%rcx
cmp    %rdi,%rsi
mov    %rax,%rsi
jb     115d <convbase+0x5d>
mov    %rsi,%rax
shr    $0x20,%rax
jne    1130 <convbase+0x30>
mov    %esi,%eax
xor    %edx,%edx
div    %edi
jmp    1138 <convbase+0x38>
mov    %rcx,%rax
ret

LLVM IR (-O0):
@CHAR_BIT = global i32 0
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @convbase(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @CHAR_BIT
%11 = sext i32 %10 to i64
%12 = mul i64 8, %11
%13 = getelementptr inbounds i8, i8* %9, i64 %12
store i8* %13, i8** %8
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 2
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 16
br i1 %18, label %19, label %20
19: ; preds = %16, %3
store i8* null, i8** %4
br label %40
20: ; preds = %16
%21 = load i8*, i8** %8
store i8 0, i8* %21
br label %22
22: ; preds = %35, %20
%23 = load i64, i64* %6
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = urem i64 %23, %25
%27 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %8
store i8 %28, i8* %30
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = load i64, i64* %6
%34 = udiv i64 %33, %32
store i64 %34, i64* %6
br label %35
35: ; preds = %22
%36 = load i64, i64* %6
%37 = icmp ugt i64 %36, 0
br i1 %37, label %22, label %38
38: ; preds = %35
%39 = load i8*, i8** %8
store i8* %39, i8** %4
br label %40
40: ; preds = %38, %19
%41 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
@CHAR_BIT = global i32 0
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @convbase(i8* writeonly %0, i64 %1, i32 %2) {
%4 = load i32, i32* @CHAR_BIT
%5 = sext i32 %4 to i64
%6 = shl nsw i64 %5, 3
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = add i32 %2, -17
%9 = icmp ult i32 %8, -15
br i1 %9, label %21, label %10
10: ; preds = %3
store i8 0, i8* %7
%11 = zext i32 %2 to i64
br label %12
12: ; preds = %12, %10
%13 = phi i64 [ %1, %10 ], [ %19, %12 ]
%14 = phi i8* [ %7, %10 ], [ %18, %12 ]
%15 = urem i64 %13, %11
%16 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = getelementptr inbounds i8, i8* %14, i64 -1
store i8 %17, i8* %18
%19 = udiv i64 %13, %11
%20 = icmp ult i64 %13, %11
br i1 %20, label %21, label %12
21: ; preds = %12, %3
%22 = phi i8* [ null, %3 ], [ %18, %12 ]
ret i8* %22
}