#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
char *memcpy(char *, char *, int);
char *ltoa(long value, char *string, int radix) {
  char buf[80], *p = buf + sizeof(buf);
  char sign = 0;
  unsigned long uval;
  if (radix < 2 || radix > 36) {
    errno = EINVAL;
    return NULL;
  }
  if (value < 0) {
    sign = '-';
    uval = -value;
  } else
    uval = value;
  *--p = 0;
  do {
    *--p = "0123456789abcdefghijklmnopqrstuvwxyz"[uval % radix];
    uval /= radix;
  } while (uval != 0);
  if (sign)
    *--p = sign;
  return memcpy(string, p, buf + sizeof(buf) - p);
}


x86 assembly (clang, -O0):
<ltoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x70(%rbp),%rax
add    $0x50,%rax
mov    %rax,-0x78(%rbp)
movb   $0x0,-0x79(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jl     114a <ltoa+0x3a>
cmpl   $0x24,-0x1c(%rbp)
jle    1169 <ltoa+0x59>
mov    0x2e87(%rip),%rax
mov    (%rax),%ecx
mov    0x2e86(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    123a <ltoa+0x12a>
cmpq   $0x0,-0x10(%rbp)
jge    118a <ltoa+0x7a>
movb   $0x2d,-0x79(%rbp)
xor    %eax,%eax
sub    -0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
jmp    1195 <ltoa+0x85>
mov    -0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x78(%rbp)
movb   $0x0,-0x1(%rax)
mov    -0x88(%rbp),%rax
movslq -0x1c(%rbp),%rcx
xor    %edx,%edx
div    %rcx
lea    0xe41(%rip),%rax
mov    (%rax,%rdx,1),%cl
mov    -0x78(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x78(%rbp)
mov    %cl,-0x1(%rax)
movslq -0x1c(%rbp),%rcx
mov    -0x88(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    11a8 <ltoa+0x98>
cmpb   $0x0,-0x79(%rbp)
je     1218 <ltoa+0x108>
mov    -0x79(%rbp),%cl
mov    -0x78(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x78(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x78(%rbp),%rsi
lea    -0x70(%rbp),%rax
add    $0x50,%rax
mov    -0x78(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <memcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltoa>:
sub    $0x58,%rsp
lea    -0x25(%rdx),%eax
cmp    $0xffffffdc,%eax
ja     1135 <ltoa+0x25>
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
mov    0x2eb4(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x58,%rsp
ret
lea    0x50(%rsp),%r8
mov    %rdi,%rcx
neg    %rcx
cmovs  %rdi,%rcx
movb   $0x0,0x4f(%rsp)
mov    %edx,%r10d
lea    0x4e(%rsp),%r9
lea    0xea8(%rip),%r11
jmp    117c <ltoa+0x6c>
nopw   0x0(%rax,%rax,1)
mov    %rcx,%rax
xor    %edx,%edx
div    %r10
movzbl (%rdx,%r11,1),%edx
mov    %dl,(%r9)
add    $0xffffffffffffffff,%r9
cmp    %r10,%rcx
mov    %rax,%rcx
jb     118e <ltoa+0x7e>
mov    %rcx,%rax
shr    $0x20,%rax
jne    1160 <ltoa+0x50>
mov    %ecx,%eax
xor    %edx,%edx
div    %r10d
jmp    1168 <ltoa+0x58>
test   %rdi,%rdi
js     1199 <ltoa+0x89>
add    $0x1,%r9
jmp    119d <ltoa+0x8d>
movb   $0x2d,(%r9)
sub    %r9d,%r8d
mov    %rsi,%rdi
mov    %r9,%rsi
mov    %r8d,%edx
call   1030 <memcpy@plt>
add    $0x58,%rsp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [37 x i8] c"0123456789abcdefghijklmnopqrstuvwxyz\00"
define i8* @ltoa(i64 %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [80 x i8]
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i64
store i64 %0, i64* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%13 = getelementptr inbounds i8, i8* %12, i64 80
store i8* %13, i8** %9
store i8 0, i8* %10
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 2
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 36
br i1 %18, label %19, label %21
19: ; preds = %16, %3
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
store i8* null, i8** %4
br label %66
21: ; preds = %16
%22 = load i64, i64* %5
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
store i8 45, i8* %10
%25 = load i64, i64* %5
%26 = sub nsw i64 0, %25
store i64 %26, i64* %11
br label %29
27: ; preds = %21
%28 = load i64, i64* %5
store i64 %28, i64* %11
br label %29
29: ; preds = %27, %24
%30 = load i8*, i8** %9
%31 = getelementptr inbounds i8, i8* %30, i32 -1
store i8* %31, i8** %9
store i8 0, i8* %31
br label %32
32: ; preds = %45, %29
%33 = load i64, i64* %11
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = urem i64 %33, %35
%37 = getelementptr inbounds [37 x i8], [37 x i8]* @.str, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = load i8*, i8** %9
%40 = getelementptr inbounds i8, i8* %39, i32 -1
store i8* %40, i8** %9
store i8 %38, i8* %40
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = load i64, i64* %11
%44 = udiv i64 %43, %42
store i64 %44, i64* %11
br label %45
45: ; preds = %32
%46 = load i64, i64* %11
%47 = icmp ne i64 %46, 0
br i1 %47, label %32, label %48
48: ; preds = %45
%49 = load i8, i8* %10
%50 = icmp ne i8 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i8, i8* %10
%53 = load i8*, i8** %9
%54 = getelementptr inbounds i8, i8* %53, i32 -1
store i8* %54, i8** %9
store i8 %52, i8* %54
br label %55
55: ; preds = %51, %48
%56 = load i8*, i8** %6
%57 = load i8*, i8** %9
%58 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%59 = getelementptr inbounds i8, i8* %58, i64 80
%60 = load i8*, i8** %9
%61 = ptrtoint i8* %59 to i64
%62 = ptrtoint i8* %60 to i64
%63 = sub i64 %61, %62
%64 = trunc i64 %63 to i32
%65 = call i8* @memcpy(i8* %56, i8* %57, i32 %64)
store i8* %65, i8** %4
br label %66
66: ; preds = %55, %19
%67 = load i8*, i8** %4
ret i8* %67
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [37 x i8] c"0123456789abcdefghijklmnopqrstuvwxyz\00"
define i8* @ltoa(i64 %0, i8* %1, i32 %2) {
%4 = alloca [80 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 80
%7 = add i32 %2, -37
%8 = icmp ult i32 %7, -35
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
br label %36
11: ; preds = %3
%12 = icmp slt i64 %0, 0
%13 = sub nsw i64 0, %0
%14 = select i1 %12, i64 %13, i64 %0
%15 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 79
store i8 0, i8* %15
%16 = zext i32 %2 to i64
br label %17
17: ; preds = %17, %11
%18 = phi i8* [ %15, %11 ], [ %23, %17 ]
%19 = phi i64 [ %14, %11 ], [ %24, %17 ]
%20 = urem i64 %19, %16
%21 = getelementptr inbounds [37 x i8], [37 x i8]* @.str, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = getelementptr inbounds i8, i8* %18, i64 -1
store i8 %22, i8* %23
%24 = udiv i64 %19, %16
%25 = icmp ult i64 %19, %16
br i1 %25, label %26, label %17
26: ; preds = %17
br i1 %12, label %27, label %29
27: ; preds = %26
%28 = getelementptr inbounds i8, i8* %18, i64 -2
store i8 45, i8* %28
br label %29
29: ; preds = %27, %26
%30 = phi i8* [ %28, %27 ], [ %23, %26 ]
%31 = ptrtoint i8* %6 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
%35 = call i8* @memcpy(i8* %1, i8* %30, i32 %34)
br label %36
36: ; preds = %29, %9
%37 = phi i8* [ null, %9 ], [ %35, %29 ]
ret i8* %37
}