#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);
const char *ordinal_string(int n) {
  static char buffer[3][20];
  static int i;
  ++i, i %= 3;
  char *buf = buffer[i];
  if (n == 1 || n == 0) {
    return "first";
  } else if (n == 2) {
    return "second";
  } else if (n == 3) {
    return "third";
  } else if (n % 10 == 1) {
    sprintf(buf, "%dst", n);
  } else if (n % 10 == 2) {
    sprintf(buf, "%dnd", n);
  } else if (n % 10 == 3) {
    sprintf(buf, "%drd", n);
  } else {
    sprintf(buf, "%dth", n);
  }
  return buffer[i];
}


x86 assembly (clang, -O0):
<ordinal_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2f5b(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2f52(%rip)
mov    0x2f4c(%rip),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,0x2f3e(%rip)
movslq 0x2f37(%rip),%rcx
lea    0x2ef4(%rip),%rax
imul   $0x14,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0xc(%rbp)
je     116b <ordinal_string+0x5b>
cmpl   $0x0,-0xc(%rbp)
jne    117b <ordinal_string+0x6b>
lea    0xe8e(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1278 <ordinal_string+0x168>
cmpl   $0x2,-0xc(%rbp)
jne    1195 <ordinal_string+0x85>
lea    0xe7a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1278 <ordinal_string+0x168>
cmpl   $0x3,-0xc(%rbp)
jne    11af <ordinal_string+0x9f>
lea    0xe67(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1278 <ordinal_string+0x168>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    11db <ordinal_string+0xcb>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe42(%rip),%rsi
call   1030 <sprintf@plt>
jmp    1250 <ordinal_string+0x140>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x2,%edx
jne    1207 <ordinal_string+0xf7>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe1b(%rip),%rsi
call   1030 <sprintf@plt>
jmp    124b <ordinal_string+0x13b>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x3,%edx
jne    1233 <ordinal_string+0x123>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xdf4(%rip),%rsi
call   1030 <sprintf@plt>
jmp    1246 <ordinal_string+0x136>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xde1(%rip),%rsi
call   1030 <sprintf@plt>
jmp    124b <ordinal_string+0x13b>
jmp    1250 <ordinal_string+0x140>
jmp    1255 <ordinal_string+0x145>
jmp    125a <ordinal_string+0x14a>
jmp    125f <ordinal_string+0x14f>
movslq 0x2e16(%rip),%rcx
lea    0x2dd3(%rip),%rax
imul   $0x14,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ordinal_string>:
push   %rbx
mov    0x2f65(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   $0x55555556,%rcx,%rcx
mov    %rcx,%rsi
shr    $0x3f,%rsi
shr    $0x20,%rcx
add    %esi,%ecx
lea    (%rcx,%rcx,2),%ecx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,0x2f39(%rip)
cmp    $0x2,%edi
jae    1151 <ordinal_string+0x41>
lea    0xeb1(%rip),%rax
pop    %rbx
ret
je     1163 <ordinal_string+0x53>
mov    %edi,%edx
cmp    $0x3,%edi
jne    116c <ordinal_string+0x5c>
lea    0xeac(%rip),%rax
pop    %rbx
ret
lea    0xe9c(%rip),%rax
pop    %rbx
ret
lea    (%rax,%rax,4),%rax
lea    0x2ec9(%rip),%rbx
lea    (%rbx,%rax,4),%rdi
movslq %edx,%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    %edx,%eax
cmp    $0x2,%eax
ja     11b2 <ordinal_string+0xa2>
cltq
lea    0xe7f(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
jmp    11b9 <ordinal_string+0xa9>
lea    0xe69(%rip),%rsi
call   1030 <sprintf@plt>
mov    0x2eb8(%rip),%eax
lea    (%rax,%rax,4),%rax
lea    (%rbx,%rax,4),%rax
pop    %rbx
ret

LLVM IR (-O0):
@ordinal_string.buffer = internal global [3 x [20 x i8]] zeroinitializer
@ordinal_string.i = internal global i32 0
@.str = [6 x i8] c"first\00"
@.str.1 = [7 x i8] c"second\00"
@.str.2 = [6 x i8] c"third\00"
@.str.3 = [5 x i8] c"%dst\00"
@.str.4 = [5 x i8] c"%dnd\00"
@.str.5 = [5 x i8] c"%drd\00"
@.str.6 = [5 x i8] c"%dth\00"
define i8* @ordinal_string(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i32, i32* @ordinal_string.i
%6 = add nsw i32 %5, 1
store i32 %6, i32* @ordinal_string.i
%7 = load i32, i32* @ordinal_string.i
%8 = srem i32 %7, 3
store i32 %8, i32* @ordinal_string.i
%9 = load i32, i32* @ordinal_string.i
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [3 x [20 x i8]], [3 x [20 x i8]]* @ordinal_string.buffer, i64 0, i64 %10
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
store i8* %12, i8** %4
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 1
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15, %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %2
br label %65
19: ; preds = %15
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %23
22: ; preds = %19
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %65
23: ; preds = %19
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %27
26: ; preds = %23
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %65
27: ; preds = %23
%28 = load i32, i32* %3
%29 = srem i32 %28, 10
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = load i32, i32* %3
%34 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %57
35: ; preds = %27
%36 = load i32, i32* %3
%37 = srem i32 %36, 10
%38 = icmp eq i32 %37, 2
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i8*, i8** %4
%41 = load i32, i32* %3
%42 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %41)
br label %56
43: ; preds = %35
%44 = load i32, i32* %3
%45 = srem i32 %44, 10
%46 = icmp eq i32 %45, 3
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i8*, i8** %4
%49 = load i32, i32* %3
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %55
51: ; preds = %43
%52 = load i8*, i8** %4
%53 = load i32, i32* %3
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %51, %47
br label %56
56: ; preds = %55, %39
br label %57
57: ; preds = %56, %31
br label %58
58: ; preds = %57
br label %59
59: ; preds = %58
br label %60
60: ; preds = %59
%61 = load i32, i32* @ordinal_string.i
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [3 x [20 x i8]], [3 x [20 x i8]]* @ordinal_string.buffer, i64 0, i64 %62
%64 = getelementptr inbounds [20 x i8], [20 x i8]* %63, i64 0, i64 0
store i8* %64, i8** %2
br label %65
65: ; preds = %60, %26, %22, %18
%66 = load i8*, i8** %2
ret i8* %66
}

LLVM IR (-O2):
@ordinal_string.buffer = internal global [3 x [20 x i8]] zeroinitializer
@ordinal_string.i = internal unnamed_addr global i32 0
@.str = [6 x i8] c"first\00"
@.str.1 = [7 x i8] c"second\00"
@.str.2 = [6 x i8] c"third\00"
@.str.3 = [5 x i8] c"%dst\00"
@.str.4 = [5 x i8] c"%dnd\00"
@.str.5 = [5 x i8] c"%drd\00"
@.str.6 = [5 x i8] c"%dth\00"
@reltable.ordinal_string = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.ordinal_string to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.4 to i64), i64 ptrtoint ([3 x i32]* @reltable.ordinal_string to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.5 to i64), i64 ptrtoint ([3 x i32]* @reltable.ordinal_string to i64)) to i32)]
define i8* @ordinal_string(i32 %0) {
%2 = load i32, i32* @ordinal_string.i
%3 = add nsw i32 %2, 1
%4 = srem i32 %3, 3
store i32 %4, i32* @ordinal_string.i
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [3 x [20 x i8]], [3 x [20 x i8]]* @ordinal_string.buffer, i64 0, i64 %5, i64 0
%7 = icmp ult i32 %0, 2
br i1 %7, label %24, label %8
8: ; preds = %1
switch i32 %0, label %10 [
i32 2, label %24
i32 3, label %9
]
9: ; preds = %8
br label %24
10: ; preds = %8
%11 = srem i32 %0, 10
%12 = add nsw i32 %11, -1
%13 = icmp ult i32 %12, 3
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = sext i32 %12 to i64
%16 = shl i64 %15, 2
%17 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.ordinal_string to i8*), i64 %16)
br label %18
18: ; preds = %10, %14
%19 = phi i8* [ %17, %14 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), %10 ]
%20 = tail call i32 @sprintf(i8* %6, i8* dereferenceable(1) %19, i32 %0)
%21 = load i32, i32* @ordinal_string.i
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [3 x [20 x i8]], [3 x [20 x i8]]* @ordinal_string.buffer, i64 0, i64 %22, i64 0
br label %24
24: ; preds = %8, %1, %18, %9
%25 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %9 ], [ %23, %18 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %8 ]
ret i8* %25
}