#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*/ _iter_itoa(int, char *, size_t);
int /*<<< orphan*/ _minmax(char *, int);
int /*<<< orphan*/ _rev_or_not(char *);
char *str_new(int);
char *str_itoa(int n) {
  char *s = str_new(12);
  if (s == NULL) {
    return (NULL);
  }
  _minmax(s, n);
  size_t i = 0;
  if (s[i] != '\0') {
    return (s);
  }
  if (n < 0) {
    s[i] = '-';
    n *= -1;
    i++;
  }
  _iter_itoa(n, s, i);
  _rev_or_not(s);
  return (s);
}


x86 assembly (clang, -O0):
<str_itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    $0xc,%edi
call   1040 <str_new@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <str_itoa+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11f1 <str_itoa+0xb1>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <_minmax@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11a7 <str_itoa+0x67>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f1 <str_itoa+0xb1>
cmpl   $0x0,-0xc(%rbp)
jge    11d0 <str_itoa+0x90>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x2d,(%rax,%rcx,1)
imul   $0xffffffff,-0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <_iter_itoa@plt>
mov    -0x18(%rbp),%rdi
call   1030 <_rev_or_not@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_itoa>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0xc,%edi
call   1040 <str_new@plt>
mov    %rax,%rbx
test   %rax,%rax
je     118a <str_itoa+0x4a>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <_minmax@plt>
cmpb   $0x0,(%rbx)
jne    118a <str_itoa+0x4a>
test   %ebp,%ebp
js     116e <str_itoa+0x2e>
xor    %edx,%edx
jmp    1178 <str_itoa+0x38>
movb   $0x2d,(%rbx)
neg    %ebp
mov    $0x1,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <_iter_itoa@plt>
mov    %rbx,%rdi
call   1030 <_rev_or_not@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @str_itoa(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i32 %0, i32* %3
%6 = call i8* @str_new(i32 12)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %41
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = load i32, i32* %3
%13 = call i32 @_minmax(i8* %11, i32 %12)
store i64 0, i64* %5
%14 = load i8*, i8** %4
%15 = load i64, i64* %5
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = load i8*, i8** %4
store i8* %21, i8** %2
br label %41
22: ; preds = %10
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 45, i8* %28
%29 = load i32, i32* %3
%30 = mul nsw i32 %29, -1
store i32 %30, i32* %3
%31 = load i64, i64* %5
%32 = add i64 %31, 1
store i64 %32, i64* %5
br label %33
33: ; preds = %25, %22
%34 = load i32, i32* %3
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = call i32 @_iter_itoa(i32 %34, i8* %35, i64 %36)
%38 = load i8*, i8** %4
%39 = call i32 @_rev_or_not(i8* %38)
%40 = load i8*, i8** %4
store i8* %40, i8** %2
br label %41
41: ; preds = %33, %20, %9
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
define i8* @str_itoa(i32 %0) {
%2 = tail call i8* @str_new(i32 12)
%3 = icmp eq i8* %2, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call i32 @_minmax(i8* %2, i32 %0)
%6 = load i8, i8* %2
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %4
%9 = icmp slt i32 %0, 0
br i1 %9, label %10, label %12
10: ; preds = %8
store i8 45, i8* %2
%11 = sub nsw i32 0, %0
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ %11, %10 ], [ %0, %8 ]
%14 = phi i64 [ 1, %10 ], [ 0, %8 ]
%15 = tail call i32 @_iter_itoa(i32 %13, i8* %2, i64 %14)
%16 = tail call i32 @_rev_or_not(i8* %2)
br label %17
17: ; preds = %12, %4, %1
ret i8* %2
}