#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_str(char *s) {
  if (s && *s) {
    char *p = s + strlen(s) - 1;
    while ((p >= s) && (*p <= ' '))
      p--;
    p[1] = '\0';
  }
  return s;
}


x86 assembly (clang, -O0):
<rtrim_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a8 <rtrim_str+0x98>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a8 <rtrim_str+0x98>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jb     117f <rtrim_str+0x6f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
setle  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118f <rtrim_str+0x7f>
jmp    11a0 <rtrim_str+0x90>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    115c <rtrim_str+0x4c>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
define i8* @rtrim_str(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 %35
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 %35
11: ; preds = %6
%12 = load i8*, i8** %2
%13 = load i8*, i8** %2
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -1
store i8* %17, i8** %3
br label %18
18: ; preds = %29, %11
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = icmp uge i8* %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp sle i32 %25, 32
br label %27
27: ; preds = %22, %18
%28 = phi i1 [ false, %18 ], [ %26, %22 ]
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i32 -1
store i8* %31, i8** %3
br label %18
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 0, i8* %34
br label %35
35: ; preds = %32, %6, %1
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
define i8* @rtrim_str(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 i32 @strlen(i8* %0)
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
br label %10
10: ; preds = %14, %6
%11 = phi i8* [ %9, %6 ], [ %12, %14 ]
%12 = getelementptr inbounds i8, i8* %11, i64 -1
%13 = icmp ult i8* %12, %0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = load i8, i8* %12
%16 = icmp slt i8 %15, 33
br i1 %16, label %10, label %17
17: ; preds = %10, %14
store i8 0, i8* %11
br label %18
18: ; preds = %17, %3, %1
ret i8* %0
}