#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 strlen(char *);
int trimTrailingPolyA(char *str) {
  int n;
  int trimmed = 0;
  n = strlen(str) - 1;
  while (str[n] == 'a') {
    str[n] = '\0';
    n--;
    trimmed++;
  }
  return trimmed;
}


x86 assembly (clang, -O0):
<trimTrailingPolyA>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x61,%eax
jne    116a <trimTrailingPolyA+0x5a>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1132 <trimTrailingPolyA+0x22>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trimTrailingPolyA>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
movslq %eax,%rcx
xor    %eax,%eax
cmpb   $0x61,-0x1(%rcx,%rbx,1)
jne    1141 <trimTrailingPolyA+0x31>
add    %rbx,%rcx
add    $0xffffffffffffffff,%rcx
xor    %eax,%eax
xchg   %ax,%ax
movb   $0x0,(%rcx,%rax,1)
cmpb   $0x61,-0x1(%rcx,%rax,1)
lea    -0x1(%rax),%rax
je     1130 <trimTrailingPolyA+0x20>
neg    %eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @trimTrailingPolyA(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = sub nsw i32 %6, 1
store i32 %7, i32* %3
br label %8
8: ; preds = %16, %1
%9 = load i8*, i8** %2
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 97
br i1 %15, label %16, label %25
16: ; preds = %8
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 0, i8* %20
%21 = load i32, i32* %3
%22 = add nsw i32 %21, -1
store i32 %22, i32* %3
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %8
25: ; preds = %8
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
define i32 @trimTrailingPolyA(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 97
br i1 %7, label %8, label %17
8: ; preds = %1, %8
%9 = phi i64 [ %13, %8 ], [ %4, %1 ]
%10 = phi i8* [ %14, %8 ], [ %5, %1 ]
%11 = phi i32 [ %12, %8 ], [ 0, %1 ]
store i8 0, i8* %10
%12 = add nuw nsw i32 %11, 1
%13 = add i64 %9, -1
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 97
br i1 %16, label %8, label %17
17: ; preds = %8, %1
%18 = phi i32 [ 0, %1 ], [ %12, %8 ]
ret i32 %18
}