#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 *);
char *ordin(int n) {
  static char buffer[8];
  char *p = buffer;
  sprintf(p, "%d", n);
  while (*p != 0)
    p++;
  switch (n % 10) {
  case 1:
    strcpy(p, "st");
    break;
  case 2:
    strcpy(p, "nd");
    break;
  case 3:
    strcpy(p, "rd");
    break;
  default:
    strcpy(p, "th");
    break;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<ordin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    0x2eff(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xebc(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116a <ordin+0x4a>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1149 <ordin+0x29>
movslq -0x4(%rbp),%rcx
mov    %ecx,%eax
imul   $0x66666667,%rcx,%rdx
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rdx
add    %ecx,%edx
add    %edx,%edx
mov    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
sub    $0x1,%eax
je     11c0 <ordin+0xa0>
jmp    119e <ordin+0x7e>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
je     11d5 <ordin+0xb5>
jmp    11af <ordin+0x8f>
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
je     11ea <ordin+0xca>
jmp    11ff <ordin+0xdf>
mov    -0x10(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1030 <strcpy@plt>
jmp    120f <ordin+0xef>
mov    -0x10(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1030 <strcpy@plt>
jmp    120f <ordin+0xef>
mov    -0x10(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   1030 <strcpy@plt>
jmp    120f <ordin+0xef>
mov    -0x10(%rbp),%rdi
lea    0xe02(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2e1b(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ordin>:
push   %rbx
mov    %edi,%ebx
lea    0x2f07(%rip),%rdi
lea    0xecf(%rip),%rsi
mov    %ebx,%edx
call   1040 <sprintf@plt>
lea    0x2ef1(%rip),%rdi
nop
cmpb   $0x0,0x1(%rdi)
lea    0x1(%rdi),%rdi
jne    1140 <ordin+0x20>
movslq %ebx,%rax
imul   $0x66666667,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
not    %eax
add    %ebx,%eax
cmp    $0x2,%eax
ja     1181 <ordin+0x61>
cltq
lea    0xe98(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
jmp    1188 <ordin+0x68>
lea    0xe84(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2e9d(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
@ordin.buffer = internal global [8 x i8] zeroinitializer
@.str = [3 x i8] c"%d\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"
@.str.4 = [3 x i8] c"th\00"
define i8* @ordin(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
store i32 %0, i32* %2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @ordin.buffer, i64 0, i64 0), i8** %3
%4 = load i8*, i8** %3
%5 = load i32, i32* %2
%6 = call i32 @sprintf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %7
7: ; preds = %12, %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %3
br label %7
15: ; preds = %7
%16 = load i32, i32* %2
%17 = srem i32 %16, 10
switch i32 %17, label %27 [
i32 1, label %18
i32 2, label %21
i32 3, label %24
]
18: ; preds = %15
%19 = load i8*, i8** %3
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %30
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %30
24: ; preds = %15
%25 = load i8*, i8** %3
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %30
27: ; preds = %15
%28 = load i8*, i8** %3
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %27, %24, %21, %18
ret i8* getelementptr inbounds ([8 x i8], [8 x i8]* @ordin.buffer, i64 0, i64 0)
}

LLVM IR (-O2):
@ordin.buffer = internal global [8 x i8] zeroinitializer
@.str = [3 x i8] c"%d\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"
@.str.4 = [3 x i8] c"th\00"
@reltable.ordin = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.ordin to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.ordin to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.ordin to i64)) to i32)]
define i8* @ordin(i32 %0) {
%2 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @ordin.buffer, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %3
3: ; preds = %3, %1
%4 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @ordin.buffer, i64 0, i64 0), %1 ], [ %7, %3 ]
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
%7 = getelementptr inbounds i8, i8* %4, i64 1
br i1 %6, label %8, label %3
8: ; preds = %3
%9 = srem i32 %0, 10
%10 = add nsw i32 %9, -1
%11 = icmp ult i32 %10, 3
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = sext i32 %10 to i64
%14 = shl i64 %13, 2
%15 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.ordin to i8*), i64 %14)
br label %16
16: ; preds = %8, %12
%17 = phi i8* [ %15, %12 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %8 ]
%18 = tail call i32 @strcpy(i8* %4, i8* %17)
ret i8* getelementptr inbounds ([8 x i8], [8 x i8]* @ordin.buffer, i64 0, i64 0)
}