#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*/ uart_write(char);
void cout_d(unsigned int val) {
  char buffer[32];
  unsigned int i = 0;
  unsigned int t;
  if (val == 0) {
    uart_write('0');
    return;
  }
  while (val != 0) {
    buffer[i++] = val % 10 + '0';
    val = val / 10;
  }
  for (t = i; t > 0; t--)
    uart_write(buffer[t - 1]);
}


x86 assembly (clang, -O0):
<cout_d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    113b <cout_d+0x2b>
mov    $0x30,%edi
call   1030 <uart_write@plt>
jmp    11b0 <cout_d+0xa0>
jmp    1140 <cout_d+0x30>
cmpl   $0x0,-0x4(%rbp)
je     1180 <cout_d+0x70>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x34(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x34(%rbp)
mov    %eax,%eax
mov    %cl,-0x30(%rbp,%rax,1)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x4(%rbp)
jmp    1140 <cout_d+0x30>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jbe    11b0 <cout_d+0xa0>
mov    -0x38(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
movsbl -0x30(%rbp,%rax,1),%edi
call   1030 <uart_write@plt>
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
jmp    1186 <cout_d+0x76>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cout_d>:
test   %edi,%edi
je     1166 <cout_d+0x56>
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
mov    $0xcccccccd,%ecx
mov    %edi,%edx
imul   %rcx,%rdx
shr    $0x23,%rdx
lea    (%rdx,%rdx,1),%esi
lea    (%rsi,%rsi,4),%esi
mov    %edi,%ebx
sub    %esi,%ebx
or     $0x30,%bl
mov    %eax,%esi
add    $0x1,%eax
mov    %bl,(%rsp,%rsi,1)
cmp    $0xa,%edi
mov    %edx,%edi
jae    1120 <cout_d+0x10>
test   %eax,%eax
je     1160 <cout_d+0x50>
mov    %eax,%ebx
nopl   0x0(%rax)
movsbl -0x1(%rsp,%rbx,1),%edi
call   1030 <uart_write@plt>
add    $0xffffffffffffffff,%rbx
jne    1150 <cout_d+0x40>
add    $0x20,%rsp
pop    %rbx
ret
mov    $0x30,%edi
jmp    1030 <uart_write@plt>

LLVM IR (-O0):
define void @cout_d(i32 %0) {
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @uart_write(i8 signext 48)
br label %40
10: ; preds = %1
br label %11
11: ; preds = %14, %10
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %11
%15 = load i32, i32* %2
%16 = urem i32 %15, 10
%17 = add i32 %16, 48
%18 = trunc i32 %17 to i8
%19 = load i32, i32* %4
%20 = add i32 %19, 1
store i32 %20, i32* %4
%21 = zext i32 %19 to i64
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %21
store i8 %18, i8* %22
%23 = load i32, i32* %2
%24 = udiv i32 %23, 10
store i32 %24, i32* %2
br label %11
25: ; preds = %11
%26 = load i32, i32* %4
store i32 %26, i32* %5
br label %27
27: ; preds = %37, %25
%28 = load i32, i32* %5
%29 = icmp ugt i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i32, i32* %5
%32 = sub i32 %31, 1
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = call i32 @uart_write(i8 signext %35)
br label %37
37: ; preds = %30
%38 = load i32, i32* %5
%39 = add i32 %38, -1
store i32 %39, i32* %5
br label %27
40: ; preds = %8, %27
ret void
}

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