#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 */
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char *);
char *rtrim(char *string) {
  int i;
  for (i = strlen(string); i != 0; --i)
    if (string[i] > 0x40)
      break;
  char *n = malloc(i + 1);
  memcpy(n, string, i + 1);
  return n;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     117f <rtrim+0x4f>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x40,%eax
jle    116c <rtrim+0x3c>
jmp    117f <rtrim+0x4f>
jmp    1171 <rtrim+0x41>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1148 <rtrim+0x18>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    $0x1,%r14d
test   %eax,%eax
je     1166 <rtrim+0x36>
movslq %eax,%rcx
add    $0x1,%eax
add    %rbx,%rcx
nop
cmpb   $0x40,(%rcx)
jg     1163 <rtrim+0x33>
add    $0xffffffff,%eax
add    $0xffffffffffffffff,%rcx
cmp    $0x1,%eax
jne    1150 <rtrim+0x20>
jmp    1166 <rtrim+0x36>
mov    %eax,%r14d
mov    %r14d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @rtrim(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %18, label %4
4: ; preds = %1
%5 = sext i32 %2 to i64
br label %6
6: ; preds = %4, %11
%7 = phi i64 [ %5, %4 ], [ %12, %11 ]
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = icmp sgt i8 %9, 64
br i1 %10, label %15, label %11
11: ; preds = %6
%12 = add nsw i64 %7, -1
%13 = trunc i64 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %6
15: ; preds = %6
%16 = trunc i64 %7 to i32
%17 = add i32 %16, 1
br label %18
18: ; preds = %11, %15, %1
%19 = phi i32 [ 1, %1 ], [ %17, %15 ], [ 1, %11 ]
%20 = tail call i8* @malloc(i32 %19)
%21 = tail call i32 @memcpy(i8* %20, i8* %0, i32 %19)
ret i8* %20
}