#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*/ monitor_put(char);
void monitor_write_dec(unsigned int d) {
  int n;
  int top = -1;
  char *digits = "0123456789ABCDEF";
  char stack[10];
  while (d > 0) {
    n = d % 10;
    stack[++top] = digits[n];
    d /= 10;
  }
  if (top == -1)
    monitor_put(digits[0]);
  while (top >= 0) {
    monitor_put(stack[top--]);
  }
}


x86 assembly (clang, -O0):
<monitor_write_dec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0xc(%rbp)
lea    0xed7(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
jbe    1174 <monitor_write_dec+0x64>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0x8(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cltq
mov    %cl,-0x22(%rbp,%rax,1)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x4(%rbp)
jmp    112d <monitor_write_dec+0x1d>
cmpl   $0xffffffff,-0xc(%rbp)
jne    118a <monitor_write_dec+0x7a>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <monitor_put@plt>
jmp    118f <monitor_write_dec+0x7f>
cmpl   $0x0,-0xc(%rbp)
jl     11b5 <monitor_write_dec+0xa5>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cltq
movsbl -0x22(%rbp,%rax,1),%edi
call   1030 <monitor_put@plt>
jmp    118f <monitor_write_dec+0x7f>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monitor_write_dec>:
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     1186 <monitor_write_dec+0x76>
mov    $0xffffffffffffffff,%rax
mov    $0xcccccccd,%r8d
lea    0xed3(%rip),%rdx
nopl   (%rax)
mov    %edi,%esi
imul   %r8,%rsi
shr    $0x23,%rsi
lea    (%rsi,%rsi,1),%ebx
lea    (%rbx,%rbx,4),%ebx
mov    %edi,%ecx
sub    %ebx,%ecx
movzbl (%rcx,%rdx,1),%ecx
mov    %cl,0x7(%rsp,%rax,1)
add    $0x1,%rax
cmp    $0x9,%edi
mov    %esi,%edi
ja     1130 <monitor_write_dec+0x20>
cmp    $0xffffffff,%eax
je     1186 <monitor_write_dec+0x76>
test   %eax,%eax
js     1180 <monitor_write_dec+0x70>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl 0x6(%rsp,%rbx,1),%edi
call   1030 <monitor_put@plt>
add    $0xffffffffffffffff,%rbx
jb     1170 <monitor_write_dec+0x60>
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x30,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <monitor_put@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define void @monitor_write_dec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [10 x i8]
store i32 %0, i32* %2
store i32 -1, i32* %4
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %5
br label %7
7: ; preds = %10, %1
%8 = load i32, i32* %2
%9 = icmp ugt i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = load i32, i32* %2
%12 = urem i32 %11, 10
store i32 %12, i32* %3
%13 = load i8*, i8** %5
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 %20
store i8 %17, i8* %21
%22 = load i32, i32* %2
%23 = udiv i32 %22, 10
store i32 %23, i32* %2
br label %7
24: ; preds = %7
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = call i32 @monitor_put(i8 signext %30)
br label %32
32: ; preds = %27, %24
br label %33
33: ; preds = %36, %32
%34 = load i32, i32* %4
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i32, i32* %4
%38 = add nsw i32 %37, -1
store i32 %38, i32* %4
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = call i32 @monitor_put(i8 signext %41)
br label %33
43: ; preds = %33
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define void @monitor_write_dec(i32 %0) {
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%4 = icmp eq i32 %0, 0
br i1 %4, label %19, label %5
5: ; preds = %1, %5
%6 = phi i64 [ %12, %5 ], [ -1, %1 ]
%7 = phi i32 [ %14, %5 ], [ %0, %1 ]
%8 = urem i32 %7, 10
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %9
%11 = load i8, i8* %10
%12 = add nsw i64 %6, 1
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %12
store i8 %11, i8* %13
%14 = udiv i32 %7, 10
%15 = icmp ult i32 %7, 10
br i1 %15, label %16, label %5
16: ; preds = %5
%17 = trunc i64 %12 to i32
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16, %1
%20 = tail call i32 @monitor_put(i8 signext 48)
br label %32
21: ; preds = %16
%22 = icmp sgt i32 %17, -1
br i1 %22, label %23, label %32
23: ; preds = %21
%24 = and i64 %12, 4294967295
br label %25
25: ; preds = %23, %25
%26 = phi i64 [ %24, %23 ], [ %27, %25 ]
%27 = add nsw i64 %26, -1
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 %26
%29 = load i8, i8* %28
%30 = tail call i32 @monitor_put(i8 signext %29)
%31 = icmp eq i64 %26, 0
br i1 %31, label %32, label %25
32: ; preds = %25, %19, %21
ret void
}