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


x86 assembly (clang, -O0):
<libamg_str_strim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <libamg_str_skip_spaces@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116e <libamg_str_strim+0x3e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d8 <libamg_str_strim+0xa8>
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     11a7 <libamg_str_strim+0x77>
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    11b7 <libamg_str_strim+0x87>
jmp    11c8 <libamg_str_strim+0x98>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    117e <libamg_str_strim+0x4e>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libamg_str_strim>:
push   %r14
push   %rbx
push   %rax
call   1050 <libamg_str_skip_spaces@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     117d <libamg_str_strim+0x4d>
lea    (%rax,%r14,1),%rcx
add    $0xffffffffffffffff,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rcx,%rbx
cmp    %r14,%rcx
jb     1179 <libamg_str_strim+0x49>
movsbl (%rbx),%edi
call   1040 <isspace@plt>
lea    -0x1(%rbx),%rcx
test   %rax,%rax
jne    1160 <libamg_str_strim+0x30>
movb   $0x0,0x1(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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