#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 */
size_t strlen(char *);
char *rtrim(char *str) {
  if (str == NULL)
    return NULL;
  size_t len = strlen(str);
  unsigned char *ustr = (unsigned char *)str;
  unsigned char *ptr = ustr + len;
  while (*ptr <= 0x20 && ptr >= ustr) {
    --ptr;
  }
  *++ptr = '\0';
  return str;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1134 <rtrim+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11b4 <rtrim+0xa4>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0x29(%rbp)
jg     1178 <rtrim+0x68>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
setae  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1188 <rtrim+0x78>
jmp    1199 <rtrim+0x89>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1155 <rtrim+0x45>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %rbx
mov    %rdi,%rbx
test   %rdi,%rdi
je     1145 <rtrim+0x35>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rbx,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x1(%rax),%rcx
cmp    %rbx,%rax
jb     1141 <rtrim+0x31>
cmpb   $0x21,(%rax)
mov    %rcx,%rax
jb     1130 <rtrim+0x20>
movb   $0x0,0x2(%rcx)
mov    %rbx,%rax
pop    %rbx
ret

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

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