#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 *str_rtrim(char *src) {
  int i;
  if (src[0] == 0)
    return NULL;
  for (i = strlen(src) - 1; src[i] == ' '; i--)
    ;
  src[i + 1] = 0;
  return src;
}


x86 assembly (clang, -O0):
<str_rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1139 <str_rtrim+0x29>
movq   $0x0,-0x8(%rbp)
jmp    1189 <str_rtrim+0x79>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    1170 <str_rtrim+0x60>
jmp    1162 <str_rtrim+0x52>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1148 <str_rtrim+0x38>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_rtrim>:
push   %rbx
cmpb   $0x0,(%rdi)
je     1166 <str_rtrim+0x56>
mov    %rdi,%rbx
call   1030 <strlen@plt>
movslq %eax,%rdx
lea    (%rdx,%rbx,1),%rcx
add    $0xffffffffffffffff,%rcx
shl    $0x20,%rdx
movabs $0x100000000,%rax
add    %rdx,%rax
movabs $0xffffffff00000000,%rdx
ff ff ff
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rdx,%rax
cmpb   $0x20,(%rcx)
lea    -0x1(%rcx),%rcx
je     1150 <str_rtrim+0x40>
sar    $0x20,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1168 <str_rtrim+0x58>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

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