#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 *realloc(char *, int);
int strlen(char *);
void string_trim_right(char **text) {
  char *string_without_blank = *text;
  int i = strlen(*text) - 1;
  while (i >= 0 && isspace(string_without_blank[i])) {
    string_without_blank[i] = '\0';
    i--;
  }
  *text = realloc(*text, strlen(string_without_blank) + 1);
}


x86 assembly (clang, -O0):
<string_trim_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jl     1183 <string_trim_right+0x53>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1193 <string_trim_right+0x63>
jmp    11ad <string_trim_right+0x7d>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1159 <string_trim_right+0x29>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_trim_right>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1172 <string_trim_right+0x42>
mov    %eax,%ebx
add    $0x1,%rbx
lea    -0x2(%rbx),%r15d
movsbl (%r12,%r15,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1172 <string_trim_right+0x42>
movb   $0x0,(%r12,%r15,1)
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1150 <string_trim_right+0x20>
mov    (%r14),%r15
mov    %r12,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%esi
mov    %r15,%rdi
call   1050 <realloc@plt>
mov    %rax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @string_trim_right(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = tail call i32 @strlen(i8* %2)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %1
%6 = zext i32 %3 to i64
br label %7
7: ; preds = %5, %15
%8 = phi i64 [ %6, %5 ], [ %9, %15 ]
%9 = add nsw i64 %8, -1
%10 = and i64 %9, 4294967295
%11 = getelementptr inbounds i8, i8* %2, i64 %10
%12 = load i8, i8* %11
%13 = tail call i64 @isspace(i8 signext %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %7
store i8 0, i8* %11
%16 = icmp ugt i64 %8, 1
br i1 %16, label %7, label %17
17: ; preds = %7, %15, %1
%18 = load i8*, i8** %0
%19 = tail call i32 @strlen(i8* %2)
%20 = add nsw i32 %19, 1
%21 = tail call i8* @realloc(i8* %18, i32 %20)
store i8* %21, i8** %0
ret void
}