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


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     115b <trim+0x3b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1134 <trim+0x14>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1178 <trim+0x58>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d4 <trim+0xb4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11c4 <trim+0xa4>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    119d <trim+0x7d>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r14
push   %rbx
push   %rax
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>
cmpb   $0x0,(%rbx)
je     117a <trim+0x5a>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl -0x1(%rbx,%r14,1),%edi
call   1040 <isspace@plt>
add    $0xffffffffffffffff,%r14
test   %rax,%rax
jne    1160 <trim+0x40>
movb   $0x0,0x1(%rbx,%r14,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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