#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 */
typedef size_t ssize_t;

/* Variables and functions */
scalar_t__ isspace(char);
size_t strlen(char *);
void trim_trailing_whitespace(char *str) {
  for (ssize_t i = strlen(str) - 1; i >= 0; --i) {
    if (isspace(str[i]) || str[i] == '\n') {
      str[i] = '\0';
    }
  }
}


x86 assembly (clang, -O0):
<trim_trailing_whitespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jb     119a <trim_trailing_whitespace+0x7a>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
jne    1178 <trim_trailing_whitespace+0x58>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1184 <trim_trailing_whitespace+0x64>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1189 <trim_trailing_whitespace+0x69>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    113d <trim_trailing_whitespace+0x1d>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_trailing_whitespace>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
add    %rax,%rbx
add    $0xffffffffffffffff,%rbx
jmp    1147 <trim_trailing_whitespace+0x27>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x0,(%rbx)
add    $0xffffffffffffffff,%rbx
movsbl (%rbx),%edi
call   1040 <isspace@plt>
test   %rax,%rax
jne    1140 <trim_trailing_whitespace+0x20>
cmpb   $0xa,(%rbx)
je     1140 <trim_trailing_whitespace+0x20>
add    $0xffffffffffffffff,%rbx
jmp    1147 <trim_trailing_whitespace+0x27>

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

LLVM IR (-O2):
define void @trim_trailing_whitespace(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
br label %3
3: ; preds = %13, %1
%4 = phi i64 [ %2, %1 ], [ %5, %13 ]
%5 = add i64 %4, -1
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = tail call i64 @isspace(i8 signext %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i8, i8* %6
%12 = icmp eq i8 %11, 10
br i1 %12, label %14, label %13
13: ; preds = %10, %14
br label %3
14: ; preds = %10, %3
store i8 0, i8* %6
br label %13
}