#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__ chr_iswhite(char);
int /*<<< orphan*/ gfree(char *);
int str_length(char *);
char *str_new(char const *);
char *str_trimr(const char *s) {
  char *result;
  char *fwd = str_new(s);
  char *bwd = fwd + str_length(fwd) - 1;
  while (chr_iswhite(*bwd))
    *bwd-- = 0;
  result = str_new(fwd);
  gfree(fwd);
  return result;
}


x86 assembly (clang, -O0):
<str_trimr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <str_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <str_length@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1060 <chr_iswhite@plt>
cmp    $0x0,%rax
je     11ab <str_trimr+0x6b>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
jmp    117e <str_trimr+0x3e>
mov    -0x18(%rbp),%rdi
call   1040 <str_new@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <gfree@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_trimr>:
push   %r15
push   %r14
push   %rbx
call   1040 <str_new@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <str_length@plt>
movslq %eax,%rbx
movsbl -0x1(%r15,%rbx,1),%edi
call   1060 <chr_iswhite@plt>
test   %rax,%rax
je     1185 <str_trimr+0x45>
add    %r15,%rbx
add    $0xffffffffffffffff,%rbx
nop
movb   $0x0,(%rbx)
movsbl -0x1(%rbx),%edi
add    $0xffffffffffffffff,%rbx
call   1060 <chr_iswhite@plt>
test   %rax,%rax
jne    1170 <str_trimr+0x30>
mov    %r15,%rdi
call   1040 <str_new@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1050 <gfree@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_trimr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @str_new(i8* %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = load i8*, i8** %4
%10 = call i32 @str_length(i8* %9)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
%13 = getelementptr inbounds i8, i8* %12, i64 -1
store i8* %13, i8** %5
br label %14
14: ; preds = %19, %1
%15 = load i8*, i8** %5
%16 = load i8, i8* %15
%17 = call i64 @chr_iswhite(i8 signext %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i32 -1
store i8* %21, i8** %5
store i8 0, i8* %20
br label %14
22: ; preds = %14
%23 = load i8*, i8** %4
%24 = call i8* @str_new(i8* %23)
store i8* %24, i8** %3
%25 = load i8*, i8** %4
%26 = call i32 @gfree(i8* %25)
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
define i8* @str_trimr(i8* %0) {
%2 = tail call i8* @str_new(i8* %0)
%3 = tail call i32 @str_length(i8* %2)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %2, i64 %4
%6 = getelementptr inbounds i8, i8* %5, i64 -1
%7 = load i8, i8* %6
%8 = tail call i64 @chr_iswhite(i8 signext %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %1, %10
%11 = phi i8* [ %12, %10 ], [ %6, %1 ]
store i8 0, i8* %11
%12 = getelementptr inbounds i8, i8* %11, i64 -1
%13 = load i8, i8* %12
%14 = tail call i64 @chr_iswhite(i8 signext %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %10
16: ; preds = %10, %1
%17 = tail call i8* @str_new(i8* %2)
%18 = tail call i32 @gfree(i8* %2)
ret i8* %17
}