#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 *trim_trailing(char *str) {
  while (strlen(str) > 0 &&
         (str[strlen(str) - 1] == '\n' || str[strlen(str) - 1] == '\r' ||
          str[strlen(str) - 1] == ' ')) {
    str[strlen(str) - 1] = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<trim_trailing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x9(%rbp)
jle    11c3 <trim_trailing+0xb3>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0xa(%rbp)
je     11bd <trim_trailing+0xad>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xd,%ecx
mov    %al,-0xa(%rbp)
je     11bd <trim_trailing+0xad>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
sete   %al
mov    %al,-0xa(%rbp)
mov    -0xa(%rbp),%al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    11d3 <trim_trailing+0xc3>
jmp    11f9 <trim_trailing+0xe9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    111c <trim_trailing+0xc>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_trailing>:
push   %rbx
mov    %rdi,%rbx
jmp    1132 <trim_trailing+0x22>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    116e <trim_trailing+0x5e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     1120 <trim_trailing+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xd,-0x1(%rax,%rbx,1)
je     1120 <trim_trailing+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0x20,-0x1(%rax,%rbx,1)
je     1120 <trim_trailing+0x10>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @trim_trailing(i8* %0) {
%2 = alloca i8*
store i8* %0, i8** %2
br label %3
3: ; preds = %41, %1
%4 = load i8*, i8** %2
%5 = call i32 @strlen(i8* %4)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %39
7: ; preds = %3
%8 = load i8*, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 10
br i1 %16, label %37, label %17
17: ; preds = %7
%18 = load i8*, i8** %2
%19 = load i8*, i8** %2
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %18, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 13
br i1 %26, label %37, label %27
27: ; preds = %17
%28 = load i8*, i8** %2
%29 = load i8*, i8** %2
%30 = call i32 @strlen(i8* %29)
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 32
br label %37
37: ; preds = %27, %17, %7
%38 = phi i1 [ true, %17 ], [ true, %7 ], [ %36, %27 ]
br label %39
39: ; preds = %37, %3
%40 = phi i1 [ false, %3 ], [ %38, %37 ]
br i1 %40, label %41, label %48
41: ; preds = %39
%42 = load i8*, i8** %2
%43 = load i8*, i8** %2
%44 = call i32 @strlen(i8* %43)
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %42, i64 %46
store i8 0, i8* %47
br label %3
48: ; preds = %39
%49 = load i8*, i8** %2
ret i8* %49
}

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