#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 fact(int);
int findSmallerInRight(char *, int, int);
int strlen(char *);
int findRank(char *str) {
  int len = strlen(str);
  int mul = fact(len);
  int rank = 1;
  int countRight;
  int i;
  for (i = 0; i < len; ++i) {
    mul /= len - i;
    countRight = findSmallerInRight(str, i, len - 1);
    rank += countRight * mul;
  }
  return rank;
}


x86 assembly (clang, -O0):
<findRank>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <fact@plt>
mov    %eax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ac <findRank+0x7c>
mov    -0xc(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1050 <findSmallerInRight@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
imul   -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1161 <findRank+0x31>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findRank>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1040 <fact@plt>
test   %r15d,%r15d
jle    1197 <findRank+0x67>
mov    %eax,%r12d
mov    %r15d,%ebp
add    $0xffffffff,%r15d
mov    $0x1,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12d,%eax
cltd
idiv   %ebp
mov    %eax,%r12d
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
call   1050 <findSmallerInRight@plt>
imul   %r12d,%eax
add    %eax,%r13d
add    $0x1,%ebx
add    $0xffffffff,%ebp
jne    1170 <findRank+0x40>
jmp    119d <findRank+0x6d>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @findRank(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @fact(i32 %10)
store i32 %11, i32* %4
store i32 1, i32* %5
store i32 0, i32* %7
br label %12
12: ; preds = %32, %1
%13 = load i32, i32* %7
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %35
16: ; preds = %12
%17 = load i32, i32* %3
%18 = load i32, i32* %7
%19 = sub nsw i32 %17, %18
%20 = load i32, i32* %4
%21 = sdiv i32 %20, %19
store i32 %21, i32* %4
%22 = load i8*, i8** %2
%23 = load i32, i32* %7
%24 = load i32, i32* %3
%25 = sub nsw i32 %24, 1
%26 = call i32 @findSmallerInRight(i8* %22, i32 %23, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = load i32, i32* %4
%29 = mul nsw i32 %27, %28
%30 = load i32, i32* %5
%31 = add nsw i32 %30, %29
store i32 %31, i32* %5
br label %32
32: ; preds = %16
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %12
35: ; preds = %12
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
define i32 @findRank(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i32 @fact(i32 %2)
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %1
%6 = add nsw i32 %2, -1
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ 0, %5 ], [ %16, %7 ]
%9 = phi i32 [ %3, %5 ], [ %12, %7 ]
%10 = phi i32 [ 1, %5 ], [ %15, %7 ]
%11 = sub nsw i32 %2, %8
%12 = sdiv i32 %9, %11
%13 = tail call i32 @findSmallerInRight(i8* %0, i32 %8, i32 %6)
%14 = mul nsw i32 %13, %12
%15 = add nsw i32 %14, %10
%16 = add nuw nsw i32 %8, 1
%17 = icmp eq i32 %16, %2
br i1 %17, label %18, label %7
18: ; preds = %7, %1
%19 = phi i32 [ 1, %1 ], [ %15, %7 ]
ret i32 %19
}