#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 /*<<< orphan*/ isspace(char);
int strlen(char *);
char *rmtrail(char *str) {
  int i;
  if (str && 0 != (i = strlen(str))) {
    while (--i >= 0) {
      if (!isspace(str[i]))
        break;
    }
    str[++i] = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<rmtrail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     119e <rmtrail+0x7e>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     119e <rmtrail+0x7e>
jmp    1154 <rmtrail+0x34>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jl     118a <rmtrail+0x6a>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
jne    1185 <rmtrail+0x65>
jmp    118a <rmtrail+0x6a>
jmp    1154 <rmtrail+0x34>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmtrail>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     1174 <rmtrail+0x54>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1174 <rmtrail+0x54>
mov    %eax,%ebx
xor    %ebp,%ebp
test   %eax,%eax
cmovs  %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %ebx,%ebx
jle    116c <rmtrail+0x4c>
movsbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
add    $0xffffffffffffffff,%rbx
test   %eax,%eax
jne    1150 <rmtrail+0x30>
add    $0x1,%ebx
mov    %ebx,%ebp
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @rmtrail(i8* returned %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %25, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %25, label %6
6: ; preds = %3
%7 = zext i32 %4 to i64
%8 = call i32 @llvm.smin.i32(i32 %4, i32 0)
br label %9
9: ; preds = %6, %14
%10 = phi i64 [ %7, %6 ], [ %11, %14 ]
%11 = add nsw i64 %10, -1
%12 = trunc i64 %10 to i32
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %0, i64 %11
%16 = load i8, i8* %15
%17 = tail call i32 @isspace(i8 signext %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %9
19: ; preds = %14
%20 = trunc i64 %10 to i32
br label %21
21: ; preds = %9, %19
%22 = phi i32 [ %20, %19 ], [ %8, %9 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %21, %3, %1
ret i8* %0
}