#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 */
scalar_t__ isspace(char);
int strlen(char *);
char *rtrim(char *buffer) {
  char *buf_ptr;
  if (!buffer || !*buffer)
    return buffer;
  buf_ptr = buffer + strlen(buffer) - 1;
  while (*buf_ptr && isspace(*buf_ptr))
    buf_ptr--;
  *(buf_ptr + 1) = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1144 <rtrim+0x24>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1151 <rtrim+0x31>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d2 <rtrim+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     11a1 <rtrim+0x81>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11b1 <rtrim+0x91>
jmp    11c2 <rtrim+0xa2>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1176 <rtrim+0x56>
mov    -0x18(%rbp),%rax
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   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     1170 <rtrim+0x50>
cmpb   $0x0,(%r14)
je     1170 <rtrim+0x50>
mov    %r14,%rdi
call   1030 <strlen@plt>
movslq %eax,%r15
add    %r14,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rbx
movzbl -0x1(%r15),%eax
test   %al,%al
je     116d <rtrim+0x4d>
lea    -0x1(%rbx),%r15
movsbl %al,%edi
call   1040 <isspace@plt>
test   %rax,%rax
jne    1150 <rtrim+0x30>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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