#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);
char *malloc(size_t);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char *remove_trailing_whitespace(const char *str) {
  size_t len;
  char *newstr, *chr;
  if (!str)
    return NULL;
  len = strlen(str);
  newstr = malloc(len + 1);
  if (!newstr)
    return NULL;
  (void)strcpy(newstr, str);
  chr = newstr + len;
  while (isspace(*--chr) && chr >= newstr)
    *chr = '\0';
  return newstr;
}


x86 assembly (clang, -O0):
<remove_trailing_whitespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <remove_trailing_whitespace+0x24>
movq   $0x0,-0x8(%rbp)
jmp    120f <remove_trailing_whitespace+0xcf>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119a <remove_trailing_whitespace+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    120f <remove_trailing_whitespace+0xcf>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
movsbl -0x1(%rax),%edi
call   1050 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     11eb <remove_trailing_whitespace+0xab>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
setae  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11fb <remove_trailing_whitespace+0xbb>
jmp    1207 <remove_trailing_whitespace+0xc7>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b3 <remove_trailing_whitespace+0x73>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_trailing_whitespace>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11bc <remove_trailing_whitespace+0x7c>
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%r14
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11bc <remove_trailing_whitespace+0x7c>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    (%r12,%r14,1),%rbx
add    $0xffffffffffffffff,%rbx
movsbl -0x1(%r12,%r14,1),%edi
call   1050 <isspace@plt>
cmp    %r12,%rbx
jb     11bf <remove_trailing_whitespace+0x7f>
test   %rax,%rax
je     11bf <remove_trailing_whitespace+0x7f>
lea    (%r14,%r12,1),%rbx
add    $0xfffffffffffffffe,%rbx
nopl   0x0(%rax)
movb   $0x0,0x1(%rbx)
movsbl (%rbx),%edi
call   1050 <isspace@plt>
cmp    %r12,%rbx
jb     11bf <remove_trailing_whitespace+0x7f>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    11a0 <remove_trailing_whitespace+0x60>
jmp    11bf <remove_trailing_whitespace+0x7f>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @remove_trailing_whitespace(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %25, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = add i64 %4, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = tail call i32 @strcpy(i8* %6, i8* %0)
%10 = getelementptr inbounds i8, i8* %6, i64 %4
%11 = getelementptr inbounds i8, i8* %10, i64 -1
%12 = load i8, i8* %11
%13 = tail call i64 @isspace(i8 signext %12)
%14 = icmp ne i64 %13, 0
%15 = icmp uge i8* %11, %6
%16 = and i1 %15, %14
br i1 %16, label %17, label %25
17: ; preds = %8, %17
%18 = phi i8* [ %19, %17 ], [ %11, %8 ]
store i8 0, i8* %18
%19 = getelementptr inbounds i8, i8* %18, i64 -1
%20 = load i8, i8* %19
%21 = tail call i64 @isspace(i8 signext %20)
%22 = icmp ne i64 %21, 0
%23 = icmp uge i8* %19, %6
%24 = and i1 %23, %22
br i1 %24, label %17, label %25
25: ; preds = %17, %8, %3, %1
%26 = phi i8* [ null, %1 ], [ null, %3 ], [ %6, %8 ], [ %6, %17 ]
ret i8* %26
}