#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(char);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char *);
void _trim(char *str) {
  if (str == NULL)
    return;
  char *begin, *end;
  begin = str;
  while (isspace(*begin))
    ++begin;
  end = begin + strlen(begin);
  if (end > begin)
    while (isspace(*--end))
      *end = '\0';
  memmove(str, begin, end - begin + 1);
}


x86 assembly (clang, -O0):
<_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    114c <_trim+0x1c>
jmp    1200 <_trim+0xd0>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     117b <_trim+0x4b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <_trim+0x24>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    11e2 <_trim+0xb2>
jmp    11af <_trim+0x7f>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movsbl -0x1(%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11dd <_trim+0xad>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11af <_trim+0x7f>
jmp    11e2 <_trim+0xb2>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1050 <memmove@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_trim>:
test   %rdi,%rdi
je     11ed <_trim+0xbd>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r12
lea    -0x2(%rdi),%r13d
mov    $0xfffffffffffffffd,%rbx
mov    $0x1,%ebp
mov    $0x2,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%r12,%r14,1),%edi
call   1040 <isspace@plt>
add    $0x1,%rbx
add    $0x1,%r13d
add    $0xffffffffffffffff,%rbp
add    $0xffffffffffffffff,%r15
add    $0x1,%r14
test   %rax,%rax
jne    1170 <_trim+0x40>
mov    %r12,%rdi
sub    %rbp,%rdi
mov    %rdi,0x8(%rsp)
call   1030 <strlen@plt>
cltq
mov    %r12,0x10(%rsp)
add    %rax,%r12
test   %eax,%eax
jle    11ee <_trim+0xbe>
mov    %r12,%rbp
sub    %r15,%rbp
movsbl -0x2(%r14,%r12,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     11f4 <_trim+0xc4>
add    %rbx,%r12
mov    0x8(%rsp),%rbx
nop
movb   $0x0,0x0(%rbp)
add    $0xffffffffffffffff,%rbp
movsbl (%r12),%edi
call   1040 <isspace@plt>
add    $0xffffffffffffffff,%r12
test   %rax,%rax
jne    11d0 <_trim+0xa0>
jmp    11f9 <_trim+0xc9>
ret
sub    %rbp,%r12
mov    %r12,%rbp
mov    0x8(%rsp),%rbx
sub    %r13d,%ebp
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <memmove@plt>

LLVM IR (-O0):
define void @_trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
br label %48
8: ; preds = %1
%9 = load i8*, i8** %2
store i8* %9, i8** %3
br label %10
10: ; preds = %15, %8
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = call i64 @isspace(i8 signext %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
br label %10
18: ; preds = %10
%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
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i8*, i8** %3
%26 = icmp ugt i8* %24, %25
br i1 %26, label %27, label %37
27: ; preds = %18
br label %28
28: ; preds = %34, %27
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %4
%31 = load i8, i8* %30
%32 = call i64 @isspace(i8 signext %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = load i8*, i8** %4
store i8 0, i8* %35
br label %28
36: ; preds = %28
br label %37
37: ; preds = %36, %18
%38 = load i8*, i8** %2
%39 = load i8*, i8** %3
%40 = load i8*, i8** %4
%41 = load i8*, i8** %3
%42 = ptrtoint i8* %40 to i64
%43 = ptrtoint i8* %41 to i64
%44 = sub i64 %42, %43
%45 = add nsw i64 %44, 1
%46 = trunc i64 %45 to i32
%47 = call i32 @memmove(i8* %38, i8* %39, i32 %46)
br label %48
48: ; preds = %37, %7
ret void
}

LLVM IR (-O2):
define void @_trim(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %33, label %3
3: ; preds = %1, %3
%4 = phi i8* [ %8, %3 ], [ %0, %1 ]
%5 = load i8, i8* %4
%6 = tail call i64 @isspace(i8 signext %5)
%7 = icmp eq i64 %6, 0
%8 = getelementptr inbounds i8, i8* %4, i64 1
br i1 %7, label %9, label %3
9: ; preds = %3
%10 = tail call i32 @strlen(i8* %4)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %4, i64 %11
%13 = icmp sgt i32 %10, 0
br i1 %13, label %14, label %25
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %12, i64 -1
%16 = load i8, i8* %15
%17 = tail call i64 @isspace(i8 signext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %14, %19
%20 = phi i8* [ %21, %19 ], [ %15, %14 ]
store i8 0, i8* %20
%21 = getelementptr inbounds i8, i8* %20, i64 -1
%22 = load i8, i8* %21
%23 = tail call i64 @isspace(i8 signext %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %19
25: ; preds = %19, %14, %9
%26 = phi i8* [ %12, %9 ], [ %15, %14 ], [ %21, %19 ]
%27 = ptrtoint i8* %26 to i64
%28 = ptrtoint i8* %4 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = add i32 %30, 1
%32 = tail call i32 @memmove(i8* %0, i8* %4, i32 %31)
br label %33
33: ; preds = %1, %25
ret void
}