#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*/ strcpy(char *, char *);
int /*<<< orphan*/ strrev(char *);
char *itostr(int value, char *str) {
  char *ptr;
  int minus;
  minus = 0;
  ptr = str;
  if (value == 0) {
    str[0] = '0';
    str[1] = 0;
    return str;
  } else if (value == 0x80000000) {
    strcpy(str, "-2147483648");
    return str;
  }
  if (value < 0) {
    minus = 1;
    value = -value;
  }
  while (value > 0) {
    *ptr = (char)((value % 10) + '0');
    ptr++;
    value /= 10;
  }
  if (minus) {
    *ptr = '-';
    ptr++;
  }
  *ptr = 0;
  strrev(str);
  return str;
}


x86 assembly (clang, -O0):
<itostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1164 <itostr+0x44>
mov    -0x18(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1225 <itostr+0x105>
cmpl   $0x80000000,-0xc(%rbp)
jne    118e <itostr+0x6e>
mov    -0x18(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1225 <itostr+0x105>
jmp    1193 <itostr+0x73>
cmpl   $0x0,-0xc(%rbp)
jge    11ac <itostr+0x8c>
movl   $0x1,-0x24(%rbp)
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11b1 <itostr+0x91>
cmpl   $0x0,-0xc(%rbp)
jle    11f0 <itostr+0xd0>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
jmp    11b1 <itostr+0x91>
cmpl   $0x0,-0x24(%rbp)
je     120d <itostr+0xed>
mov    -0x20(%rbp),%rax
movb   $0x2d,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <strrev@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itostr>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x80000000,%edi
je     113b <itostr+0x1b>
test   %edi,%edi
jne    114c <itostr+0x2c>
movw   $0x30,(%r14)
jmp    11aa <itostr+0x8a>
lea    0xebe(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
jmp    11aa <itostr+0x8a>
mov    %edi,%edx
neg    %edx
cmovs  %edi,%edx
mov    %r14,%rax
test   %edx,%edx
je     1194 <itostr+0x74>
mov    $0xcccccccd,%r8d
mov    %r14,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %edx,%esi
imul   %r8,%rsi
shr    $0x23,%rsi
lea    (%rsi,%rsi,1),%ebx
lea    (%rbx,%rbx,4),%ebx
mov    %edx,%ecx
sub    %ebx,%ecx
or     $0x30,%cl
mov    %cl,(%rax)
add    $0x1,%rax
cmp    $0x9,%edx
mov    %esi,%edx
ja     1170 <itostr+0x50>
test   %edi,%edi
jns    119f <itostr+0x7f>
movb   $0x2d,(%rax)
add    $0x1,%rax
movb   $0x0,(%rax)
mov    %r14,%rdi
call   1040 <strrev@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"-2147483648\00"
define i8* @itostr(i32 %0, i8* returned %1) {
switch i32 %0, label %7 [
i32 0, label %3
i32 -2147483648, label %5
]
3: ; preds = %2
store i8 48, i8* %1
%4 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %4
br label %28
5: ; preds = %2
%6 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %28
7: ; preds = %2
%8 = icmp slt i32 %0, 0
%9 = sub nsw i32 0, %0
%10 = select i1 %8, i32 %9, i32 %0
%11 = icmp eq i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %7, %12
%13 = phi i8* [ %18, %12 ], [ %1, %7 ]
%14 = phi i32 [ %19, %12 ], [ %10, %7 ]
%15 = urem i32 %14, 10
%16 = trunc i32 %15 to i8
%17 = or i8 %16, 48
store i8 %17, i8* %13
%18 = getelementptr inbounds i8, i8* %13, i64 1
%19 = udiv i32 %14, 10
%20 = icmp ult i32 %14, 10
br i1 %20, label %21, label %12
21: ; preds = %12, %7
%22 = phi i8* [ %1, %7 ], [ %18, %12 ]
br i1 %8, label %23, label %25
23: ; preds = %21
store i8 45, i8* %22
%24 = getelementptr inbounds i8, i8* %22, i64 1
br label %25
25: ; preds = %23, %21
%26 = phi i8* [ %24, %23 ], [ %22, %21 ]
store i8 0, i8* %26
%27 = tail call i32 @strrev(i8* %1)
br label %28
28: ; preds = %25, %5, %3
ret i8* %1
}