#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*/ labs(long);
void ltoa(long l, char *s) {
  int sign = l < 0 ? -1 : 1;
  char *p = s;
  char c;
  do {
    *p++ = labs(l % 10) + '0';
  } while (l /= 10);
  if (sign < 0)
    *p++ = '-';
  *p-- = '\0';
  while (s < p) {
    c = *s;
    *s++ = *p;
    *p-- = c;
  }
}


x86 assembly (clang, -O0):
<ltoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdx
mov    $0x1,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmovl  %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rdx,%rdi
call   1030 <labs@plt>
add    $0x30,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1140 <ltoa+0x30>
cmpl   $0x0,-0x14(%rbp)
jge    11a4 <ltoa+0x94>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x2d,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11fd <ltoa+0xed>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    11b6 <ltoa+0xa6>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltoa>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
movabs $0x6666666666666667,%rbp
66 66 66
mov    %rsi,%r13
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rax
imul   %rbp
mov    %rdx,%r12
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%r12
add    %rax,%r12
lea    (%r12,%r12,1),%rax
lea    (%rax,%rax,4),%rax
mov    %rbx,%rdi
sub    %rax,%rdi
call   1030 <labs@plt>
add    $0x30,%al
mov    %al,0x0(%r13)
add    $0x1,%r13
add    $0x9,%rbx
cmp    $0x12,%rbx
mov    %r12,%rbx
ja     1140 <ltoa+0x30>
test   %r14,%r14
jns    118f <ltoa+0x7f>
movb   $0x2d,0x0(%r13)
add    $0x1,%r13
movb   $0x0,0x0(%r13)
add    $0xffffffffffffffff,%r13
cmp    %r15,%r13
jbe    11d2 <ltoa+0xc2>
add    $0x1,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl -0x1(%r15),%eax
movzbl 0x0(%r13),%ecx
mov    %cl,-0x1(%r15)
mov    %al,0x0(%r13)
add    $0xffffffffffffffff,%r13
lea    0x1(%r15),%rax
cmp    %r13,%r15
mov    %rax,%r15
jb     11b0 <ltoa+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @ltoa(i64 %0, i8* %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i8* [ %1, %2 ], [ %10, %3 ]
%5 = phi i64 [ %0, %2 ], [ %11, %3 ]
%6 = srem i64 %5, 10
%7 = tail call i32 @labs(i64 %6)
%8 = trunc i32 %7 to i8
%9 = add i8 %8, 48
%10 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %9, i8* %4
%11 = sdiv i64 %5, 10
%12 = add i64 %5, 9
%13 = icmp ult i64 %12, 19
br i1 %13, label %14, label %3
14: ; preds = %3
%15 = icmp slt i64 %0, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = getelementptr inbounds i8, i8* %4, i64 2
store i8 45, i8* %10
br label %18
18: ; preds = %16, %14
%19 = phi i8* [ %17, %16 ], [ %10, %14 ]
store i8 0, i8* %19
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = icmp ugt i8* %20, %1
br i1 %21, label %22, label %30
22: ; preds = %18, %22
%23 = phi i8* [ %28, %22 ], [ %20, %18 ]
%24 = phi i8* [ %27, %22 ], [ %1, %18 ]
%25 = load i8, i8* %24
%26 = load i8, i8* %23
%27 = getelementptr inbounds i8, i8* %24, i64 1
store i8 %26, i8* %24
store i8 %25, i8* %23
%28 = getelementptr inbounds i8, i8* %23, i64 -1
%29 = icmp ult i8* %27, %28
br i1 %29, label %22, label %30
30: ; preds = %22, %18
ret void
}