#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);
char *skip_spaces(char *);
size_t strlen(char *);
char *strim(char *s) {
  size_t size;
  char *end;
  size = strlen(s);
  if (!size)
    return s;
  end = s + size - 1;
  while (end >= s && isspace(*end))
    end--;
  *(end + 1) = '\0';
  return skip_spaces(s);
}


x86 assembly (clang, -O0):
<strim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1161 <strim+0x31>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d0 <strim+0xa0>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x21(%rbp)
jb     119a <strim+0x6a>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11aa <strim+0x7a>
jmp    11bb <strim+0x8b>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1171 <strim+0x41>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <skip_spaces@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %rax,%rax
je     117c <strim+0x4c>
lea    (%rax,%r14,1),%rcx
add    $0xffffffffffffffff,%rcx
nopl   0x0(%rax)
mov    %rcx,%rbx
cmp    %r14,%rcx
jb     1169 <strim+0x39>
movsbl (%rbx),%edi
call   1040 <isspace@plt>
lea    -0x1(%rbx),%rcx
test   %rax,%rax
jne    1150 <strim+0x20>
movb   $0x0,0x1(%rbx)
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <skip_spaces@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @strim(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 %2
br label %6
6: ; preds = %10, %4
%7 = phi i8* [ %5, %4 ], [ %8, %10 ]
%8 = getelementptr inbounds i8, i8* %7, i64 -1
%9 = icmp ult i8* %8, %0
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = load i8, i8* %8
%12 = tail call i64 @isspace(i8 signext %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %6
14: ; preds = %6, %10
store i8 0, i8* %7
%15 = tail call i8* @skip_spaces(i8* %0)
br label %16
16: ; preds = %1, %14
%17 = phi i8* [ %15, %14 ], [ %0, %1 ]
ret i8* %17
}