#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int log(int);
char *malloc(int);
int /*<<< orphan*/ reverse(char *);
int /*<<< orphan*/ stderr;
char *ctob(char input, int base) {
  char c;
  char *output;
  long long mod;
  int i, len;
  i = 0;
  len = (int)(log(10) / log(base)) + 1;
  if (base == 2)
    len = 8;
  if (!(output = malloc(len))) {
    fprintf(stderr, "Malloc Error!\n");
    exit(2);
  }
  if (base > 62 || base < 2) {
    fprintf(stderr, "Base Error!\n");
    exit(4);
  }
  do {
    mod = input % base;
    if (mod > 35)
      c = 'a' - 36;
    else if (mod > 9)
      c = 'A' - 10;
    else
      c = '0';
    output[i++] = mod + c;
  } while (input /= base);
  if (base == 2)
    output[i++] = '0';
  output[i] = '\0';
  reverse(output);
  return output;
}


x86 assembly (clang, -O0):
<ctob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0xa,%edi
call   1060 <log@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <log@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    119c <ctob+0x4c>
movl   $0x8,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d1 <ctob+0x81>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
cmpl   $0x3e,-0x8(%rbp)
jg     11e5 <ctob+0x95>
cmpl   $0x2,-0x8(%rbp)
jge    1204 <ctob+0xb4>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x4,%edi
call   1070 <exit@plt>
jmp    1209 <ctob+0xb9>
movsbl -0x1(%rbp),%eax
cltd
idivl  -0x8(%rbp)
movslq %edx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x23,-0x20(%rbp)
jle    122c <ctob+0xdc>
movb   $0x3d,-0x9(%rbp)
jmp    1249 <ctob+0xf9>
cmpq   $0x9,-0x20(%rbp)
jle    1240 <ctob+0xf0>
movb   $0x37,-0x9(%rbp)
jmp    1244 <ctob+0xf4>
movb   $0x30,-0x9(%rbp)
jmp    1249 <ctob+0xf9>
mov    -0x20(%rbp),%rax
movsbq -0x9(%rbp),%rcx
add    %rcx,%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%ecx
movsbl -0x1(%rbp),%eax
cltd
idiv   %ecx
mov    %al,-0x1(%rbp)
cmp    $0x0,%al
jne    1209 <ctob+0xb9>
cmpl   $0x2,-0x8(%rbp)
jne    12a1 <ctob+0x151>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq %ecx,%rcx
movb   $0x30,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <reverse@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctob>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0xa,%edi
call   1060 <log@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1060 <log@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
add    $0x1,%eax
cmp    $0x2,%r14d
mov    $0x8,%edi
cmovne %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     120c <ctob+0xbc>
mov    %rax,%rbx
lea    -0x3f(%r14),%eax
cmp    $0xffffffc3,%eax
jb     122b <ctob+0xdb>
xor    %ecx,%ecx
jmp    11be <ctob+0x6e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %dl,%al
mov    %al,(%rbx,%rcx,1)
add    $0x1,%rcx
test   %bpl,%bpl
je     11e5 <ctob+0x95>
movsbl %bpl,%eax
cltd
idiv   %r14d
mov    $0x37,%esi
mov    %eax,%ebp
cmp    $0xa,%edx
jge    11d7 <ctob+0x87>
mov    $0x30,%esi
mov    $0x3d,%eax
cmp    $0x24,%edx
jge    11b0 <ctob+0x60>
mov    %esi,%eax
jmp    11b0 <ctob+0x60>
cmp    $0x2,%r14d
jne    11f5 <ctob+0xa5>
mov    %ecx,%eax
add    $0x1,%rcx
movb   $0x30,(%rbx,%rax,1)
movslq %ecx,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1030 <reverse@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x4,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"Malloc Error!\0A\00"
@.str.1 = [13 x i8] c"Base Error!\0A\00"
define i8* @ctob(i8 signext %0, i32 %1) {
%3 = alloca i8
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8 %0, i8* %3
store i32 %1, i32* %4
store i32 0, i32* %8
%10 = call i32 @log(i32 10)
%11 = load i32, i32* %4
%12 = call i32 @log(i32 %11)
%13 = sdiv i32 %10, %12
%14 = add nsw i32 %13, 1
store i32 %14, i32* %9
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 8, i32* %9
br label %18
18: ; preds = %17, %2
%19 = load i32, i32* %9
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %18
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 62
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 2
br i1 %31, label %32, label %36
32: ; preds = %29, %26
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 4)
unreachable
36: ; preds = %29
br label %37
37: ; preds = %63, %36
%38 = load i8, i8* %3
%39 = sext i8 %38 to i32
%40 = load i32, i32* %4
%41 = srem i32 %39, %40
%42 = sext i32 %41 to i64
store i64 %42, i64* %7
%43 = load i64, i64* %7
%44 = icmp sgt i64 %43, 35
br i1 %44, label %45, label %46
45: ; preds = %37
store i8 61, i8* %5
br label %52
46: ; preds = %37
%47 = load i64, i64* %7
%48 = icmp sgt i64 %47, 9
br i1 %48, label %49, label %50
49: ; preds = %46
store i8 55, i8* %5
br label %51
50: ; preds = %46
store i8 48, i8* %5
br label %51
51: ; preds = %50, %49
br label %52
52: ; preds = %51, %45
%53 = load i64, i64* %7
%54 = load i8, i8* %5
%55 = sext i8 %54 to i64
%56 = add nsw i64 %53, %55
%57 = trunc i64 %56 to i8
%58 = load i8*, i8** %6
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
store i8 %57, i8* %62
br label %63
63: ; preds = %52
%64 = load i32, i32* %4
%65 = load i8, i8* %3
%66 = sext i8 %65 to i32
%67 = sdiv i32 %66, %64
%68 = trunc i32 %67 to i8
store i8 %68, i8* %3
%69 = icmp ne i8 %68, 0
br i1 %69, label %37, label %70
70: ; preds = %63
%71 = load i32, i32* %4
%72 = icmp eq i32 %71, 2
br i1 %72, label %73, label %79
73: ; preds = %70
%74 = load i8*, i8** %6
%75 = load i32, i32* %8
%76 = add nsw i32 %75, 1
store i32 %76, i32* %8
%77 = sext i32 %75 to i64
%78 = getelementptr inbounds i8, i8* %74, i64 %77
store i8 48, i8* %78
br label %79
79: ; preds = %73, %70
%80 = load i8*, i8** %6
%81 = load i32, i32* %8
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
store i8 0, i8* %83
%84 = load i8*, i8** %6
%85 = call i32 @reverse(i8* %84)
%86 = load i8*, i8** %6
ret i8* %86
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"Malloc Error!\0A\00"
@.str.1 = [13 x i8] c"Base Error!\0A\00"
define i8* @ctob(i8 signext %0, i32 %1) {
%3 = tail call i32 @log(i32 10)
%4 = tail call i32 @log(i32 %1)
%5 = sdiv i32 %3, %4
%6 = add nsw i32 %5, 1
%7 = icmp eq i32 %1, 2
%8 = select i1 %7, i32 8, i32 %6
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 2)
unreachable
15: ; preds = %2
%16 = add i32 %1, -63
%17 = icmp ult i32 %16, -61
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 4)
unreachable
22: ; preds = %15, %22
%23 = phi i64 [ %33, %22 ], [ 0, %15 ]
%24 = phi i8 [ %36, %22 ], [ %0, %15 ]
%25 = sext i8 %24 to i32
%26 = srem i32 %25, %1
%27 = trunc i32 %26 to i8
%28 = icmp sgt i32 %26, 35
%29 = icmp sgt i32 %26, 9
%30 = select i1 %29, i8 55, i8 48
%31 = select i1 %28, i8 61, i8 %30
%32 = add i8 %31, %27
%33 = add nuw i64 %23, 1
%34 = getelementptr inbounds i8, i8* %9, i64 %23
store i8 %32, i8* %34
%35 = sdiv i32 %25, %1
%36 = trunc i32 %35 to i8
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %22
38: ; preds = %22
br i1 %7, label %39, label %43
39: ; preds = %38
%40 = add i64 %23, 2
%41 = and i64 %33, 4294967295
%42 = getelementptr inbounds i8, i8* %9, i64 %41
store i8 48, i8* %42
br label %43
43: ; preds = %39, %38
%44 = phi i64 [ %40, %39 ], [ %33, %38 ]
%45 = shl i64 %44, 32
%46 = ashr exact i64 %45, 32
%47 = getelementptr inbounds i8, i8* %9, i64 %46
store i8 0, i8* %47
%48 = tail call i32 @reverse(i8* %9)
ret i8* %9
}