#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);
size_t strlen(char *);
void trim(char *buf) {
  char *p = buf;
  while (isspace(*p))
    ++p;
  char *end = &p[strlen(p)];
  while (end > p && isspace(end[-1]))
    --end;
  *end = 0;
  memmove(buf, p, end + 1 - p);
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     116b <trim+0x3b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1144 <trim+0x14>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x21(%rbp)
jbe    11b4 <trim+0x84>
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11c4 <trim+0x94>
jmp    11d5 <trim+0xa5>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    118a <trim+0x5a>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <memmove@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    -0x1(%rdi),%rbx
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1140 <trim+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%r13
add    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%r12
mov    %eax,%r15d
cmp    %rbx,%r13
jbe    1197 <trim+0x67>
lea    -0x1(%r12),%r13
movsbl -0x1(%r12),%edi
call   1040 <isspace@plt>
mov    %rax,%rcx
lea    -0x1(%r15),%eax
test   %rcx,%rcx
jne    1170 <trim+0x40>
movb   $0x0,(%r12)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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
store i8* %5, i8** %3
br label %6
6: ; preds = %11, %1
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = call i64 @isspace(i8 signext %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %3
br label %6
14: ; preds = %6
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i64 @strlen(i8* %16)
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8* %18, i8** %4
br label %19
19: ; preds = %31, %14
%20 = load i8*, i8** %4
%21 = load i8*, i8** %3
%22 = icmp ugt i8* %20, %21
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 -1
%26 = load i8, i8* %25
%27 = call i64 @isspace(i8 signext %26)
%28 = icmp ne i64 %27, 0
br label %29
29: ; preds = %23, %19
%30 = phi i1 [ false, %19 ], [ %28, %23 ]
br i1 %30, label %31, label %34
31: ; preds = %29
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i32 -1
store i8* %33, i8** %4
br label %19
34: ; preds = %29
%35 = load i8*, i8** %4
store i8 0, i8* %35
%36 = load i8*, i8** %2
%37 = load i8*, i8** %3
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = load i8*, i8** %3
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i32 @memmove(i8* %36, i8* %37, i32 %44)
ret void
}

LLVM IR (-O2):
define void @trim(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %7, %2 ]
%4 = load i8, i8* %3
%5 = tail call i64 @isspace(i8 signext %4)
%6 = icmp eq i64 %5, 0
%7 = getelementptr inbounds i8, i8* %3, i64 1
br i1 %6, label %8, label %2
8: ; preds = %2
%9 = tail call i64 @strlen(i8* %3)
%10 = getelementptr inbounds i8, i8* %3, i64 %9
br label %11
11: ; preds = %14, %8
%12 = phi i8* [ %10, %8 ], [ %15, %14 ]
%13 = icmp ugt i8* %12, %3
br i1 %13, label %14, label %19
14: ; preds = %11
%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 %19, label %11
19: ; preds = %11, %14
store i8 0, i8* %12
%20 = getelementptr inbounds i8, i8* %12, i64 1
%21 = ptrtoint i8* %20 to i64
%22 = ptrtoint i8* %3 to i64
%23 = sub i64 %21, %22
%24 = trunc i64 %23 to i32
%25 = tail call i32 @memmove(i8* %0, i8* %3, i32 %24)
ret void
}