#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);
scalar_t__ strlen(char *);
char *trim_right(char *str) {
  if (str[0] != '\0') {
    for (int last = ((int)strlen(str)) - 1; last >= 0 && isspace(str[last]);
         --last) {
      str[last] = '\0';
    }
  }
  return str;
}


x86 assembly (clang, -O0):
<trim_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a4 <trim_right+0x84>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jl     1175 <trim_right+0x55>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1185 <trim_right+0x65>
jmp    119f <trim_right+0x7f>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    114b <trim_right+0x2b>
jmp    11a4 <trim_right+0x84>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_right>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
je     1162 <trim_right+0x42>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1162 <trim_right+0x42>
mov    %eax,%ebx
add    $0x1,%rbx
nop
lea    -0x2(%rbx),%r15d
movsbl (%r14,%r15,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1162 <trim_right+0x42>
movb   $0x0,(%r14,%r15,1)
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1140 <trim_right+0x20>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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