#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 */
int strlen(char *);
char *rtrim(char *totrim) {
  int smlen;
  char *pnt;
  pnt = totrim;
  smlen = strlen(totrim);
  if (smlen > 4095)
    smlen = 4095;
  pnt = pnt + smlen;
  while (*pnt == ' ' || *pnt == 0x0) {
    *pnt = 0x0;
    if (pnt == totrim)
      break;
    else
      pnt--;
  }
  return totrim;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xfff,-0xc(%rbp)
jle    1144 <rtrim+0x34>
movl   $0xfff,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x19(%rbp)
je     1178 <rtrim+0x68>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1188 <rtrim+0x78>
jmp    11b3 <rtrim+0xa3>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11a2 <rtrim+0x92>
jmp    11b3 <rtrim+0xa3>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1153 <rtrim+0x43>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0xfff,%eax
mov    $0xfff,%ecx
cmovl  %eax,%ecx
movslq %ecx,%rax
nopl   0x0(%rax)
testb  $0xdf,(%rbx,%rax,1)
jne    1140 <rtrim+0x30>
movb   $0x0,(%rbx,%rax,1)
add    $0xffffffffffffffff,%rax
jb     1130 <rtrim+0x20>
mov    %rbx,%rax
pop    %rbx
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
store i8* %5, i8** %4
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 4095
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 4095, i32* %3
br label %11
11: ; preds = %10, %1
%12 = load i8*, i8** %4
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8* %15, i8** %4
br label %16
16: ; preds = %37, %11
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 32
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br label %26
26: ; preds = %21, %16
%27 = phi i1 [ true, %16 ], [ %25, %21 ]
br i1 %27, label %28, label %38
28: ; preds = %26
%29 = load i8*, i8** %4
store i8 0, i8* %29
%30 = load i8*, i8** %4
%31 = load i8*, i8** %2
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %34
33: ; preds = %28
br label %38
34: ; preds = %28
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i32 -1
store i8* %36, i8** %4
br label %37
37: ; preds = %34
br label %16
38: ; preds = %33, %26
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
define i8* @rtrim(i8* returned %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 4095
%4 = select i1 %3, i32 %2, i32 4095
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
br label %7
7: ; preds = %10, %1
%8 = phi i8* [ %6, %1 ], [ %12, %10 ]
%9 = load i8, i8* %8
switch i8 %9, label %13 [
i8 32, label %10
i8 0, label %10
]
10: ; preds = %7, %7
store i8 0, i8* %8
%11 = icmp eq i8* %8, %0
%12 = getelementptr inbounds i8, i8* %8, i64 -1
br i1 %11, label %13, label %7
13: ; preds = %7, %10
ret i8* %0
}