#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(unsigned char);
size_t strlen(char *);
char *strtrim_right(char *p) {
  unsigned char *end;
  size_t len;
  if (p && *p) {
    len = strlen(p);
    end = (unsigned char *)(p + len - 1);
    while (len) {
      if (isspace(*end)) {
        *end = 0;
        --end;
        --len;
      } else
        break;
    }
  }
  return (p);
}


x86 assembly (clang, -O0):
<strtrim_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11b8 <strtrim_right+0x98>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b8 <strtrim_right+0x98>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b3 <strtrim_right+0x93>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11a9 <strtrim_right+0x89>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11ae <strtrim_right+0x8e>
jmp    11b3 <strtrim_right+0x93>
jmp    1164 <strtrim_right+0x44>
jmp    11b8 <strtrim_right+0x98>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtrim_right>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
je     116c <strtrim_right+0x4c>
cmpb   $0x0,(%r14)
je     116c <strtrim_right+0x4c>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     116c <strtrim_right+0x4c>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     116c <strtrim_right+0x4c>
movb   $0x0,-0x1(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
jne    1150 <strtrim_right+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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