#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 /*<<< orphan*/ isspace(char);
char *strchr(char *, int /*<<< orphan*/);
char *cgi_rtrim(char *str) {
  char *last;
  if (str && *str) {
    last = strchr(str, 0) - 1;
    for (; last >= str; --last) {
      if (!isspace(*last))
        break;
    }
    last[1] = 0;
  }
  return str;
}


x86 assembly (clang, -O0):
<cgi_rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a0 <cgi_rtrim+0x80>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a0 <cgi_rtrim+0x80>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jb     1198 <cgi_rtrim+0x78>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
jne    1182 <cgi_rtrim+0x62>
jmp    1198 <cgi_rtrim+0x78>
jmp    1187 <cgi_rtrim+0x67>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    115a <cgi_rtrim+0x3a>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgi_rtrim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
je     116c <cgi_rtrim+0x4c>
cmpb   $0x0,(%r14)
je     116c <cgi_rtrim+0x4c>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r14,1),%rcx
add    $0xffffffffffffffff,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rcx,%rbx
cmp    %r14,%rcx
jb     1168 <cgi_rtrim+0x48>
movsbl (%rbx),%edi
call   1040 <isspace@plt>
lea    -0x1(%rbx),%rcx
test   %eax,%eax
jne    1150 <cgi_rtrim+0x30>
movb   $0x0,0x1(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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