#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*/ concat(char *, char *);
char itoc(int);
int /*<<< orphan*/ strcpy(char *, char *);
void itostr(char ai_string[], int ai_value) {
  int thou, hund, ten, unit;
  char temp[2];
  strcpy(ai_string, "(");
  thou = ai_value / 1000;
  hund = (ai_value - (1000 * thou)) / 100;
  ten = (ai_value - ((1000 * thou) + (100 * hund))) / 10;
  unit = ai_value - ((1000 * thou) + (100 * hund) + (10 * ten));
  temp[1] = '\0';
  if (ai_value >= 1000) {
    temp[0] = itoc(thou);
    concat(ai_string, temp);
  }
  if (ai_value >= 100) {
    temp[0] = itoc(hund);
    concat(ai_string, temp);
  }
  temp[0] = itoc(ten);
  concat(ai_string, temp);
  temp[0] = itoc(unit);
  concat(ai_string, temp);
  concat(ai_string, ")");
}


x86 assembly (clang, -O0):
<itostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb6(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
imul   $0x3e8,-0x10(%rbp),%ecx
sub    %ecx,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
imul   $0x3e8,-0x10(%rbp),%ecx
imul   $0x64,-0x14(%rbp),%edx
add    %edx,%ecx
sub    %ecx,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
imul   $0x3e8,-0x10(%rbp),%ecx
imul   $0x64,-0x14(%rbp),%edx
add    %edx,%ecx
imul   $0xa,-0x18(%rbp),%edx
add    %edx,%ecx
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
movb   $0x0,-0x1d(%rbp)
cmpl   $0x3e8,-0xc(%rbp)
jl     11d5 <itostr+0xa5>
mov    -0x10(%rbp),%edi
call   1050 <itoc@plt>
mov    %al,-0x1e(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
call   1040 <concat@plt>
cmpl   $0x64,-0xc(%rbp)
jl     11f7 <itostr+0xc7>
mov    -0x14(%rbp),%edi
call   1050 <itoc@plt>
mov    %al,-0x1e(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
call   1040 <concat@plt>
mov    -0x18(%rbp),%edi
call   1050 <itoc@plt>
mov    %al,-0x1e(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
call   1040 <concat@plt>
mov    -0x1c(%rbp),%edi
call   1050 <itoc@plt>
mov    %al,-0x1e(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
call   1040 <concat@plt>
mov    -0x8(%rbp),%rdi
lea    0xdd0(%rip),%rsi
call   1040 <concat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itostr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xeb7(%rip),%rsi
call   1030 <strcpy@plt>
movslq %r14d,%rax
imul   $0x10624dd3,%rax,%rdi
mov    %rdi,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rdi
add    %ecx,%edi
imul   $0x3e8,%edi,%ecx
mov    %eax,%edx
sub    %ecx,%edx
movslq %edx,%rdx
imul   $0x51eb851f,%rdx,%rbp
mov    %rbp,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rbp
add    %edx,%ebp
imul   $0x64,%ebp,%edx
add    %ecx,%edx
mov    %eax,%r12d
sub    %edx,%r12d
movslq %r12d,%rcx
imul   $0x66666667,%rcx,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rbx
add    %ecx,%ebx
movb   $0x0,0xf(%rsp)
cmp    $0x3e8,%eax
jl     11cc <itostr+0x9c>
call   1050 <itoc@plt>
mov    %al,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %r15,%rdi
call   1040 <concat@plt>
jmp    11d2 <itostr+0xa2>
cmp    $0x64,%r14d
jl     11ea <itostr+0xba>
mov    %ebp,%edi
call   1050 <itoc@plt>
mov    %al,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %r15,%rdi
call   1040 <concat@plt>
lea    (%rbx,%rbx,1),%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%r12d
mov    %ebx,%edi
call   1050 <itoc@plt>
mov    %al,0xe(%rsp)
lea    0xe(%rsp),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <concat@plt>
mov    %r12d,%edi
call   1050 <itoc@plt>
mov    %al,0xe(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <concat@plt>
lea    0xdd6(%rip),%rsi
mov    %r15,%rdi
call   1040 <concat@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"(\00"
@.str.1 = [2 x i8] c")\00"
define void @itostr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i8*, i8** %3
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %4
%13 = sdiv i32 %12, 1000
store i32 %13, i32* %5
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = mul nsw i32 1000, %15
%17 = sub nsw i32 %14, %16
%18 = sdiv i32 %17, 100
store i32 %18, i32* %6
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = mul nsw i32 1000, %20
%22 = load i32, i32* %6
%23 = mul nsw i32 100, %22
%24 = add nsw i32 %21, %23
%25 = sub nsw i32 %19, %24
%26 = sdiv i32 %25, 10
store i32 %26, i32* %7
%27 = load i32, i32* %4
%28 = load i32, i32* %5
%29 = mul nsw i32 1000, %28
%30 = load i32, i32* %6
%31 = mul nsw i32 100, %30
%32 = add nsw i32 %29, %31
%33 = load i32, i32* %7
%34 = mul nsw i32 10, %33
%35 = add nsw i32 %32, %34
%36 = sub nsw i32 %27, %35
store i32 %36, i32* %8
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 1
store i8 0, i8* %37
%38 = load i32, i32* %4
%39 = icmp sge i32 %38, 1000
br i1 %39, label %40, label %47
40: ; preds = %2
%41 = load i32, i32* %5
%42 = call signext i8 @itoc(i32 %41)
%43 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 %42, i8* %43
%44 = load i8*, i8** %3
%45 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%46 = call i32 @concat(i8* %44, i8* %45)
br label %47
47: ; preds = %40, %2
%48 = load i32, i32* %4
%49 = icmp sge i32 %48, 100
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32, i32* %6
%52 = call signext i8 @itoc(i32 %51)
%53 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 %52, i8* %53
%54 = load i8*, i8** %3
%55 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%56 = call i32 @concat(i8* %54, i8* %55)
br label %57
57: ; preds = %50, %47
%58 = load i32, i32* %7
%59 = call signext i8 @itoc(i32 %58)
%60 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 %59, i8* %60
%61 = load i8*, i8** %3
%62 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%63 = call i32 @concat(i8* %61, i8* %62)
%64 = load i32, i32* %8
%65 = call signext i8 @itoc(i32 %64)
%66 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 %65, i8* %66
%67 = load i8*, i8** %3
%68 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%69 = call i32 @concat(i8* %67, i8* %68)
%70 = load i8*, i8** %3
%71 = call i32 @concat(i8* %70, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"(\00"
@.str.1 = [2 x i8] c")\00"
define void @itostr(i8* %0, i32 %1) {
%3 = alloca [2 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = sdiv i32 %1, 1000
%7 = mul nsw i32 %6, 1000
%8 = srem i32 %1, 1000
%9 = sdiv i32 %8, 100
%10 = mul nsw i32 %9, 100
%11 = add nsw i32 %10, %7
%12 = sub i32 %1, %11
%13 = sdiv i32 %12, 10
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 0, i8* %14
%15 = icmp sgt i32 %1, 999
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call signext i8 @itoc(i32 %6)
store i8 %17, i8* %4
%18 = call i32 @concat(i8* %0, i8* %4)
br label %21
19: ; preds = %2
%20 = icmp sgt i32 %1, 99
br i1 %20, label %21, label %24
21: ; preds = %16, %19
%22 = call signext i8 @itoc(i32 %9)
store i8 %22, i8* %4
%23 = call i32 @concat(i8* %0, i8* %4)
br label %24
24: ; preds = %21, %19
%25 = mul nsw i32 %13, -10
%26 = add i32 %12, %25
%27 = call signext i8 @itoc(i32 %13)
store i8 %27, i8* %4
%28 = call i32 @concat(i8* %0, i8* %4)
%29 = call signext i8 @itoc(i32 %26)
store i8 %29, i8* %4
%30 = call i32 @concat(i8* %0, i8* %4)
%31 = call i32 @concat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret void
}