#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*/ sprintf(char *, char *, int);
char *ltoa(long val, char *str, int radix) {
  switch (radix) {
  case 8:
    sprintf(str, "%lo", val);
    break;
  case 10:
    sprintf(str, "%lu", val);
    break;
  case 16:
    sprintf(str, "%lx", val);
    break;
  default:
    sprintf(str, "bad radix: %d", radix);
    break;
  }
  return (str);
}


x86 assembly (clang, -O0):
<ltoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x8,%eax
je     1159 <ltoa+0x49>
jmp    1137 <ltoa+0x27>
mov    -0x18(%rbp),%eax
sub    $0xa,%eax
je     1174 <ltoa+0x64>
jmp    1148 <ltoa+0x38>
mov    -0x18(%rbp),%eax
sub    $0x10,%eax
je     118f <ltoa+0x7f>
jmp    11aa <ltoa+0x9a>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %eax,%edx
lea    0xe96(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11bd <ltoa+0xad>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %eax,%edx
lea    0xe7f(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11bd <ltoa+0xad>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %eax,%edx
lea    0xe68(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11bd <ltoa+0xad>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe54(%rip),%rsi
call   1030 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltoa>:
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rax
cmp    $0x10,%edx
je     112f <ltoa+0x1f>
cmp    $0xa,%edx
je     1138 <ltoa+0x28>
cmp    $0x8,%edx
jne    1146 <ltoa+0x36>
lea    0xed3(%rip),%rsi
jmp    113f <ltoa+0x2f>
lea    0xed2(%rip),%rsi
jmp    113f <ltoa+0x2f>
lea    0xec5(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
jmp    1150 <ltoa+0x40>
lea    0xebf(%rip),%rsi
mov    %rbx,%rdi
call   1030 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lo\00"
@.str.1 = [4 x i8] c"%lu\00"
@.str.2 = [4 x i8] c"%lx\00"
@.str.3 = [14 x i8] c"bad radix: %d\00"
define i8* @ltoa(i64 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
switch i32 %7, label %23 [
i32 8, label %8
i32 10, label %13
i32 16, label %18
]
8: ; preds = %3
%9 = load i8*, i8** %5
%10 = load i64, i64* %4
%11 = trunc i64 %10 to i32
%12 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %27
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i64, i64* %4
%16 = trunc i64 %15 to i32
%17 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %27
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = load i64, i64* %4
%21 = trunc i64 %20 to i32
%22 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %27
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = load i32, i32* %6
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %18, %13, %8
%28 = load i8*, i8** %5
ret i8* %28
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lo\00"
@.str.1 = [4 x i8] c"%lu\00"
@.str.2 = [4 x i8] c"%lx\00"
@.str.3 = [14 x i8] c"bad radix: %d\00"
define i8* @ltoa(i64 %0, i8* returned writeonly %1, i32 %2) {
switch i32 %2, label %13 [
i32 8, label %4
i32 10, label %7
i32 16, label %10
]
4: ; preds = %3
%5 = trunc i64 %0 to i32
%6 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %15
7: ; preds = %3
%8 = trunc i64 %0 to i32
%9 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %15
10: ; preds = %3
%11 = trunc i64 %0 to i32
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %15
13: ; preds = %3
%14 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %2)
br label %15
15: ; preds = %13, %10, %7, %4
ret i8* %1
}