#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*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ memmove(char *, char *, int);
int snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ stderr;
int strlen(char *);
void arabic(char *buf, unsigned int n) {
  const char thousand_separator = ' ';
  if (snprintf(buf, 128, "%u", n) >= 128) {
    fprintf(stderr, "truncated number %u\n", n);
    return;
  }
  int from = strlen(buf);
  int to = from + (from - 1) / 3;
  if (from != to)
    buf[to] = 0;
  while (from != to) {
    from -= 3;
    to -= 3;
    memmove(&buf[to], &buf[from], 3);
    buf[--to] = thousand_separator;
  }
}


x86 assembly (clang, -O0):
<arabic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x20,-0xd(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x80,%esi
lea    0xe9a(%rip),%rdx
call   1040 <snprintf@plt>
cmp    $0x80,%eax
jl     1193 <arabic+0x53>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe7a(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1231 <arabic+0xf1>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     11d5 <arabic+0x95>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11da <arabic+0x9a>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1231 <arabic+0xf1>
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x3,%edx
call   1060 <memmove@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    11da <arabic+0x9a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arabic>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xeac(%rip),%rdx
mov    $0x80,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
cmp    $0x80,%eax
jl     1189 <arabic+0x49>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <fprintf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%eax
cmp    $0x5,%eax
jb     120c <arabic+0xcc>
lea    -0x1(%r15),%eax
cltq
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%r15,1),%ecx
movslq %ecx,%rcx
movb   $0x0,(%r14,%rcx,1)
movslq %r15d,%rdx
lea    (%rdx,%r14,1),%r12
add    $0xfffffffffffffffd,%r12
lea    (%r14,%rcx,1),%rbx
add    $0xfffffffffffffffc,%rbx
lea    (%r15,%rax,1),%r14d
add    $0xfffffffc,%r14d
xchg   %ax,%ax
lea    0x1(%rbx),%rdi
mov    %r12,%rsi
mov    $0x3,%edx
call   1060 <memmove@plt>
movb   $0x20,(%rbx)
add    $0xfffffffd,%r15d
add    $0xfffffffffffffffd,%r12
add    $0xfffffffffffffffc,%rbx
lea    -0x4(%r14),%eax
cmp    %r15d,%r14d
mov    %eax,%r14d
jne    11e0 <arabic+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"truncated number %u\0A\00"
define void @arabic(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8 32, i8* %5
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp sge i32 %10, 128
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* %4
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %56
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = sdiv i32 %21, 3
%23 = add nsw i32 %19, %22
store i32 %23, i32* %7
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %16
%28 = load i8*, i8** %3
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %27, %16
br label %33
33: ; preds = %37, %32
%34 = load i32, i32* %6
%35 = load i32, i32* %7
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %56
37: ; preds = %33
%38 = load i32, i32* %6
%39 = sub nsw i32 %38, 3
store i32 %39, i32* %6
%40 = load i32, i32* %7
%41 = sub nsw i32 %40, 3
store i32 %41, i32* %7
%42 = load i8*, i8** %3
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8*, i8** %3
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = call i32 @memmove(i8* %45, i8* %49, i32 3)
%51 = load i8*, i8** %3
%52 = load i32, i32* %7
%53 = add nsw i32 %52, -1
store i32 %53, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
store i8 32, i8* %55
br label %33
56: ; preds = %12, %33
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"truncated number %u\0A\00"
define void @arabic(i8* %0, i32 %1) {
%3 = tail call i32 @snprintf(i8* %0, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%4 = icmp sgt i32 %3, 127
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %33
8: ; preds = %2
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, -1
%11 = sdiv i32 %10, 3
%12 = add i32 %11, %9
%13 = add i32 %9, 1
%14 = icmp ult i32 %13, 5
br i1 %14, label %33, label %15
15: ; preds = %8
%16 = sext i32 %12 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
%18 = sext i32 %12 to i64
%19 = sext i32 %9 to i64
br label %20
20: ; preds = %15, %20
%21 = phi i64 [ %19, %15 ], [ %23, %20 ]
%22 = phi i64 [ %18, %15 ], [ %28, %20 ]
%23 = add nsw i64 %21, -3
%24 = add nsw i64 %22, -3
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = getelementptr inbounds i8, i8* %0, i64 %23
%27 = tail call i32 @memmove(i8* %25, i8* %26, i32 3)
%28 = add nsw i64 %22, -4
%29 = getelementptr inbounds i8, i8* %0, i64 %28
store i8 32, i8* %29
%30 = trunc i64 %28 to i32
%31 = trunc i64 %23 to i32
%32 = icmp eq i32 %31, %30
br i1 %32, label %33, label %20
33: ; preds = %20, %8, %5
ret void
}