#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*/ strcat(char *, char *);
int strlen(char *);
char *ordinal(int val, char *buf) {
  char *p1;
  sprintf(buf, "%d", val);
  p1 = &buf[strlen(buf) - 1];
  switch (*p1) {
  case '1':
    strcat(buf, "st");
    break;
  case '2':
    strcat(buf, "nd");
    break;
  case '3':
    strcat(buf, "rd");
    break;
  default:
    strcat(buf, "th");
    break;
  }
  return buf;
}


x86 assembly (clang, -O0):
<ordinal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xeb3(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    %rdi,-0x28(%rbp)
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    $0xffffffff,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x31,%eax
je     11ac <ordinal+0x7c>
jmp    118a <ordinal+0x5a>
mov    -0x1c(%rbp),%eax
sub    $0x32,%eax
je     11c1 <ordinal+0x91>
jmp    119b <ordinal+0x6b>
mov    -0x1c(%rbp),%eax
sub    $0x33,%eax
je     11d6 <ordinal+0xa6>
jmp    11eb <ordinal+0xbb>
mov    -0x10(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1040 <strcat@plt>
jmp    11fb <ordinal+0xcb>
mov    -0x10(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1040 <strcat@plt>
jmp    11fb <ordinal+0xcb>
mov    -0x10(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1040 <strcat@plt>
jmp    11fb <ordinal+0xcb>
mov    -0x10(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ordinal>:
push   %rbx
mov    %rsi,%rbx
mov    %edi,%edx
lea    0xec3(%rip),%rsi
mov    %rbx,%rdi
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movsbq -0x1(%rax,%rbx,1),%rax
add    $0xffffffffffffffcf,%rax
cmp    $0x2,%eax
ja     116e <ordinal+0x3e>
lea    0xeab(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
jmp    1175 <ordinal+0x45>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.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* @ordinal(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = load i32, i32* %3
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i8*, i8** %4
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %9, i64 %13
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
switch i32 %17, label %27 [
i32 49, label %18
i32 50, label %21
i32 51, label %24
]
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %30
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call i32 @strcat(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %30
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %30
27: ; preds = %2
%28 = load i8*, i8** %4
%29 = call i32 @strcat(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
%31 = load i8*, i8** %4
ret i8* %31
}

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