#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);
int strlen(char *);
char *trim_right(char *str) {
  int len = strlen(str);
  char *cur = str + len - 1;
  while (cur != str && isspace(*cur))
    --cur;
  cur[isspace(*cur) ? 0 : 1] = '\0';
  return str;
}


x86 assembly (clang, -O0):
<trim_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
je     1174 <trim_right+0x54>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1184 <trim_right+0x64>
jmp    1195 <trim_right+0x75>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    114b <trim_right+0x2b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
mov    %rax,%rsi
mov    -0x28(%rbp),%rax
mov    $0x1,%ecx
xor    %edx,%edx
cmp    $0x0,%rsi
cmovne %edx,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_right>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
movslq %eax,%rbx
nop
cmp    $0x1,%rbx
je     114f <trim_right+0x2f>
movsbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1130 <trim_right+0x10>
add    %r14,%rbx
jmp    1152 <trim_right+0x32>
mov    %r14,%rbx
movsbl (%rbx),%edi
call   1040 <isspace@plt>
cmp    $0x1,%rax
adc    $0x0,%rbx
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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