#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 */
typedef char *str;

/* Variables and functions */
scalar_t__ isascii(char);
scalar_t__ iscntrl(char);
scalar_t__ isspace(char);
scalar_t__ strlen(char *);
char *ztrim_right(char *str) {
  {
    char *p;
    for (p = (str) + strlen(str);
         p > (str) && ((isascii(p[-1]) && isspace(p[-1])) || iscntrl(p[-1]));
         p--)
      ;
    *p = 0;
  };
  return str;
}


x86 assembly (clang, -O0):
<ztrim_right>:
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   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jbe    11d1 <ztrim_right+0x91>
mov    -0x10(%rbp),%rax
movsbl -0x1(%rax),%edi
call   1060 <isascii@plt>
cmp    $0x0,%rax
je     11b4 <ztrim_right+0x74>
mov    -0x10(%rbp),%rax
movsbl -0x1(%rax),%edi
call   1050 <isspace@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x1a(%rbp)
jne    11cb <ztrim_right+0x8b>
mov    -0x10(%rbp),%rax
movsbl -0x1(%rax),%edi
call   1030 <iscntrl@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11e1 <ztrim_right+0xa1>
jmp    11f7 <ztrim_right+0xb7>
jmp    11e6 <ztrim_right+0xa6>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    116b <ztrim_right+0x2b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ztrim_right>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%rbx
test   %rax,%rax
jg     1187 <ztrim_right+0x47>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl -0x1(%rbx),%edi
call   1030 <iscntrl@plt>
test   %rax,%rax
je     1155 <ztrim_right+0x15>
add    $0xffffffffffffffff,%rbx
cmp    %r14,%rbx
jbe    1155 <ztrim_right+0x15>
movsbl -0x1(%rbx),%edi
call   1060 <isascii@plt>
test   %rax,%rax
je     1170 <ztrim_right+0x30>
movsbl -0x1(%rbx),%edi
call   1050 <isspace@plt>
test   %rax,%rax
jne    117e <ztrim_right+0x3e>
jmp    1170 <ztrim_right+0x30>

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

LLVM IR (-O2):
define i8* @ztrim_right(i8* returned %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = getelementptr inbounds i8, i8* %0, i64 %2
%4 = icmp sgt i64 %2, 0
br i1 %4, label %5, label %21
5: ; preds = %1, %19
%6 = phi i8* [ %7, %19 ], [ %3, %1 ]
%7 = getelementptr inbounds i8, i8* %6, i64 -1
%8 = load i8, i8* %7
%9 = tail call i64 @isascii(i8 signext %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %5
%12 = load i8, i8* %7
%13 = tail call i64 @isspace(i8 signext %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11, %5
%16 = load i8, i8* %7
%17 = tail call i64 @iscntrl(i8 signext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %11, %15
%20 = icmp ugt i8* %7, %0
br i1 %20, label %5, label %21
21: ; preds = %15, %19, %1
%22 = phi i8* [ %3, %1 ], [ %7, %19 ], [ %6, %15 ]
store i8 0, i8* %22
ret i8* %0
}