#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 *strtrim(char *str) {
  int tail = strlen(str);
  while (str[tail] <= ' ' && tail > 0)
    tail--;
  str[tail + 1] = 0;
  while (*str <= ' ' && *str != 0)
    str++;
  return str;
}


x86 assembly (clang, -O0):
<strtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0xd(%rbp)
jg     114c <strtrim+0x3c>
cmpl   $0x0,-0xc(%rbp)
setg   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    115c <strtrim+0x4c>
jmp    116a <strtrim+0x5a>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1128 <strtrim+0x18>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0xe(%rbp)
jg     11a0 <strtrim+0x90>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
test   $0x1,%al
jne    11b0 <strtrim+0xa0>
jmp    11c1 <strtrim+0xb1>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    117b <strtrim+0x6b>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtrim>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
movslq %eax,%rcx
mov    %rcx,%rdx
shl    $0x20,%rdx
movabs $0x200000000,%rax
add    %rdx,%rax
movabs $0xffffffff00000000,%rdx
ff ff ff
nopw   0x0(%rax,%rax,1)
add    %rdx,%rax
cmpb   $0x20,(%rbx,%rcx,1)
jg     1155 <strtrim+0x45>
lea    -0x1(%rcx),%rsi
test   %rcx,%rcx
mov    %rsi,%rcx
jg     1140 <strtrim+0x30>
sar    $0x20,%rax
movb   $0x0,(%rbx,%rax,1)
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
cmp    $0x20,%al
jg     1180 <strtrim+0x70>
test   %al,%al
jne    1170 <strtrim+0x60>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @strtrim(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sext i32 %2 to i64
br label %4
4: ; preds = %4, %1
%5 = phi i64 [ %11, %4 ], [ %3, %1 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = icmp slt i8 %7, 33
%9 = icmp sgt i64 %5, 0
%10 = select i1 %8, i1 %9, i1 false
%11 = add nsw i64 %5, -1
br i1 %10, label %4, label %12
12: ; preds = %4
%13 = shl i64 %5, 32
%14 = add i64 %13, 4294967296
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %17, %12
%18 = phi i8* [ %0, %12 ], [ %23, %17 ]
%19 = load i8, i8* %18
%20 = icmp sgt i8 %19, 32
%21 = icmp eq i8 %19, 0
%22 = or i1 %20, %21
%23 = getelementptr inbounds i8, i8* %18, i64 1
br i1 %22, label %24, label %17
24: ; preds = %17
ret i8* %18
}