#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 int);
int strlen(char *);
char *trim(char *s) {
  int len = 0;
  while (isspace((unsigned int)(*s)))
    s++;
  len = strlen(s);
  while (len > 0 && isspace((unsigned int)(s[len - 1])))
    len--;
  s[len] = '\0';
  return s;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     115a <trim+0x3a>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1133 <trim+0x13>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jle    1195 <trim+0x75>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    11a5 <trim+0x85>
jmp    11b3 <trim+0x93>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1166 <trim+0x46>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1130 <trim+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
xor    %ebp,%ebp
test   %eax,%eax
cmovs  %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %r14d,%r14d
jle    1180 <trim+0x60>
movsbl -0x1(%rbx,%r14,1),%edi
add    $0xffffffffffffffff,%r14
call   1040 <isspace@plt>
test   %rax,%rax
jne    1160 <trim+0x40>
add    $0x1,%r14d
mov    %r14d,%ebp
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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