#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(unsigned char);
int /*<<< orphan*/ memmove(char *, char *, int /*<<< orphan*/);
size_t strlen(char *);
char *Trim(char *p) {
  if (p) {
    size_t l = strlen(p);
    while (l > 0 && isspace((unsigned char)p[0])) {
      memmove(p, p + 1, l--);
    }
    while (l > 1 && isspace((unsigned char)p[l - 1])) {
      p[--l] = 0;
    }
  }
  return p;
}


x86 assembly (clang, -O0):
<Trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1217 <Trim+0xe7>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jbe    117a <Trim+0x4a>
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    118a <Trim+0x5a>
jmp    11b1 <Trim+0x81>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
mov    %eax,%edx
call   1050 <memmove@plt>
jmp    1154 <Trim+0x24>
jmp    11b6 <Trim+0x86>
xor    %eax,%eax
cmpq   $0x1,-0x10(%rbp)
mov    %al,-0x12(%rbp)
jbe    11e5 <Trim+0xb5>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    11f5 <Trim+0xc5>
jmp    1212 <Trim+0xe2>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x10(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    11b6 <Trim+0x86>
jmp    1217 <Trim+0xe7>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Trim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     11b0 <Trim+0x80>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     11b0 <Trim+0x80>
mov    %rax,%rbx
lea    0x1(%r14),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%r14),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1183 <Trim+0x53>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <memmove@plt>
add    $0xffffffffffffffff,%rbx
jne    1160 <Trim+0x30>
jmp    11b0 <Trim+0x80>
cmp    $0x2,%rbx
jb     11b0 <Trim+0x80>
nopl   0x0(%rax)
movzbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     11b0 <Trim+0x80>
movb   $0x0,-0x1(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1190 <Trim+0x60>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @Trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %48
6: ; preds = %1
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %3
br label %9
9: ; preds = %20, %6
%10 = load i64, i64* %3
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i8*, i8** %2
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = call i64 @isspace(i8 zeroext %15)
%17 = icmp ne i64 %16, 0
br label %18
18: ; preds = %12, %9
%19 = phi i1 [ false, %9 ], [ %17, %12 ]
br i1 %19, label %20, label %28
20: ; preds = %18
%21 = load i8*, i8** %2
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i64, i64* %3
%25 = add i64 %24, -1
store i64 %25, i64* %3
%26 = trunc i64 %24 to i32
%27 = call i32 @memmove(i8* %21, i8* %23, i32 %26)
br label %9
28: ; preds = %18
br label %29
29: ; preds = %42, %28
%30 = load i64, i64* %3
%31 = icmp ugt i64 %30, 1
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = load i8*, i8** %2
%34 = load i64, i64* %3
%35 = sub i64 %34, 1
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = call i64 @isspace(i8 zeroext %37)
%39 = icmp ne i64 %38, 0
br label %40
40: ; preds = %32, %29
%41 = phi i1 [ false, %29 ], [ %39, %32 ]
br i1 %41, label %42, label %47
42: ; preds = %40
%43 = load i8*, i8** %2
%44 = load i64, i64* %3
%45 = add i64 %44, -1
store i64 %45, i64* %3
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
br label %29
47: ; preds = %40
br label %48
48: ; preds = %47, %1
%49 = load i8*, i8** %2
ret i8* %49
}

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