#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*/ *strchr(char const *, char);
char *strrchr(char *, int /*<<< orphan*/);
char *trim(char *str, const char *set) {
  if (str == NULL)
    return NULL;
  char *end = strrchr(str, 0);
  if (set == NULL)
    set = " \t\n\r";
  while (--end >= str) {
    if (strchr(set, *end) == NULL)
      break;
    *end = 0;
  }
  return str;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1148 <trim+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11bb <trim+0x9b>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116d <trim+0x4d>
lea    0xe97(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1172 <trim+0x52>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
cmp    -0x10(%rbp),%rax
jb     11b3 <trim+0x93>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
movsbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    11a7 <trim+0x87>
jmp    11b3 <trim+0x93>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1172 <trim+0x52>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
je     115e <trim+0x4e>
mov    %rsi,%r12
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
mov    %rax,%rbx
test   %r12,%r12
lea    0xec6(%rip),%r15
cmovne %r12,%r15
xchg   %ax,%ax
add    $0xffffffffffffffff,%rbx
cmp    %r14,%rbx
jb     115e <trim+0x4e>
movsbl (%rbx),%esi
mov    %r15,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     115e <trim+0x4e>
movb   $0x0,(%rbx)
jmp    1140 <trim+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c" \09\0A\0D\00"
define i8* @trim(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
store i8* null, i8** %3
br label %33
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i8* @strrchr(i8* %11, i32 0)
store i8* %12, i8** %6
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %10
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %5
br label %16
16: ; preds = %15, %10
br label %17
17: ; preds = %29, %16
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i32 -1
store i8* %19, i8** %6
%20 = load i8*, i8** %4
%21 = icmp uge i8* %19, %20
br i1 %21, label %22, label %31
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = load i8*, i8** %6
%25 = load i8, i8* %24
%26 = call i32* @strchr(i8* %23, i8 signext %25)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %22
br label %31
29: ; preds = %22
%30 = load i8*, i8** %6
store i8 0, i8* %30
br label %17
31: ; preds = %28, %17
%32 = load i8*, i8** %4
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %9
%34 = load i8*, i8** %3
ret i8* %34
}

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