#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 */
scalar_t__ isspace(char const);
scalar_t__ strlen(char const *);
int muggle_str_rstrip_idx(const char *str) {
  if (str == NULL) {
    return -1;
  }
  int str_len = (int)strlen(str);
  int idx = str_len - 1;
  while (isspace(str[idx])) {
    if (--idx < 0) {
      return -1;
    }
  }
  return idx;
}


x86 assembly (clang, -O0):
<muggle_str_rstrip_idx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1143 <muggle_str_rstrip_idx+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    119c <muggle_str_rstrip_idx+0x7c>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1196 <muggle_str_rstrip_idx+0x76>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1191 <muggle_str_rstrip_idx+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    119c <muggle_str_rstrip_idx+0x7c>
jmp    1158 <muggle_str_rstrip_idx+0x38>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<muggle_str_rstrip_idx>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1172 <muggle_str_rstrip_idx+0x52>
mov    %rdi,%r15
call   1030 <strlen@plt>
movslq %eax,%rbp
lea    0x1(%rbp),%rbx
add    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl -0x2(%r15,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     116f <muggle_str_rstrip_idx+0x4f>
add    $0xffffffffffffffff,%rbx
add    $0xffffffff,%ebp
cmp    $0x1,%rbx
jg     1150 <muggle_str_rstrip_idx+0x30>
jmp    1172 <muggle_str_rstrip_idx+0x52>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @muggle_str_rstrip_idx(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %18, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = shl i64 %4, 32
%6 = ashr exact i64 %5, 32
br label %7
7: ; preds = %14, %3
%8 = phi i64 [ %9, %14 ], [ %6, %3 ]
%9 = add nsw i64 %8, -1
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i64 @isspace(i8 signext %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %7
%15 = icmp slt i64 %8, 2
br i1 %15, label %18, label %7
16: ; preds = %7
%17 = trunc i64 %9 to i32
br label %18
18: ; preds = %14, %16, %1
%19 = phi i32 [ -1, %1 ], [ %17, %16 ], [ -1, %14 ]
ret i32 %19
}