#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__ chr_iswhite(char);
int /*<<< orphan*/ gfree(char *);
int str_length(char *);
char *str_new(char const *);
char *str_trim(const char *s) {
  char *result;
  char *fwd = str_new(s);
  while (chr_iswhite(*fwd))
    fwd++;
  char *bwd = fwd + str_length(fwd) - 1;
  while (chr_iswhite(*bwd))
    *bwd-- = 0;
  result = str_new(fwd);
  gfree(fwd);
  return result;
}


x86 assembly (clang, -O0):
<str_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <str_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1060 <chr_iswhite@plt>
cmp    $0x0,%rax
je     1180 <str_trim+0x40>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1159 <str_trim+0x19>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <str_length@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   1060 <chr_iswhite@plt>
cmp    $0x0,%rax
je     11d2 <str_trim+0x92>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
jmp    11a5 <str_trim+0x65>
mov    -0x18(%rbp),%rdi
call   1040 <str_new@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <gfree@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_trim>:
push   %r14
push   %rbx
push   %rax
call   1040 <str_new@plt>
mov    %rax,%rbx
add    $0xffffffffffffffff,%rbx
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1060 <chr_iswhite@plt>
test   %rax,%rax
jne    1150 <str_trim+0x10>
mov    %rbx,%rdi
call   1030 <str_length@plt>
movslq %eax,%r14
movsbl -0x1(%rbx,%r14,1),%edi
call   1060 <chr_iswhite@plt>
test   %rax,%rax
je     119a <str_trim+0x5a>
nopl   (%rax)
movb   $0x0,-0x1(%rbx,%r14,1)
movsbl -0x2(%rbx,%r14,1),%edi
call   1060 <chr_iswhite@plt>
add    $0xffffffffffffffff,%r14
test   %rax,%rax
jne    1180 <str_trim+0x40>
mov    %rbx,%rdi
call   1040 <str_new@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <gfree@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @str_trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @str_new(i8* %6)
store i8* %7, i8** %4
br label %8
8: ; preds = %13, %1
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = call i64 @chr_iswhite(i8 signext %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %4
br label %8
16: ; preds = %8
%17 = load i8*, i8** %4
%18 = load i8*, i8** %4
%19 = call i32 @str_length(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** %5
br label %23
23: ; preds = %28, %16
%24 = load i8*, i8** %5
%25 = load i8, i8* %24
%26 = call i64 @chr_iswhite(i8 signext %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %5
store i8 0, i8* %29
br label %23
31: ; preds = %23
%32 = load i8*, i8** %4
%33 = call i8* @str_new(i8* %32)
store i8* %33, i8** %3
%34 = load i8*, i8** %4
%35 = call i32 @gfree(i8* %34)
%36 = load i8*, i8** %3
ret i8* %36
}

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