#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 *);
char *rtrim(char *string) {
  char *original = string + strlen(string);
  int i = strlen(string);
  while (i-- && *(--original) == ' ')
    ;
  if (i != -1)
    *(original + 1) = 0;
  else
    *(original) = 0;
  return string;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     117e <rtrim+0x6e>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0x20,%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    118e <rtrim+0x7e>
jmp    1193 <rtrim+0x83>
jmp    1149 <rtrim+0x39>
cmpl   $0xffffffff,-0x14(%rbp)
je     11aa <rtrim+0x9a>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11b1 <rtrim+0xa1>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
movslq %eax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%edx
lea    (%rbx,%r14,1),%rcx
add    $0x1,%rcx
sub    %rdx,%rbx
add    %r14,%rbx
nopw   0x0(%rax,%rax,1)
sub    $0x1,%eax
jb     1154 <rtrim+0x44>
lea    -0x1(%rcx),%rdx
cmpb   $0x20,-0x2(%rcx)
mov    %rdx,%rcx
je     1140 <rtrim+0x30>
jmp    1157 <rtrim+0x47>
mov    %rbx,%rdx
movb   $0x0,(%rdx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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