#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*/ strcpy(char *, char *);
const char *format_num(int num) {
  static char buf[30];
  int i;
  if (num <= 0) {
    strcpy(buf, "0");
    return buf;
  }
  i = 30 - 1;
  buf[i--] = '\0';
  do {
    buf[i--] = (num % 10) + '0';
    num /= 10;
  } while (num > 0);
  return &buf[i + 1];
}


x86 assembly (clang, -O0):
<format_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1148 <format_num+0x38>
lea    0x2f14(%rip),%rdi
lea    0xecd(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2f01(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11bd <format_num+0xad>
movl   $0x1d,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
movslq %eax,%rcx
lea    0x2edc(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
movslq %eax,%rcx
lea    0x2eb5(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1168 <format_num+0x58>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rcx
lea    0x2e8a(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_num>:
push   %rbx
test   %edi,%edi
jle    1162 <format_num+0x52>
movb   $0x0,0x2f41(%rip)
lea    0x2f3a(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %edi,%rax
imul   $0x66666667,%rax,%rdi
mov    %rdi,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rdi
add    %ecx,%edi
lea    (%rdi,%rdi,1),%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %eax,%edx
sub    %ecx,%edx
add    $0x30,%dl
mov    %dl,-0x1(%rbx)
add    $0xffffffffffffffff,%rbx
cmp    $0x9,%eax
jg     1130 <format_num+0x20>
jmp    1178 <format_num+0x68>
lea    0x2ed7(%rip),%rbx
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@format_num.buf = internal global [30 x i8] zeroinitializer
@.str = [2 x i8] c"0\00"
define i8* @format_num(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @strcpy(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 0), i8** %2
br label %33
9: ; preds = %1
store i32 29, i32* %4
%10 = load i32, i32* %4
%11 = add nsw i32 %10, -1
store i32 %11, i32* %4
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds [30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %25, %9
%15 = load i32, i32* %3
%16 = srem i32 %15, 10
%17 = add nsw i32 %16, 48
%18 = trunc i32 %17 to i8
%19 = load i32, i32* %4
%20 = add nsw i32 %19, -1
store i32 %20, i32* %4
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds [30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 %21
store i8 %18, i8* %22
%23 = load i32, i32* %3
%24 = sdiv i32 %23, 10
store i32 %24, i32* %3
br label %25
25: ; preds = %14
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 0
br i1 %27, label %14, label %28
28: ; preds = %25
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 %31
store i8* %32, i8** %2
br label %33
33: ; preds = %28, %7
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@format_num.buf = internal global [30 x i8] zeroinitializer
@.str = [2 x i8] c"0\00"
define i8* @format_num(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @strcpy(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %18
5: ; preds = %1
store i8 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 29)
br label %6
6: ; preds = %6, %5
%7 = phi i64 [ %12, %6 ], [ 28, %5 ]
%8 = phi i32 [ %14, %6 ], [ %0, %5 ]
%9 = srem i32 %8, 10
%10 = trunc i32 %9 to i8
%11 = add nsw i8 %10, 48
%12 = add i64 %7, -1
%13 = getelementptr inbounds [30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 %7
store i8 %11, i8* %13
%14 = sdiv i32 %8, 10
%15 = icmp sgt i32 %8, 9
br i1 %15, label %6, label %16
16: ; preds = %6
%17 = getelementptr inbounds [30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 %7
br label %18
18: ; preds = %16, %3
%19 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @format_num.buf, i64 0, i64 0), %3 ], [ %17, %16 ]
ret i8* %19
}