#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__ isspace(char);
int strlen(char *);
char *trim(char *string) {
  while (isspace(*string))
    string++;
  for (int i = strlen(string); i-- && isspace(string[i]);)
    string[i] = '\0';
  return string;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@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),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xd(%rbp)
je     1193 <trim+0x73>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    11a3 <trim+0x83>
jmp    11b4 <trim+0x94>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    115f <trim+0x3f>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1130 <trim+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1183 <trim+0x63>
mov    %eax,%r14d
movslq %eax,%r15
neg    %r14d
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
lea    (%r15,%r12,1),%r13
movsbl -0x1(%rbx,%r13,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1183 <trim+0x63>
movb   $0x0,-0x1(%rbx,%r13,1)
add    $0xffffffffffffffff,%r12
cmp    %r12d,%r14d
jne    1160 <trim+0x40>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
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 @isspace(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 = call i32 @strlen(i8* %13)
store i32 %14, i32* %3
br label %15
15: ; preds = %29, %12
%16 = load i32, i32* %3
%17 = add nsw i32 %16, -1
store i32 %17, i32* %3
%18 = icmp ne i32 %16, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i8*, i8** %2
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = call i64 @isspace(i8 signext %24)
%26 = icmp ne i64 %25, 0
br label %27
27: ; preds = %19, %15
%28 = phi i1 [ false, %15 ], [ %26, %19 ]
br i1 %28, label %29, label %34
29: ; preds = %27
%30 = load i8*, i8** %2
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
br label %15
34: ; preds = %27
%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 @isspace(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 = icmp eq i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = sext i32 %9 to i64
br label %13
13: ; preds = %11, %21
%14 = phi i64 [ %12, %11 ], [ %15, %21 ]
%15 = add nsw i64 %14, -1
%16 = getelementptr inbounds i8, i8* %3, i64 %15
%17 = load i8, i8* %16
%18 = tail call i64 @isspace(i8 signext %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13, %21, %8
ret i8* %3
21: ; preds = %13
store i8 0, i8* %16
%22 = trunc i64 %15 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %20, label %13
}