#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 /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *trim(char *s) {
  char *s1 = s, *s2 = &s[strlen(s) - 1];
  while ((isspace(*s2)) && (s2 >= s1))
    s2--;
  *(s2 + 1) = '\0';
  while ((isspace(*s1)) && (s1 < s2))
    s1++;
  strcpy(s, s1);
  return s;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     1194 <trim+0x64>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
setae  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11a4 <trim+0x74>
jmp    11b5 <trim+0x85>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1168 <trim+0x38>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x22(%rbp)
je     11e9 <trim+0xb9>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11f9 <trim+0xc9>
jmp    120a <trim+0xda>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11bd <trim+0x8d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
cltq
lea    (%rax,%r14,1),%r15
add    $0xffffffffffffffff,%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rbx
movsbl (%r15),%edi
call   1050 <isspace@plt>
add    $0xffffffffffffffff,%r15
cmp    %r14,%rbx
jb     116a <trim+0x3a>
test   %rax,%rax
jne    1150 <trim+0x20>
movb   $0x0,0x2(%r15)
add    $0x1,%r15
mov    %r14,%rsi
cs nopw 0x0(%rax,%rax,1)
mov    %rsi,%rbx
movsbl (%rsi),%edi
call   1050 <isspace@plt>
lea    0x1(%rbx),%rsi
cmp    %r15,%rbx
jae    1199 <trim+0x69>
test   %rax,%rax
jne    1180 <trim+0x50>
add    $0xffffffffffffffff,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @trim(i8* returned %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
br label %6
6: ; preds = %6, %1
%7 = phi i8* [ %5, %1 ], [ %13, %6 ]
%8 = load i8, i8* %7
%9 = tail call i64 @isspace(i8 signext %8)
%10 = icmp ne i64 %9, 0
%11 = icmp uge i8* %7, %0
%12 = and i1 %11, %10
%13 = getelementptr inbounds i8, i8* %7, i64 -1
br i1 %12, label %6, label %14
14: ; preds = %6
%15 = getelementptr inbounds i8, i8* %7, i64 1
store i8 0, i8* %15
br label %16
16: ; preds = %16, %14
%17 = phi i8* [ %0, %14 ], [ %23, %16 ]
%18 = load i8, i8* %17
%19 = tail call i64 @isspace(i8 signext %18)
%20 = icmp ne i64 %19, 0
%21 = icmp ult i8* %17, %7
%22 = and i1 %21, %20
%23 = getelementptr inbounds i8, i8* %17, i64 1
br i1 %22, label %16, label %24
24: ; preds = %16
%25 = tail call i32 @strcpy(i8* %0, i8* %17)
ret i8* %0
}