#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 */
size_t abs(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ reverse(char *);
int strlen(char *);
int itob(int n, char s[], int b) {
  int i, sign;
  char symbols[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  if (b < 2 || b > strlen(symbols)) {
    printf("error - not valid base for numeral system\n");
    return 1;
  }
  sign = n;
  i = 0;
  do {
    s[i++] = symbols[abs(n % b)];
  } while ((abs(n /= b)) > 0);
  if (sign < 0)
    s[i++] = '-';
  s[i] = '\0';
  reverse(s);
  return 0;
}


x86 assembly (clang, -O0):
<itob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe93(%rip),%rsi
mov    $0x25,%edx
call   1070 <memcpy@plt>
cmpl   $0x2,-0x14(%rbp)
jl     119d <itob+0x4d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
jle    11b5 <itob+0x65>
lea    0xe81(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1242 <itob+0xf2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,%edi
call   1040 <abs@plt>
mov    -0x50(%rbp,%rax,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    -0x8(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,%edi
mov    %edi,-0x8(%rbp)
call   1040 <abs@plt>
cmp    $0x0,%rax
ja     11c2 <itob+0x72>
cmpl   $0x0,-0x1c(%rbp)
jge    1226 <itob+0xd6>
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>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itob>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movaps 0xebd(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xea1(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x5a595857565554,%rax
mov    %rax,0x1d(%rsp)
cmp    $0x2,%edx
jl     118d <itob+0x4d>
mov    %edx,%r12d
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsp,%rdi
call   1060 <strlen@plt>
cmp    %r12d,%eax
jge    11a0 <itob+0x60>
lea    0xe91(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    11f9 <itob+0xb9>
xor    %ebp,%ebp
mov    %r14d,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%eax
cltd
idiv   %r12d
mov    %edx,%edi
mov    %eax,%ebx
call   1050 <abs@plt>
movzbl (%rsp,%rax,1),%eax
mov    %al,(%r15,%rbp,1)
add    $0x1,%rbp
mov    %ebx,%edi
call   1050 <abs@plt>
test   %rax,%rax
jne    11b0 <itob+0x70>
test   %r14d,%r14d
jns    11e7 <itob+0xa7>
mov    %ebp,%eax
add    $0x1,%rbp
movb   $0x2d,(%r15,%rax,1)
movslq %ebp,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rdi
call   1030 <reverse@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@__const.itob.symbols = [37 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
@str = [42 x i8] c"error - not valid base for numeral system\00"
define i32 @itob(i32 %0, i8* %1, i32 %2) {
%4 = alloca [37 x i8]
%5 = getelementptr inbounds [37 x i8], [37 x i8]* %4, i64 0, i64 0
%6 = icmp slt i32 %2, 2
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = call i32 @strlen(i8* %5)
%9 = icmp slt i32 %8, %2
br i1 %9, label %10, label %12
10: ; preds = %7, %3
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %36
12: ; preds = %7, %12
%13 = phi i64 [ %19, %12 ], [ 0, %7 ]
%14 = phi i32 [ %21, %12 ], [ %0, %7 ]
%15 = srem i32 %14, %2
%16 = call i64 @abs(i32 %15)
%17 = getelementptr inbounds [37 x i8], [37 x i8]* %4, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = add nuw i64 %13, 1
%20 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 %18, i8* %20
%21 = sdiv i32 %14, %2
%22 = call i64 @abs(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %12
24: ; preds = %12
%25 = icmp slt i32 %0, 0
br i1 %25, label %26, label %30
26: ; preds = %24
%27 = add i64 %13, 2
%28 = and i64 %19, 4294967295
%29 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 45, i8* %29
br label %30
30: ; preds = %26, %24
%31 = phi i64 [ %27, %26 ], [ %19, %24 ]
%32 = shl i64 %31, 32
%33 = ashr exact i64 %32, 32
%34 = getelementptr inbounds i8, i8* %1, i64 %33
store i8 0, i8* %34
%35 = call i32 @reverse(i8* %1)
br label %36
36: ; preds = %30, %10
%37 = phi i32 [ 1, %10 ], [ 0, %30 ]
ret i32 %37
}