#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 pl_dectobin(int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
char *pl_itoa(int val, char *buf, size_t len, int base) {
  switch (base) {
  default:
    return buf;
  case 10:
    snprintf(buf, len, "%d", val);
    break;
  case 16:
    snprintf(buf, len, "%x", val);
    break;
  case 8:
    snprintf(buf, len, "%o", val);
    break;
  case 2:
    val = pl_dectobin(val);
    snprintf(buf, len, "%d", val);
    break;
  }
  return buf;
}


x86 assembly (clang, -O0):
<pl_itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0xfffffffe,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0xe,%eax
ja     115f <pl_itoa+0x3f>
mov    -0x30(%rbp),%rax
lea    0xeaa(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ea <pl_itoa+0xca>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%ecx
lea    0xebe(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11e2 <pl_itoa+0xc2>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%ecx
lea    0xea5(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11e2 <pl_itoa+0xc2>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%ecx
lea    0xe8c(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11e2 <pl_itoa+0xc2>
mov    -0xc(%rbp),%edi
call   1040 <pl_dectobin@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%ecx
lea    0xe5f(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pl_itoa>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
add    $0xfffffffe,%ecx
ror    %ecx
cmp    $0x7,%ecx
ja     1173 <pl_itoa+0x53>
mov    %rdx,%r14
mov    %edi,%eax
lea    0xec3(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    %eax,%edi
call   1040 <pl_dectobin@plt>
lea    0xecc(%rip),%rdx
jmp    1166 <pl_itoa+0x46>
lea    0xec9(%rip),%rdx
jmp    1166 <pl_itoa+0x46>
lea    0xebd(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%ecx
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [3 x i8] c"%o\00"
define i8* @pl_itoa(i32 %0, i8* returned %1, i64 %2, i32 %3) {
%5 = add i32 %3, -2
%6 = tail call i32 @llvm.fshl.i32(i32 %5, i32 %5, i32 31)
switch i32 %6, label %16 [
i32 4, label %7
i32 7, label %9
i32 3, label %11
i32 0, label %13
]
7: ; preds = %4
%8 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %16
9: ; preds = %4
%10 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %16
11: ; preds = %4
%12 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %16
13: ; preds = %4
%14 = tail call i32 @pl_dectobin(i32 %0)
%15 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %7, %9, %11, %13, %4
ret i8* %1
}