#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 */
char *str_copy(char *);
int str_len(char *);
char *str_sub(char *, int /*<<< orphan*/, int);
char *trim(char *str) {
  char *it;
  it = str + str_len(str) - 1;
  int i = 0;
  while (*it == ' ') {
    it--;
    i++;
  }
  str = str_sub(str, 0, str_len(str) - i);
  while (*str == ' ')
    str++;
  return str_copy(str);
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <str_len@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1192 <trim+0x62>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1168 <trim+0x38>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <str_len@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
sub    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <str_sub@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    11d8 <trim+0xa8>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11b7 <trim+0x87>
mov    -0x8(%rbp),%rdi
call   1030 <str_copy@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <str_len@plt>
cltq
add    %r14,%rax
mov    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
cmpb   $0x20,(%rax,%rbx,1)
lea    -0x1(%rbx),%rbx
je     1150 <trim+0x20>
mov    %r14,%rdi
call   1050 <str_len@plt>
lea    (%rax,%rbx,1),%edx
add    $0x2,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <str_sub@plt>
add    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
cmpb   $0x20,0x1(%rax)
lea    0x1(%rax),%rax
je     1180 <trim+0x50>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <str_copy@plt>

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

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