#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*/ sprintf(char *, char *, unsigned long, ...);
void sprint_unsignedlong(char *buffer, unsigned long n) {
  unsigned long a = n;
  unsigned int exposant = 0;
  while (a > 1000) {
    a /= 1000;
    exposant += 3;
  }
  if (exposant <= 3)
    sprintf(buffer, "%lu", n);
  else
    sprintf(buffer, "%lu*10^%d", a, exposant);
}


x86 assembly (clang, -O0):
<sprint_unsignedlong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x3e8,-0x18(%rbp)
jbe    115d <sprint_unsignedlong+0x4d>
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x1c(%rbp)
jmp    112f <sprint_unsignedlong+0x1f>
cmpl   $0x3,-0x1c(%rbp)
ja     1182 <sprint_unsignedlong+0x72>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe8a(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    119b <sprint_unsignedlong+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sprint_unsignedlong>:
mov    %rsi,%r8
cmp    $0x3e9,%rsi
jb     1152 <sprint_unsignedlong+0x42>
xor    %ecx,%ecx
movabs $0x20c49ba5e353f7cf,%r9
9b c4 20
mov    %r8,%rdx
nopl   0x0(%rax,%rax,1)
mov    %rdx,%rsi
mov    %rdx,%rax
shr    $0x3,%rax
mul    %r9
shr    $0x4,%rdx
add    $0x3,%ecx
cmp    $0xf4627,%rsi
ja     1130 <sprint_unsignedlong+0x20>
cmp    $0x3,%ecx
ja     1163 <sprint_unsignedlong+0x53>
lea    0xea7(%rip),%rsi
mov    %r8,%rdx
xor    %eax,%eax
jmp    1030 <sprintf@plt>
lea    0xe9a(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%lu\00"
@.str.1 = [10 x i8] c"%lu*10^%d\00"
define void @sprint_unsignedlong(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
store i64 %7, i64* %5
store i32 0, i32* %6
br label %8
8: ; preds = %11, %2
%9 = load i64, i64* %5
%10 = icmp ugt i64 %9, 1000
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load i64, i64* %5
%13 = udiv i64 %12, 1000
store i64 %13, i64* %5
%14 = load i32, i32* %6
%15 = add i32 %14, 3
store i32 %15, i32* %6
br label %8
16: ; preds = %8
%17 = load i32, i32* %6
%18 = icmp ule i32 %17, 3
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = load i64, i64* %4
%22 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %21)
br label %28
23: ; preds = %16
%24 = load i8*, i8** %3
%25 = load i64, i64* %5
%26 = load i32, i32* %6
%27 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %25, i32 %26)
br label %28
28: ; preds = %23, %19
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lu\00"
@.str.1 = [10 x i8] c"%lu*10^%d\00"
define void @sprint_unsignedlong(i8* nocapture writeonly %0, i64 %1) {
%3 = icmp ugt i64 %1, 1000
br i1 %3, label %4, label %12
4: ; preds = %2, %4
%5 = phi i32 [ %8, %4 ], [ 0, %2 ]
%6 = phi i64 [ %7, %4 ], [ %1, %2 ]
%7 = udiv i64 %6, 1000
%8 = add i32 %5, 3
%9 = icmp ugt i64 %6, 1000999
br i1 %9, label %4, label %10
10: ; preds = %4
%11 = icmp ult i32 %8, 4
br i1 %11, label %12, label %14
12: ; preds = %2, %10
%13 = tail call i32 (i8*, i8*, i64, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %1)
br label %16
14: ; preds = %10
%15 = tail call i32 (i8*, i8*, i64, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 %7, i32 %8)
br label %16
16: ; preds = %14, %12
ret void
}