#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 *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *loud_ordinal(int n) {
  static char buf[16];
  sprintf(buf, "%dth", n);
  if (n < 0)
    n = -n;
  n %= 100;
  if (n > 20)
    n %= 10;
  if (n && n <= 3) {
    char *ptr = buf + strlen(buf) - 2;
    switch (n) {
    case 1:
      strcpy(ptr, "st");
      break;
    case 2:
      strcpy(ptr, "nd");
      break;
    case 3:
      strcpy(ptr, "rd");
      break;
    }
  }
  return (buf);
}


x86 assembly (clang, -O0):
<loud_ordinal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edx
lea    0x2f0b(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1050 <sprintf@plt>
cmpl   $0x0,-0x4(%rbp)
jge    1163 <loud_ordinal+0x33>
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0x4(%rbp)
cmpl   $0x14,-0x4(%rbp)
jle    1189 <loud_ordinal+0x59>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1231 <loud_ordinal+0x101>
cmpl   $0x3,-0x4(%rbp)
jg     1231 <loud_ordinal+0x101>
lea    0x2eac(%rip),%rdi
mov    %rdi,-0x20(%rbp)
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rcx
cltq
lea    -0x2(%rax,%rcx,1),%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x1,%eax
je     11f2 <loud_ordinal+0xc2>
jmp    11d0 <loud_ordinal+0xa0>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
je     1207 <loud_ordinal+0xd7>
jmp    11e1 <loud_ordinal+0xb1>
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
je     121c <loud_ordinal+0xec>
jmp    122c <loud_ordinal+0xfc>
mov    -0x10(%rbp),%rdi
lea    0xe08(%rip),%rsi
call   1030 <strcpy@plt>
jmp    122c <loud_ordinal+0xfc>
mov    -0x10(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1030 <strcpy@plt>
jmp    122c <loud_ordinal+0xfc>
mov    -0x10(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1231 <loud_ordinal+0x101>
lea    0x2e18(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loud_ordinal>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x2f14(%rip),%rdi
lea    0xebd(%rip),%rsi
mov    %ebx,%edx
call   1050 <sprintf@plt>
mov    %ebx,%eax
neg    %eax
cmovs  %ebx,%eax
imul   $0x51eb851f,%rax,%rcx
shr    $0x25,%rcx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
imul   $0xcd,%eax,%ecx
shr    $0xa,%ecx
and    $0x3e,%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %eax,%edx
sub    %cl,%dl
cmp    $0x15,%eax
movzbl %dl,%ebp
cmovb  %eax,%ebp
add    $0xffffffff,%ebp
cmp    $0x2,%ebp
ja     11b9 <loud_ordinal+0x89>
lea    0x2ec4(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%ebp
ja     11b9 <loud_ordinal+0x89>
cltq
lea    (%rax,%rbx,1),%rdi
add    $0xfffffffffffffffe,%rdi
movslq %ebp,%rax
lea    0xe63(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
call   1030 <strcpy@plt>
lea    0x2e90(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@loud_ordinal.buf = internal global [16 x i8] zeroinitializer
@.str = [5 x i8] c"%dth\00"
@.str.1 = [3 x i8] c"st\00"
@.str.2 = [3 x i8] c"nd\00"
@.str.3 = [3 x i8] c"rd\00"
define i8* @loud_ordinal(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @sprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* %2
%10 = sub nsw i32 0, %9
store i32 %10, i32* %2
br label %11
11: ; preds = %8, %1
%12 = load i32, i32* %2
%13 = srem i32 %12, 100
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp sgt i32 %14, 20
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* %2
%18 = srem i32 %17, 10
store i32 %18, i32* %2
br label %19
19: ; preds = %16, %11
%20 = load i32, i32* %2
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = load i32, i32* %2
%24 = icmp sle i32 %23, 3
br i1 %24, label %25, label %41
25: ; preds = %22
%26 = call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 0))
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 0), i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -2
store i8* %29, i8** %3
%30 = load i32, i32* %2
switch i32 %30, label %40 [
i32 1, label %31
i32 2, label %34
i32 3, label %37
]
31: ; preds = %25
%32 = load i8*, i8** %3
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %40
34: ; preds = %25
%35 = load i8*, i8** %3
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %40
37: ; preds = %25
%38 = load i8*, i8** %3
%39 = call i32 @strcpy(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %25, %37, %34, %31
br label %41
41: ; preds = %40, %22, %19
ret i8* getelementptr inbounds ([16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@loud_ordinal.buf = internal global [16 x i8] zeroinitializer
@.str = [5 x i8] c"%dth\00"
@.str.1 = [3 x i8] c"st\00"
@.str.2 = [3 x i8] c"nd\00"
@.str.3 = [3 x i8] c"rd\00"
@reltable.loud_ordinal = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.loud_ordinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.loud_ordinal to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.loud_ordinal to i64)) to i32)]
define i8* @loud_ordinal(i32 %0) {
%2 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call i32 @llvm.abs.i32(i32 %0, i1 true)
%4 = urem i32 %3, 100
%5 = icmp ugt i32 %4, 20
%6 = trunc i32 %4 to i8
%7 = urem i8 %6, 10
%8 = zext i8 %7 to i32
%9 = select i1 %5, i32 %8, i32 %4
%10 = add nsw i32 %9, -1
%11 = icmp ult i32 %10, 3
br i1 %11, label %12, label %24
12: ; preds = %1
%13 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 0))
%14 = add nsw i32 %9, -1
%15 = icmp ult i32 %14, 3
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = sext i32 %13 to i64
%18 = getelementptr inbounds [16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 %17
%19 = getelementptr inbounds i8, i8* %18, i64 -2
%20 = sext i32 %14 to i64
%21 = shl i64 %20, 2
%22 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.loud_ordinal to i8*), i64 %21)
%23 = tail call i32 @strcpy(i8* %19, i8* %22)
br label %24
24: ; preds = %12, %16, %1
ret i8* getelementptr inbounds ([16 x i8], [16 x i8]* @loud_ordinal.buf, i64 0, i64 0)
}