#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(unsigned char);
long strlen(char *);
void util_str_rstrip(char *str) {
  long len, i;
  len = strlen(str);
  i = len - 1;
  while (i >= 0 && isspace((unsigned char)str[i])) {
    str[i] = '\0';
    i--;
  }
}


x86 assembly (clang, -O0):
<util_str_rstrip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jl     1170 <util_str_rstrip+0x50>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1180 <util_str_rstrip+0x60>
jmp    119d <util_str_rstrip+0x7d>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1145 <util_str_rstrip+0x25>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_str_rstrip>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %rax,%rax
jle    1160 <util_str_rstrip+0x40>
mov    %rax,%rbx
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
movzbl -0x2(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1160 <util_str_rstrip+0x40>
movb   $0x0,-0x2(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1140 <util_str_rstrip+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @util_str_rstrip(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = sub nsw i64 %7, 1
store i64 %8, i64* %4
br label %9
9: ; preds = %21, %1
%10 = load i64, i64* %4
%11 = icmp sge i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i8*, i8** %2
%14 = load i64, i64* %4
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i8, i8* %15
%17 = call i64 @isspace(i8 zeroext %16)
%18 = icmp ne i64 %17, 0
br label %19
19: ; preds = %12, %9
%20 = phi i1 [ false, %9 ], [ %18, %12 ]
br i1 %20, label %21, label %27
21: ; preds = %19
%22 = load i8*, i8** %2
%23 = load i64, i64* %4
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 0, i8* %24
%25 = load i64, i64* %4
%26 = add nsw i64 %25, -1
store i64 %26, i64* %4
br label %9
27: ; preds = %19
ret void
}

LLVM IR (-O2):
define void @util_str_rstrip(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp sgt i64 %2, 0
br i1 %3, label %4, label %13
4: ; preds = %1, %11
%5 = phi i64 [ %6, %11 ], [ %2, %1 ]
%6 = add nsw i64 %5, -1
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = tail call i64 @isspace(i8 zeroext %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %4
store i8 0, i8* %7
%12 = icmp ugt i64 %5, 1
br i1 %12, label %4, label %13
13: ; preds = %4, %11, %1
ret void
}