#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 strlen(char *);
char *trim(char *str) {
  char *a, *z;
  a = str;
  while (*a == ' ')
    a++;
  z = a + strlen(a);
  while (*--z == ' ' && (z > a))
    ;
  *++z = '\0';
  return a;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1145 <trim+0x35>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1124 <trim+0x14>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movsbl -0x1(%rax),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0x21(%rbp)
jne    1195 <trim+0x85>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
seta   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11a5 <trim+0x95>
jmp    11aa <trim+0x9a>
jmp    1166 <trim+0x56>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
cmpb   $0x20,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1120 <trim+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
lea    -0x1(%rbx),%rcx
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rcx),%rdx
cmpb   $0x20,(%rcx,%rax,1)
jne    1155 <trim+0x45>
add    %rax,%rcx
cmp    %rbx,%rcx
mov    %rdx,%rcx
ja     1140 <trim+0x30>
movb   $0x0,0x2(%rdx,%rax,1)
mov    %rbx,%rax
pop    %rbx
ret

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

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