#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ reverse(char *);
int /*<<< orphan*/ stderr;
void itob(int n, char s[], int b) {
  static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  int i, sign;
  if (b < 2 || b > 36) {
    fprintf(stderr, "EX3_5: Cannot support base %d\n", b);
    exit(EXIT_FAILURE);
  }
  if ((sign = n) < 0)
    n = -n;
  i = 0;
  do {
    s[i++] = digits[n % b];
  } while ((n /= b) > 0);
  if (sign < 0)
    s[i++] = '-';
  s[i] = '\0';
  reverse(s);
}


x86 assembly (clang, -O0):
<itob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jl     1156 <itob+0x26>
cmpl   $0x24,-0x14(%rbp)
jle    117c <itob+0x4c>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe97(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1193 <itob+0x63>
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x14(%rbp)
movslq %edx,%rcx
lea    0x2e95(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jg     119a <itob+0x6a>
cmpl   $0x0,-0x1c(%rbp)
jge    11f8 <itob+0xc8>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x2d,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <reverse@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itob>:
push   %rax
mov    %edx,%r8d
lea    -0x25(%r8),%eax
cmp    $0xffffffdc,%eax
jbe    1185 <itob+0x55>
mov    %edi,%eax
neg    %eax
cmovs  %edi,%eax
xor    %ecx,%ecx
lea    0xeb3(%rip),%r9
nopl   (%rax)
cltd
idiv   %r8d
movslq %edx,%rdx
movzbl (%rdx,%r9,1),%edx
mov    %dl,(%rsi,%rcx,1)
add    $0x1,%rcx
test   %eax,%eax
jg     1150 <itob+0x20>
test   %edi,%edi
jns    1175 <itob+0x45>
mov    %ecx,%eax
add    $0x1,%rcx
movb   $0x2d,(%rsi,%rax,1)
movslq %ecx,%rax
movb   $0x0,(%rsi,%rax,1)
mov    %rsi,%rdi
pop    %rax
jmp    1030 <reverse@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
mov    %r8d,%edx
call   1040 <fprintf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@itob.digits = internal global [37 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
@stderr = global i32 0
@.str = [31 x i8] c"EX3_5: Cannot support base %d\0A\00"
@EXIT_FAILURE = global i32 0
define void @itob(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 2
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i32, i32* %6
%13 = icmp sgt i32 %12, 36
br i1 %13, label %14, label %20
14: ; preds = %11, %3
%15 = load i32, i32* @stderr
%16 = load i32, i32* %6
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %11
%21 = load i32, i32* %4
store i32 %21, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %4
%25 = sub nsw i32 0, %24
store i32 %25, i32* %4
br label %26
26: ; preds = %23, %20
store i32 0, i32* %7
br label %27
27: ; preds = %39, %26
%28 = load i32, i32* %4
%29 = load i32, i32* %6
%30 = srem i32 %28, %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [37 x i8], [37 x i8]* @itob.digits, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = load i8*, i8** %5
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 %33, i8* %38
br label %39
39: ; preds = %27
%40 = load i32, i32* %6
%41 = load i32, i32* %4
%42 = sdiv i32 %41, %40
store i32 %42, i32* %4
%43 = icmp sgt i32 %42, 0
br i1 %43, label %27, label %44
44: ; preds = %39
%45 = load i32, i32* %8
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i8*, i8** %5
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 45, i8* %52
br label %53
53: ; preds = %47, %44
%54 = load i8*, i8** %5
%55 = load i32, i32* %7
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
%58 = load i8*, i8** %5
%59 = call i32 @reverse(i8* %58)
ret void
}

LLVM IR (-O2):
@itob.digits = internal unnamed_addr constant [37 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
@stderr = global i32 0
@.str = [31 x i8] c"EX3_5: Cannot support base %d\0A\00"
@EXIT_FAILURE = global i32 0
define void @itob(i32 %0, i8* %1, i32 %2) {
%4 = add i32 %2, -37
%5 = icmp ult i32 %4, -35
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %2)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %3
%12 = icmp slt i32 %0, 0
%13 = sub nsw i32 0, %0
%14 = select i1 %12, i32 %13, i32 %0
br label %15
15: ; preds = %15, %11
%16 = phi i64 [ %22, %15 ], [ 0, %11 ]
%17 = phi i32 [ %24, %15 ], [ %14, %11 ]
%18 = srem i32 %17, %2
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [37 x i8], [37 x i8]* @itob.digits, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = add nuw i64 %16, 1
%23 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 %21, i8* %23
%24 = sdiv i32 %17, %2
%25 = icmp sgt i32 %24, 0
br i1 %25, label %15, label %26
26: ; preds = %15
br i1 %12, label %27, label %31
27: ; preds = %26
%28 = add i64 %16, 2
%29 = and i64 %22, 4294967295
%30 = getelementptr inbounds i8, i8* %1, i64 %29
store i8 45, i8* %30
br label %31
31: ; preds = %27, %26
%32 = phi i64 [ %28, %27 ], [ %22, %26 ]
%33 = shl i64 %32, 32
%34 = ashr exact i64 %33, 32
%35 = getelementptr inbounds i8, i8* %1, i64 %34
store i8 0, i8* %35
%36 = tail call i32 @reverse(i8* %1)
ret void
}