char *itoa(int val) {
  register char *ptr;
  static char buffer[16];
  ptr = buffer + sizeof(buffer);
  *--ptr = '\0';
  if (val == 0) {
    *--ptr = '0';
  } else
    while (val != 0) {
      *--ptr = (val % 10) + '0';
      val /= 10;
    }
  return (ptr);
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
lea    0x2f22(%rip),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,-0x1(%rax)
cmpl   $0x0,-0x4(%rbp)
jne    114b <itoa+0x4b>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x30,-0x1(%rax)
jmp    1194 <itoa+0x94>
jmp    1150 <itoa+0x50>
cmpl   $0x0,-0x4(%rbp)
je     118f <itoa+0x8f>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    1150 <itoa+0x50>
jmp    1194 <itoa+0x94>
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
movb   $0x0,0x2f38(%rip)
test   %edi,%edi
je     1156 <itoa+0x56>
lea    0x2f2d(%rip),%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %edi,%rcx
imul   $0x66666667,%rcx,%rdi
mov    %rdi,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rdi
add    %edx,%edi
lea    (%rdi,%rdi,1),%edx
lea    (%rdx,%rdx,4),%edx
mov    %ecx,%esi
sub    %edx,%esi
add    $0x30,%sil
mov    %sil,-0x1(%rax)
add    $0xffffffffffffffff,%rax
add    $0x9,%ecx
cmp    $0x12,%ecx
ja     1120 <itoa+0x20>
ret
movb   $0x30,0x2ee1(%rip)
lea    0x2eda(%rip),%rax
ret

LLVM IR (-O0):
@itoa.buffer = internal global [16 x i8] zeroinitializer
define i8* @itoa(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
store i32 %0, i32* %2
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @itoa.buffer, i64 1, i64 0), i8** %3
%4 = load i8*, i8** %3
%5 = getelementptr inbounds i8, i8* %4, i32 -1
store i8* %5, i8** %3
store i8 0, i8* %5
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i32 -1
store i8* %10, i8** %3
store i8 48, i8* %10
br label %25
11: ; preds = %1
br label %12
12: ; preds = %15, %11
%13 = load i32, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i32, i32* %2
%17 = srem i32 %16, 10
%18 = add nsw i32 %17, 48
%19 = trunc i32 %18 to i8
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i32 -1
store i8* %21, i8** %3
store i8 %19, i8* %21
%22 = load i32, i32* %2
%23 = sdiv i32 %22, 10
store i32 %23, i32* %2
br label %12
24: ; preds = %12
br label %25
25: ; preds = %24, %8
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@itoa.buffer = internal global [16 x i8] zeroinitializer
define i8* @itoa(i32 %0) {
store i8 0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @itoa.buffer, i64 0, i64 15)
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %4
3: ; preds = %1
store i8 48, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @itoa.buffer, i64 0, i64 14)
br label %14
4: ; preds = %1, %4
%5 = phi i8* [ %10, %4 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @itoa.buffer, i64 0, i64 15), %1 ]
%6 = phi i32 [ %11, %4 ], [ %0, %1 ]
%7 = srem i32 %6, 10
%8 = trunc i32 %7 to i8
%9 = add nsw i8 %8, 48
%10 = getelementptr inbounds i8, i8* %5, i64 -1
store i8 %9, i8* %10
%11 = sdiv i32 %6, 10
%12 = add i32 %6, 9
%13 = icmp ult i32 %12, 19
br i1 %13, label %14, label %4
14: ; preds = %4, %3
%15 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @itoa.buffer, i64 0, i64 14), %3 ], [ %10, %4 ]
ret i8* %15
}