#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 *);
scalar_t__ whitespace(char);
char *trim(char *str) {
  while (whitespace(*str))
    str++;
  char *trailing_zero = str + strlen(str);
  while (trailing_zero > str) {
    if (whitespace(trailing_zero[-1]))
      trailing_zero--;
    else
      break;
  }
  *trailing_zero = '\0';
  return str;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <whitespace@plt>
cmp    $0x0,%rax
je     1153 <trim+0x33>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    112c <trim+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
cmp    -0x8(%rbp),%rax
jbe    11b4 <trim+0x94>
mov    -0x10(%rbp),%rax
movsbl -0x1(%rax),%edi
call   1040 <whitespace@plt>
cmp    $0x0,%rax
je     11aa <trim+0x8a>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    11af <trim+0x8f>
jmp    11b4 <trim+0x94>
jmp    1174 <trim+0x54>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <whitespace@plt>
test   %rax,%rax
jne    1130 <trim+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%r14
mov    %rbx,%rcx
mov    %rcx,%r15
lea    (%rcx,%r14,1),%rax
cmp    %rbx,%rax
jbe    1170 <trim+0x50>
movsbl -0x1(%r15,%r14,1),%edi
call   1040 <whitespace@plt>
lea    -0x1(%r15),%rcx
test   %rax,%rax
jne    1150 <trim+0x30>
movb   $0x0,(%r15,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @trim(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 = call i64 @whitespace(i8 signext %6)
%8 = icmp ne i64 %7, 0
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 = %32, %12
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = icmp ugt i8* %19, %20
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 -1
%25 = load i8, i8* %24
%26 = call i64 @whitespace(i8 signext %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %3
br label %32
31: ; preds = %22
br label %33
32: ; preds = %28
br label %18
33: ; preds = %31, %18
%34 = load i8*, i8** %3
store i8 0, i8* %34
%35 = load i8*, i8** %2
ret i8* %35
}

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