#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*/ dec2bin(int, char *);
int /*<<< orphan*/ dec2hex(int, char *);
int /*<<< orphan*/ dec2oct(int, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ reverse(char *);
void itob(int n, char s[], int b) {
  switch (b) {
  case 2:
    dec2bin(n, s);
    printf("%s\n", s);
    break;
  case 8:
    dec2oct(n, s);
    reverse(s);
    printf("%s\n", s);
    break;
  case 10:
    printf("%d\n", n);
    break;
  case 16:
    dec2hex(n, s);
    reverse(s);
    printf("%s\n", s);
    break;
  default:
    break;
  }
}


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)
mov    -0x14(%rbp),%eax
add    $0xfffffffe,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0xe,%eax
ja     121c <itob+0xcc>
mov    -0x20(%rbp),%rax
lea    0xe7e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <dec2bin@plt>
mov    -0x10(%rbp),%rsi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1221 <itob+0xd1>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <dec2oct@plt>
mov    -0x10(%rbp),%rdi
call   1030 <reverse@plt>
mov    -0x10(%rbp),%rsi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1221 <itob+0xd1>
mov    -0x4(%rbp),%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1221 <itob+0xd1>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1070 <dec2hex@plt>
mov    -0x10(%rbp),%rdi
call   1030 <reverse@plt>
mov    -0x10(%rbp),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1221 <itob+0xd1>
jmp    1221 <itob+0xd1>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itob>:
push   %rbx
add    $0xfffffffe,%edx
ror    %edx
cmp    $0x7,%edx
ja     118c <itob+0x2c>
mov    %rsi,%rbx
mov    %edi,%esi
lea    0xe89(%rip),%rax
movslq (%rax,%rdx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %esi,%edi
mov    %rbx,%rsi
call   1070 <dec2bin@plt>
jmp    11bb <itob+0x5b>
pop    %rbx
ret
mov    %esi,%edi
mov    %rbx,%rsi
call   1060 <dec2oct@plt>
jmp    11b3 <itob+0x53>
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
pop    %rbx
jmp    1050 <printf@plt>
mov    %esi,%edi
mov    %rbx,%rsi
call   1080 <dec2hex@plt>
mov    %rbx,%rdi
call   1030 <reverse@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1040 <puts@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define void @itob(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
switch i32 %7, label %33 [
i32 2, label %8
i32 8, label %14
i32 10, label %22
i32 16, label %25
]
8: ; preds = %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = call i32 @dec2bin(i32 %9, i8* %10)
%12 = load i8*, i8** %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %34
14: ; preds = %3
%15 = load i32, i32* %4
%16 = load i8*, i8** %5
%17 = call i32 @dec2oct(i32 %15, i8* %16)
%18 = load i8*, i8** %5
%19 = call i32 @reverse(i8* %18)
%20 = load i8*, i8** %5
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %34
22: ; preds = %3
%23 = load i32, i32* %4
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %34
25: ; preds = %3
%26 = load i32, i32* %4
%27 = load i8*, i8** %5
%28 = call i32 @dec2hex(i32 %26, i8* %27)
%29 = load i8*, i8** %5
%30 = call i32 @reverse(i8* %29)
%31 = load i8*, i8** %5
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %31)
br label %34
33: ; preds = %3
br label %34
34: ; preds = %33, %25, %22, %14, %8
ret void
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%d\0A\00"
define void @itob(i32 %0, i8* %1, i32 %2) {
%4 = add i32 %2, -2
%5 = tail call i32 @llvm.fshl.i32(i32 %4, i32 %4, i32 31)
switch i32 %5, label %19 [
i32 0, label %6
i32 3, label %9
i32 4, label %13
i32 7, label %15
]
6: ; preds = %3
%7 = tail call i32 @dec2bin(i32 %0, i8* %1)
%8 = tail call i32 @puts(i8* dereferenceable(1) %1)
br label %19
9: ; preds = %3
%10 = tail call i32 @dec2oct(i32 %0, i8* %1)
%11 = tail call i32 @reverse(i8* %1)
%12 = tail call i32 @puts(i8* dereferenceable(1) %1)
br label %19
13: ; preds = %3
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %19
15: ; preds = %3
%16 = tail call i32 @dec2hex(i32 %0, i8* %1)
%17 = tail call i32 @reverse(i8* %1)
%18 = tail call i32 @puts(i8* dereferenceable(1) %1)
br label %19
19: ; preds = %3, %15, %13, %9, %6
ret void
}