#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 *);
void rtrim(char *str) {
  unsigned char *ustr = (unsigned char *)str;
  int i = strlen(str) - 1;
  if (i < 0)
    return;
  while (i >= 0 && ustr[i] <= 32)
    i--;
  str[i + 1] = '\0';
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1142 <rtrim+0x32>
jmp    119a <rtrim+0x8a>
jmp    1147 <rtrim+0x37>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jl     116b <rtrim+0x5b>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
setle  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    117b <rtrim+0x6b>
jmp    1189 <rtrim+0x79>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1147 <rtrim+0x37>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    114e <rtrim+0x3e>
mov    %eax,%ecx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %rcx,%rcx
jle    1148 <rtrim+0x38>
add    $0xffffffffffffffff,%rcx
mov    %ecx,%edx
cmpb   $0x21,(%rbx,%rdx,1)
jb     1130 <rtrim+0x20>
add    $0x1,%rcx
mov    %rcx,%rax
cltq
movb   $0x0,(%rbx,%rax,1)
pop    %rbx
ret

LLVM IR (-O0):
define void @rtrim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %3
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
%8 = sub nsw i32 %7, 1
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
br label %35
12: ; preds = %1
br label %13
13: ; preds = %26, %12
%14 = load i32, i32* %4
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i8*, i8** %3
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp sle i32 %22, 32
br label %24
24: ; preds = %16, %13
%25 = phi i1 [ false, %13 ], [ %23, %16 ]
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = load i32, i32* %4
%28 = add nsw i32 %27, -1
store i32 %28, i32* %4
br label %13
29: ; preds = %24
%30 = load i8*, i8** %2
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %29, %11
ret void
}

LLVM IR (-O2):
define void @rtrim(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 1
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %4, %10
%7 = phi i64 [ %5, %4 ], [ %8, %10 ]
%8 = add nsw i64 %7, -1
%9 = icmp sgt i64 %7, 0
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = and i64 %8, 4294967295
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = icmp ult i8 %13, 33
br i1 %14, label %6, label %15
15: ; preds = %6, %10
%16 = phi i64 [ 0, %6 ], [ %7, %10 ]
%17 = shl i64 %16, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %1, %15
ret void
}