#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(unsigned char);
char *sld_string_allocate(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *sld_string_trim_trailing(char *string) {
  char *trimmed_string = NULL;
  char *string_end = NULL;
  trimmed_string = sld_string_allocate(string);
  strcpy(trimmed_string, string);
  string_end = trimmed_string + strlen(trimmed_string) - 1;
  while (isspace((unsigned char)*string_end) && string_end > trimmed_string)
    string_end--;
  *(string_end + 1) = '\0';
  return trimmed_string;
}


x86 assembly (clang, -O0):
<sld_string_trim_trailing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <sld_string_allocate@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <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
movzbl (%rax),%edi
call   1060 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     11c7 <sld_string_trim_trailing+0x87>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
seta   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11d7 <sld_string_trim_trailing+0x97>
jmp    11e8 <sld_string_trim_trailing+0xa8>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    119b <sld_string_trim_trailing+0x5b>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sld_string_trim_trailing>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <sld_string_allocate@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rax,%r15,1),%rcx
add    $0xffffffffffffffff,%rcx
nopl   (%rax)
mov    %rcx,%rbx
movzbl (%rcx),%edi
call   1060 <isspace@plt>
lea    -0x1(%rbx),%rcx
cmp    %r15,%rbx
jbe    1189 <sld_string_trim_trailing+0x49>
test   %rax,%rax
jne    1170 <sld_string_trim_trailing+0x30>
movb   $0x0,0x2(%rcx)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @sld_string_trim_trailing(i8* %0) {
%2 = tail call i8* @sld_string_allocate(i8* %0)
%3 = tail call i32 @strcpy(i8* %2, i8* %0)
%4 = tail call i32 @strlen(i8* %2)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %2, i64 %5
br label %7
7: ; preds = %7, %1
%8 = phi i8* [ %6, %1 ], [ %9, %7 ]
%9 = getelementptr inbounds i8, i8* %8, i64 -1
%10 = load i8, i8* %9
%11 = tail call i64 @isspace(i8 zeroext %10)
%12 = icmp ne i64 %11, 0
%13 = icmp ugt i8* %9, %2
%14 = and i1 %13, %12
br i1 %14, label %7, label %15
15: ; preds = %7
store i8 0, i8* %8
ret i8* %2
}