#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 strlen(char *);
char *trim(char *str) {
  char *end;
  if (strlen(str) > 0) {
    while (isspace(*str))
      str++;
    end = str + strlen(str) - 1;
    while (end > str && isspace(*end))
      end--;
    *(end + 1) = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jle    11e1 <trim+0xc1>
jmp    1143 <trim+0x23>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     116a <trim+0x4a>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1143 <trim+0x23>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jbe    11b8 <trim+0x98>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11c8 <trim+0xa8>
jmp    11d9 <trim+0xb9>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    118f <trim+0x6f>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    1195 <trim+0x75>
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1140 <trim+0x20>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%r14
lea    -0x1(%rbx),%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rcx,%r15
lea    (%rcx,%r14,1),%rax
cmp    %rbx,%rax
jbe    118f <trim+0x6f>
movsbl (%r15,%r14,1),%edi
call   1040 <isspace@plt>
lea    -0x1(%r15),%rcx
test   %rax,%rax
jne    1170 <trim+0x50>
movb   $0x0,0x1(%r15,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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