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


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1153 <trim+0x33>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    112c <trim+0xc>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b9 <trim+0x99>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11b4 <trim+0x94>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    117c <trim+0x5c>
jmp    11b9 <trim+0x99>
mov    -0x8(%rbp),%rax
add    $0x10,%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)
movzbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1130 <trim+0x10>
cmpb   $0x0,(%rbx)
je     1187 <trim+0x67>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r14
movzbl -0x1(%rbx,%rax,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1187 <trim+0x67>
add    %rbx,%r14
add    $0xffffffffffffffff,%r14
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%r14)
movzbl -0x1(%r14),%edi
add    $0xffffffffffffffff,%r14
call   1040 <isspace@plt>
test   %rax,%rax
jne    1170 <trim+0x50>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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 zeroext %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 %26, label %11
11: ; preds = %8
%12 = tail call i64 @strlen(i8* %3)
%13 = add i64 %12, -1
%14 = getelementptr inbounds i8, i8* %3, i64 %13
%15 = load i8, i8* %14
%16 = tail call i64 @isspace(i8 zeroext %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %11, %18
%19 = phi i8* [ %22, %18 ], [ %14, %11 ]
%20 = phi i64 [ %21, %18 ], [ %13, %11 ]
store i8 0, i8* %19
%21 = add i64 %20, -1
%22 = getelementptr inbounds i8, i8* %3, i64 %21
%23 = load i8, i8* %22
%24 = tail call i64 @isspace(i8 zeroext %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %18
26: ; preds = %18, %11, %8
ret i8* %3
}