#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 *strtrm(char *str) {
  char *d;
  while (*str == ' ')
    str++;
  d = str + strlen(str);
  while (*--d == ' ')
    *d = '\0';
  return str;
}


x86 assembly (clang, -O0):
<strtrm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    113d <strtrm+0x2d>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    111c <strtrm+0xc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0x20,%eax
jne    1186 <strtrm+0x76>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    115e <strtrm+0x4e>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
define i8* @strtrm(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
br label %4
4: ; preds = %9, %1
%5 = load i8*, i8** %2
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = icmp eq i32 %7, 32
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** %2
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8* %17, i8** %3
br label %18
18: ; preds = %24, %12
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i32 -1
store i8* %20, i8** %3
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 32
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i8*, i8** %3
store i8 0, i8* %25
br label %18
26: ; preds = %18
%27 = load i8*, i8** %2
ret i8* %27
}

LLVM IR (-O2):
define i8* @strtrm(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
%11 = getelementptr inbounds i8, i8* %10, i64 -1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 32
br i1 %13, label %14, label %19
14: ; preds = %7, %14
%15 = phi i8* [ %16, %14 ], [ %11, %7 ]
store i8 0, i8* %15
%16 = getelementptr inbounds i8, i8* %15, i64 -1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 32
br i1 %18, label %14, label %19
19: ; preds = %14, %7
ret i8* %3
}