void itoa(int n, char s[]) {
  static int depth = 1;
  static int indx = 0;
  if (n < 0) {
    s[indx++] = '-';
    n = -n;
  }
  if (n / 10) {
    depth++;
    itoa(n / 10, s);
  }
  depth--;
  s[indx++] = n % 10 + '0';
  if (depth == 0)
    s[indx] = '\0';
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    114d <itoa+0x3d>
mov    -0x10(%rbp),%rax
mov    0x2efd(%rip),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,0x2ef2(%rip)
movslq %ecx,%rcx
movb   $0x2d,(%rax,%rcx,1)
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%eax
je     1186 <itoa+0x76>
mov    0x2ec1(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2eb8(%rip)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
call   1030 <itoa@plt>
mov    0x2e9c(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2e93(%rip)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    -0x10(%rbp),%rax
mov    0x2e83(%rip),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,0x2e78(%rip)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0x0,0x2e63(%rip)
jne    11da <itoa+0xca>
mov    -0x10(%rbp),%rax
movslq 0x2e5a(%rip),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
test   %edi,%edi
jns    1134 <itoa+0x24>
movslq 0x2f0c(%rip),%rax
lea    0x1(%rax),%ecx
mov    %ecx,0x2f03(%rip)
movb   $0x2d,(%r14,%rax,1)
neg    %ebp
mov    %ebp,%ebx
cmp    $0xa,%ebp
jb     1157 <itoa+0x47>
mov    $0xcccccccd,%edi
imul   %rbx,%rdi
shr    $0x23,%rdi
addl   $0x1,0x2ed9(%rip)
mov    %r14,%rsi
call   1030 <itoa@plt>
mov    $0xcccccccd,%eax
imul   %rbx,%rax
shr    $0x23,%rax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebp
or     $0x30,%bpl
addl   $0xffffffff,0x2eb2(%rip)
movslq 0x2eb3(%rip),%rcx
lea    0x1(%rcx),%rax
mov    %eax,0x2ea9(%rip)
mov    %bpl,(%r14,%rcx,1)
jne    1192 <itoa+0x82>
movb   $0x0,(%r14,%rax,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@itoa.depth = internal global i32 1
@itoa.indx = internal global i32 0
define void @itoa(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = load i8*, i8** %4
%9 = load i32, i32* @itoa.indx
%10 = add nsw i32 %9, 1
store i32 %10, i32* @itoa.indx
%11 = sext i32 %9 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
store i8 45, i8* %12
%13 = load i32, i32* %3
%14 = sub nsw i32 0, %13
store i32 %14, i32* %3
br label %15
15: ; preds = %7, %2
%16 = load i32, i32* %3
%17 = sdiv i32 %16, 10
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i32, i32* @itoa.depth
%21 = add nsw i32 %20, 1
store i32 %21, i32* @itoa.depth
%22 = load i32, i32* %3
%23 = sdiv i32 %22, 10
%24 = load i8*, i8** %4
br label %25
25: ; preds = %19, %15
%26 = load i32, i32* @itoa.depth
%27 = add nsw i32 %26, -1
store i32 %27, i32* @itoa.depth
%28 = load i32, i32* %3
%29 = srem i32 %28, 10
%30 = add nsw i32 %29, 48
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %4
%33 = load i32, i32* @itoa.indx
%34 = add nsw i32 %33, 1
store i32 %34, i32* @itoa.indx
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 %31, i8* %36
%37 = load i32, i32* @itoa.depth
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %25
%40 = load i8*, i8** %4
%41 = load i32, i32* @itoa.indx
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %39, %25
ret void
}

LLVM IR (-O2):
@itoa.depth = internal unnamed_addr global i32 1
@itoa.indx = internal unnamed_addr global i32 0
define void @itoa(i32 %0, i8* nocapture writeonly %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i32, i32* @itoa.indx
%6 = add nsw i32 %5, 1
store i32 %6, i32* @itoa.indx
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 45, i8* %8
%9 = sub nsw i32 0, %0
br label %10
10: ; preds = %4, %2
%11 = phi i32 [ %9, %4 ], [ %0, %2 ]
%12 = icmp ult i32 %11, 10
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = udiv i32 %11, 10
%15 = load i32, i32* @itoa.depth
%16 = add nsw i32 %15, 1
store i32 %16, i32* @itoa.depth
tail call void @itoa(i32 %14, i8* %1)
br label %17
17: ; preds = %13, %10
%18 = load i32, i32* @itoa.depth
%19 = add nsw i32 %18, -1
store i32 %19, i32* @itoa.depth
%20 = urem i32 %11, 10
%21 = trunc i32 %20 to i8
%22 = or i8 %21, 48
%23 = load i32, i32* @itoa.indx
%24 = add nsw i32 %23, 1
store i32 %24, i32* @itoa.indx
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 %22, i8* %26
%27 = icmp eq i32 %19, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = sext i32 %24 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %28, %17
ret void
}