#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*/ reverse(char *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void itoa(int n, char s[]) {
  if (n == 0) {
    s[0] = '0';
    s[1] = '\0';
    return;
  }
  char t[100];
  int nn;
  int i = 0;
  int sign = 1;
  if (n < 0)
    sign = -1;
  s[0] = sign * (n % 10) + '0';
  s[1] = '\0';
  if ((nn = sign * (n / 10)) == 0)
    return;
  itoa(nn, t);
  reverse(t);
  strcat(s, t);
  i = strlen(s);
  if (sign == -1)
    s[i++] = '-';
  s[i] = '\0';
  reverse(s);
  return;
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1170 <itoa+0x30>
mov    -0x10(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    1276 <itoa+0x136>
movl   $0x0,-0x88(%rbp)
movl   $0x1,-0x8c(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1198 <itoa+0x58>
movl   $0xffffffff,-0x8c(%rbp)
ff ff ff
mov    -0x8c(%rbp),%eax
mov    %eax,-0x94(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    -0x94(%rbp),%eax
imul   %edx,%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8c(%rbp),%eax
mov    %eax,-0x90(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x90(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x84(%rbp)
cmp    $0x0,%eax
jne    1201 <itoa+0xc1>
jmp    1276 <itoa+0x136>
mov    -0x84(%rbp),%edi
lea    -0x80(%rbp),%rsi
call   1060 <itoa@plt>
lea    -0x80(%rbp),%rdi
call   1030 <reverse@plt>
mov    -0x10(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0xffffffff,-0x8c(%rbp)
jne    125e <itoa+0x11e>
mov    -0x10(%rbp),%rax
mov    -0x88(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x88(%rbp)
movslq %ecx,%rcx
movb   $0x2d,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x88(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <reverse@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %rsi,%rbx
test   %edi,%edi
je     11d9 <itoa+0x99>
mov    %edi,%ebp
mov    %edi,%eax
not    %eax
shr    $0x1f,%eax
add    %eax,%eax
add    $0xffffffff,%eax
movslq %edi,%rcx
imul   $0x66666667,%rcx,%rdi
mov    %rdi,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rdi
add    %ecx,%edi
lea    (%rdi,%rdi,1),%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %ebp,%edx
sub    %ecx,%edx
imul   %eax,%edx
add    $0x30,%dl
mov    %dl,(%rbx)
movb   $0x0,0x1(%rbx)
imul   %eax,%edi
test   %edi,%edi
je     11de <itoa+0x9e>
mov    %rsp,%r14
mov    %r14,%rsi
call   1060 <itoa@plt>
mov    %r14,%rdi
call   1030 <reverse@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %ebp,%ebp
jns    11c9 <itoa+0x89>
movslq %eax,%rcx
add    $0x1,%eax
movb   $0x2d,(%rbx,%rcx,1)
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1030 <reverse@plt>
jmp    11de <itoa+0x9e>
movw   $0x30,(%rbx)
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @itoa(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [100 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 48, i8* %13
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 0, i8* %15
br label %62
16: ; preds = %2
store i32 0, i32* %7
store i32 1, i32* %8
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 -1, i32* %8
br label %20
20: ; preds = %19, %16
%21 = load i32, i32* %8
%22 = load i32, i32* %3
%23 = srem i32 %22, 10
%24 = mul nsw i32 %21, %23
%25 = add nsw i32 %24, 48
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 0
store i8 %26, i8* %28
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8 0, i8* %30
%31 = load i32, i32* %8
%32 = load i32, i32* %3
%33 = sdiv i32 %32, 10
%34 = mul nsw i32 %31, %33
store i32 %34, i32* %6
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %20
br label %62
37: ; preds = %20
%38 = load i32, i32* %6
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%41 = call i32 @reverse(i8* %40)
%42 = load i8*, i8** %4
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%44 = call i32 @strcat(i8* %42, i8* %43)
%45 = load i8*, i8** %4
%46 = call i32 @strlen(i8* %45)
store i32 %46, i32* %7
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %37
%50 = load i8*, i8** %4
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8 45, i8* %54
br label %55
55: ; preds = %49, %37
%56 = load i8*, i8** %4
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %4
%61 = call i32 @reverse(i8* %60)
br label %62
62: ; preds = %55, %36, %11
ret void
}

LLVM IR (-O2):
define void @itoa(i32 %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = icmp eq i32 %0, 0
br i1 %4, label %5, label %7
5: ; preds = %2
store i8 48, i8* %1
%6 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %6
br label %33
7: ; preds = %2
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%9 = icmp slt i32 %0, 0
%10 = select i1 %9, i32 -1, i32 1
%11 = srem i32 %0, 10
%12 = mul nsw i32 %10, %11
%13 = trunc i32 %12 to i8
%14 = add nsw i8 %13, 48
store i8 %14, i8* %1
%15 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %15
%16 = sdiv i32 %0, 10
%17 = mul nsw i32 %10, %16
%18 = icmp eq i32 %17, 0
br i1 %18, label %32, label %19
19: ; preds = %7
%20 = call i32 @reverse(i8* %8)
%21 = call i32 @strcat(i8* %1, i8* %8)
%22 = call i32 @strlen(i8* %1)
br i1 %9, label %23, label %27
23: ; preds = %19
%24 = add nsw i32 %22, 1
%25 = sext i32 %22 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 45, i8* %26
br label %27
27: ; preds = %23, %19
%28 = phi i32 [ %24, %23 ], [ %22, %19 ]
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
store i8 0, i8* %30
%31 = call i32 @reverse(i8* %1)
br label %32
32: ; preds = %7, %27
br label %33
33: ; preds = %32, %5
ret void
}