#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*/ strcpy_overlap(char *, char *);
int strlen(char *);
char *Trim(char *str, char trimchar) {
  char *end;
  while (*str == trimchar) {
    strcpy_overlap(str, str + 1);
  }
  end = str + strlen(str) - 1;
  while ((end >= str) && (*end == trimchar)) {
    *(end--) = 0;
  }
  return str;
}


x86 assembly (clang, -O0):
<Trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jne    115b <Trim+0x3b>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x1,%rsi
call   1030 <strcpy_overlap@plt>
jmp    1132 <Trim+0x12>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x21(%rbp)
jb     11a6 <Trim+0x86>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11b6 <Trim+0x96>
jmp    11cd <Trim+0xad>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
jmp    1180 <Trim+0x60>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Trim>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
cmp    %r14b,(%rdi)
jne    1150 <Trim+0x30>
lea    0x1(%rbx),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strcpy_overlap@plt>
cmp    %r14b,(%rbx)
je     1140 <Trim+0x20>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jb     1181 <Trim+0x61>
cs nopw 0x0(%rax,%rax,1)
cmp    %r14b,(%rax)
jne    1181 <Trim+0x61>
movb   $0x0,(%rax)
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jae    1170 <Trim+0x50>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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