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


x86 assembly (clang, -O0):
<strtrm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1152 <strtrm+0x32>
jmp    112c <strtrm+0xc>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    116f <strtrm+0x4f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ed <strtrm+0xcd>
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
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x21(%rbp)
jbe    11c8 <strtrm+0xa8>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11d8 <strtrm+0xb8>
jmp    11dd <strtrm+0xbd>
jmp    1194 <strtrm+0x74>
mov    -0x18(%rbp),%rax
movb   $0x0,0x2(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtrm>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
nopw   0x0(%rax,%rax,1)
movsbl (%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1130 <strtrm+0x10>
cmpb   $0x0,(%rbx)
je     1180 <strtrm+0x60>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x2,%r14
jl     117a <strtrm+0x5a>
movsbl -0x1(%rbx,%r14,1),%edi
add    $0xffffffffffffffff,%r14
call   1040 <isspace@plt>
test   %rax,%rax
jne    1160 <strtrm+0x40>
movb   $0x0,0x1(%r14,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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