#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*/ printf(char *, ...);
int /*<<< orphan*/ reverse(char *, int);
void itoa(int n, char s[]) {
  int i, sign;
  if ((sign = n) < 0) {
    n = -n;
  }
  i = 0;
  do {
    printf("%d\n", n % 10);
    s[i++] = n % 10 + '0';
  } while ((n /= 10) > 0);
  if (sign < 0) {
    s[i++] = '-';
  }
  s[i] = '\0';
  reverse(s, i);
  printf("%s\n", s);
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1146 <itoa+0x26>
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jg     114d <itoa+0x2d>
cmpl   $0x0,-0x18(%rbp)
jge    11c2 <itoa+0xa2>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movb   $0x2d,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <reverse@plt>
mov    -0x10(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%ebx
neg    %ebx
mov    %edi,0x4(%rsp)
cmovs  %edi,%ebx
lea    0xeb0(%rip),%r15
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebx,%r14
imul   $0x66666667,%r14,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x22,%rbx
add    %eax,%ebx
lea    (%rbx,%rbx,1),%eax
lea    (%rax,%rax,4),%eax
mov    %r14d,%ebp
sub    %eax,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x30,%bpl
mov    %bpl,(%r12,%r13,1)
add    $0x1,%r13
cmp    $0x9,%r14d
jg     1160 <itoa+0x30>
cmpl   $0x0,0x4(%rsp)
jns    11b3 <itoa+0x83>
mov    %r13d,%eax
add    $0x1,%r13d
movb   $0x2d,(%r12,%rax,1)
movslq %r13d,%rsi
movb   $0x0,(%r12,%rsi,1)
mov    %r12,%rdi
call   1030 <reverse@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @itoa(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* %3
store i32 %7, i32* %6
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* %3
%11 = sub nsw i32 0, %10
store i32 %11, i32* %3
br label %12
12: ; preds = %9, %2
store i32 0, i32* %5
br label %13
13: ; preds = %26, %12
%14 = load i32, i32* %3
%15 = srem i32 %14, 10
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %3
%18 = srem i32 %17, 10
%19 = add nsw i32 %18, 48
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
store i32 %23, i32* %5
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
br label %26
26: ; preds = %13
%27 = load i32, i32* %3
%28 = sdiv i32 %27, 10
store i32 %28, i32* %3
%29 = icmp sgt i32 %28, 0
br i1 %29, label %13, label %30
30: ; preds = %26
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i8*, i8** %4
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 45, i8* %38
br label %39
39: ; preds = %33, %30
%40 = load i8*, i8** %4
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %4
%45 = load i32, i32* %5
%46 = call i32 @reverse(i8* %44, i32 %45)
%47 = load i8*, i8** %4
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %47)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define void @itoa(i32 %0, i8* %1) {
%3 = icmp slt i32 %0, 0
%4 = sub nsw i32 0, %0
%5 = select i1 %3, i32 %4, i32 %0
br label %6
6: ; preds = %6, %2
%7 = phi i64 [ %13, %6 ], [ 0, %2 ]
%8 = phi i32 [ %15, %6 ], [ %5, %2 ]
%9 = srem i32 %8, 10
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = trunc i32 %9 to i8
%12 = add nsw i8 %11, 48
%13 = add nuw i64 %7, 1
%14 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %12, i8* %14
%15 = sdiv i32 %8, 10
%16 = icmp sgt i32 %8, 9
br i1 %16, label %6, label %17
17: ; preds = %6
%18 = trunc i64 %13 to i32
br i1 %3, label %19, label %24
19: ; preds = %17
%20 = trunc i64 %7 to i32
%21 = add nuw nsw i32 %20, 2
%22 = and i64 %13, 4294967295
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 45, i8* %23
br label %24
24: ; preds = %19, %17
%25 = phi i32 [ %21, %19 ], [ %18, %17 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 0, i8* %27
%28 = tail call i32 @reverse(i8* %1, i32 %25)
%29 = tail call i32 @puts(i8* dereferenceable(1) %1)
ret void
}