#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__ strchr(char *, char);
char *strdup(char const *);
int strlen(char const *);
char *ytht_strrtrim_s(const char *s) {
  char *t = NULL, *t2;
  if (s[0] == 0)
    return NULL;
  if ((t = strdup(s)) == NULL)
    return NULL;
  t2 = t + strlen(s) - 1;
  while (strchr(" \t\r\n", *t2) && t2 > t) {
    t2--;
  }
  t2[1] = 0;
  return t;
}


x86 assembly (clang, -O0):
<ytht_strrtrim_s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1161 <ytht_strrtrim_s+0x31>
movq   $0x0,-0x8(%rbp)
jmp    120e <ytht_strrtrim_s+0xde>
mov    -0x10(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1185 <ytht_strrtrim_s+0x55>
movq   $0x0,-0x8(%rbp)
jmp    120e <ytht_strrtrim_s+0xde>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
lea    0xe4b(%rip),%rdi
movsbl (%rax),%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     11dd <ytht_strrtrim_s+0xad>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
seta   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11ed <ytht_strrtrim_s+0xbd>
jmp    11fe <ytht_strrtrim_s+0xce>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    11aa <ytht_strrtrim_s+0x7a>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ytht_strrtrim_s>:
push   %r15
push   %r14
push   %rbx
cmpb   $0x0,(%rdi)
je     1192 <ytht_strrtrim_s+0x62>
mov    %rdi,%r14
call   1050 <strdup@plt>
test   %rax,%rax
je     1192 <ytht_strrtrim_s+0x62>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rax,%r15,1),%rbx
add    $0xffffffffffffffff,%rbx
lea    0xe9d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%rbx),%esi
mov    %r14,%rdi
call   1040 <strchr@plt>
lea    -0x1(%rbx),%rcx
test   %rax,%rax
je     118c <ytht_strrtrim_s+0x5c>
cmp    %r15,%rbx
mov    %rcx,%rbx
ja     1170 <ytht_strrtrim_s+0x40>
movb   $0x0,0x2(%rcx)
jmp    1195 <ytht_strrtrim_s+0x65>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c" \09\0D\0A\00"
define i8* @ytht_strrtrim_s(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i64 0
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* null, i8** %2
br label %42
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %4
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %12
store i8* null, i8** %2
br label %42
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 -1
store i8* %23, i8** %5
br label %24
24: ; preds = %35, %17
%25 = load i8*, i8** %5
%26 = load i8, i8* %25
%27 = call i64 @strchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = load i8*, i8** %4
%32 = icmp ugt i8* %30, %31
br label %33
33: ; preds = %29, %24
%34 = phi i1 [ false, %24 ], [ %32, %29 ]
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 -1
store i8* %37, i8** %5
br label %24
38: ; preds = %33
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8 0, i8* %40
%41 = load i8*, i8** %4
store i8* %41, i8** %2
br label %42
42: ; preds = %38, %16, %11
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@.str = [5 x i8] c" \09\0D\0A\00"
define i8* @ytht_strrtrim_s(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = tail call i8* @strdup(i8* %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %20, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %0)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
br label %11
11: ; preds = %11, %7
%12 = phi i8* [ %10, %7 ], [ %13, %11 ]
%13 = getelementptr inbounds i8, i8* %12, i64 -1
%14 = load i8, i8* %13
%15 = tail call i64 @strchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %14)
%16 = icmp ne i64 %15, 0
%17 = icmp ugt i8* %13, %5
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %11, label %19
19: ; preds = %11
store i8 0, i8* %12
br label %20
20: ; preds = %4, %1, %19
%21 = phi i8* [ %5, %19 ], [ null, %1 ], [ null, %4 ]
ret i8* %21
}