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


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x10(%rbp)
movsbl -0x1(%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1171 <trim+0x51>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1139 <trim+0x19>
jmp    1176 <trim+0x56>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     119d <trim+0x7d>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1176 <trim+0x56>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r14
movsbl -0x1(%rax,%r15,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1165 <trim+0x45>
lea    (%r14,%r15,1),%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
movsbl -0x1(%rbx),%edi
add    $0xffffffffffffffff,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1150 <trim+0x30>
add    $0xffffffffffffffff,%r15
nopl   0x0(%rax)
movsbl 0x1(%r15),%edi
add    $0x1,%r15
call   1040 <isspace@plt>
test   %rax,%rax
jne    1170 <trim+0x50>
mov    %r15,%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 = call i64 @strlen(i8* %4)
store i64 %5, i64* %3
br label %6
6: ; preds = %14, %1
%7 = load i8*, i8** %2
%8 = load i64, i64* %3
%9 = add i64 %8, -1
store i64 %9, i64* %3
%10 = getelementptr inbounds i8, i8* %7, i64 %9
%11 = load i8, i8* %10
%12 = call i64 @isspace(i8 signext %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %6
%15 = load i8*, i8** %2
%16 = load i64, i64* %3
%17 = getelementptr inbounds i8, i8* %15, i64 %16
store i8 0, i8* %17
br label %6
18: ; preds = %6
br label %19
19: ; preds = %24, %18
%20 = load i8*, i8** %2
%21 = load i8, i8* %20
%22 = call i64 @isspace(i8 signext %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i8*, i8** %2
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %2
br label %19
27: ; preds = %19
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
define i8* @trim(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, -1
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
%6 = tail call i64 @isspace(i8 signext %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %1, %8
%9 = phi i8* [ %12, %8 ], [ %4, %1 ]
%10 = phi i64 [ %11, %8 ], [ %3, %1 ]
store i8 0, i8* %9
%11 = add i64 %10, -1
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i64 @isspace(i8 signext %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %1
br label %17
17: ; preds = %16, %17
%18 = phi i8* [ %22, %17 ], [ %0, %16 ]
%19 = load i8, i8* %18
%20 = tail call i64 @isspace(i8 signext %19)
%21 = icmp eq i64 %20, 0
%22 = getelementptr inbounds i8, i8* %18, i64 1
br i1 %21, label %23, label %17
23: ; preds = %17
ret i8* %18
}