#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*/ FREE(char *);
char *STRDUP(char const *);
scalar_t__ isspace(char);
int strlen(char *);
char *trim_spaces(const char *s) {
  char *dup = STRDUP(s);
  int n = strlen(dup) - 1;
  while (isspace(dup[n]) && n > 0)
    dup[n--] = '\0';
  if (n == 0)
    return STRDUP("");
  char *p = dup;
  while (isspace(*p))
    ++p;
  char *ret = STRDUP(p);
  FREE(dup);
  return ret;
}


x86 assembly (clang, -O0):
<trim_spaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <STRDUP@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x31(%rbp)
je     1195 <trim_spaces+0x55>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11a5 <trim_spaces+0x65>
jmp    11c0 <trim_spaces+0x80>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0xffffffff,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1168 <trim_spaces+0x28>
cmpl   $0x0,-0x1c(%rbp)
jne    11df <trim_spaces+0x9f>
lea    0xe2f(%rip),%rdi
call   1060 <STRDUP@plt>
mov    %rax,-0x8(%rbp)
jmp    122c <trim_spaces+0xec>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     120e <trim_spaces+0xce>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11e7 <trim_spaces+0xa7>
mov    -0x28(%rbp),%rdi
call   1060 <STRDUP@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <FREE@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_spaces>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1060 <STRDUP@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
movslq %eax,%rbx
lea    -0x1(%rbx),%r15
movsbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     11ba <trim_spaces+0x7a>
cmp    $0x2,%ebp
jl     11ba <trim_spaces+0x7a>
lea    (%r14,%rbx,1),%rax
add    $0xffffffffffffffff,%rax
mov    %ebp,%ebx
lea    -0x2(%r14),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x1(%rbx),%r12
movb   $0x0,(%rax)
movsbl (%r15,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     11b5 <trim_spaces+0x75>
mov    %rbx,%rax
add    %r15,%rax
mov    %r12,%rbx
cmp    $0x1,%r12
ja     1190 <trim_spaces+0x50>
lea    -0x1(%r12),%r15d
test   %r15d,%r15d
je     1201 <trim_spaces+0xc1>
mov    %r14,%rbx
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    11d0 <trim_spaces+0x90>
mov    %rbx,%rdi
call   1060 <STRDUP@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <FREE@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <STRDUP@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @trim_spaces(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @STRDUP(i8* %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %5
br label %13
13: ; preds = %26, %1
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = call i64 @isspace(i8 signext %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 0
br label %24
24: ; preds = %21, %13
%25 = phi i1 [ false, %13 ], [ %23, %21 ]
br i1 %25, label %26, label %32
26: ; preds = %24
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = add nsw i32 %28, -1
store i32 %29, i32* %5
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 0, i8* %31
br label %13
32: ; preds = %24
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i8* @STRDUP(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %36, i8** %2
br label %53
37: ; preds = %32
%38 = load i8*, i8** %4
store i8* %38, i8** %6
br label %39
39: ; preds = %44, %37
%40 = load i8*, i8** %6
%41 = load i8, i8* %40
%42 = call i64 @isspace(i8 signext %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %6
br label %39
47: ; preds = %39
%48 = load i8*, i8** %6
%49 = call i8* @STRDUP(i8* %48)
store i8* %49, i8** %7
%50 = load i8*, i8** %4
%51 = call i32 @FREE(i8* %50)
%52 = load i8*, i8** %7
store i8* %52, i8** %2
br label %53
53: ; preds = %47, %35
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @trim_spaces(i8* %0) {
%2 = tail call i8* @STRDUP(i8* %0)
%3 = tail call i32 @strlen(i8* %2)
%4 = add nsw i32 %3, -1
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %2, i64 %5
%7 = load i8, i8* %6
%8 = tail call i64 @isspace(i8 signext %7)
%9 = icmp ne i64 %8, 0
%10 = icmp sgt i32 %3, 1
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %12, label %27
12: ; preds = %1
%13 = zext i32 %3 to i64
%14 = add nsw i64 %13, -1
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ %14, %12 ], [ %18, %15 ]
%17 = phi i8* [ %6, %12 ], [ %19, %15 ]
store i8 0, i8* %17
%18 = add nsw i64 %16, -1
%19 = getelementptr inbounds i8, i8* %2, i64 %18
%20 = load i8, i8* %19
%21 = tail call i64 @isspace(i8 signext %20)
%22 = icmp ne i64 %21, 0
%23 = icmp ugt i64 %16, 1
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %15, label %25
25: ; preds = %15
%26 = trunc i64 %18 to i32
br label %27
27: ; preds = %25, %1
%28 = phi i32 [ %4, %1 ], [ %26, %25 ]
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i8* @STRDUP(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %41
32: ; preds = %27, %32
%33 = phi i8* [ %37, %32 ], [ %2, %27 ]
%34 = load i8, i8* %33
%35 = tail call i64 @isspace(i8 signext %34)
%36 = icmp eq i64 %35, 0
%37 = getelementptr inbounds i8, i8* %33, i64 1
br i1 %36, label %38, label %32
38: ; preds = %32
%39 = tail call i8* @STRDUP(i8* %33)
%40 = tail call i32 @FREE(i8* %2)
br label %41
41: ; preds = %38, %30
%42 = phi i8* [ %31, %30 ], [ %39, %38 ]
ret i8* %42
}