#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 *malloc(int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *trim_spaces(char *str) {
  while (isspace(*str))
    str++;
  if (*str == '\0')
    return NULL;
  char *end = str + strlen(str) - 1;
  while (end > str && isspace(*end))
    end--;
  int len = ++end - str;
  char *ret = malloc(len + 1);
  strncpy(ret, str, len);
  ret[len] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<trim_spaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     1173 <trim_spaces+0x33>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114c <trim_spaces+0xc>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1190 <trim_spaces+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1248 <trim_spaces+0x108>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x31(%rbp)
jbe    11de <trim_spaces+0x9e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11ee <trim_spaces+0xae>
jmp    11ff <trim_spaces+0xbf>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11b5 <trim_spaces+0x75>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_spaces>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1050 <isspace@plt>
test   %rax,%rax
jne    1150 <trim_spaces+0x10>
cmpb   $0x0,(%rbx)
je     11dd <trim_spaces+0x9d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdx
lea    (%rbx,%rdx,1),%r15
add    $0xffffffffffffffff,%r15
shl    $0x20,%rdx
movabs $0xffffffff00000000,%r13
ff ff ff
nopl   0x0(%rax,%rax,1)
mov    %rdx,%r12
mov    %eax,%r14d
cmp    %rbx,%r15
jbe    11b8 <trim_spaces+0x78>
movsbl (%r15),%edi
call   1050 <isspace@plt>
mov    %rax,%rcx
lea    (%r12,%r13,1),%rdx
lea    -0x1(%r14),%eax
add    $0xffffffffffffffff,%r15
test   %rcx,%rcx
jne    1190 <trim_spaces+0x50>
lea    0x1(%r14),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
sar    $0x20,%r12
movb   $0x0,(%r15,%r12,1)
jmp    11e0 <trim_spaces+0xa0>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @trim_spaces(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
br label %7
7: ; preds = %12, %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = call i64 @isspace(i8 signext %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %3
br label %7
15: ; preds = %7
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i8* null, i8** %2
br label %62
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = load i8*, i8** %3
%24 = call i32 @strlen(i8* %23)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = getelementptr inbounds i8, i8* %26, i64 -1
store i8* %27, i8** %4
br label %28
28: ; preds = %39, %21
%29 = load i8*, i8** %4
%30 = load i8*, i8** %3
%31 = icmp ugt i8* %29, %30
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = load i8, i8* %33
%35 = call i64 @isspace(i8 signext %34)
%36 = icmp ne i64 %35, 0
br label %37
37: ; preds = %32, %28
%38 = phi i1 [ false, %28 ], [ %36, %32 ]
br i1 %38, label %39, label %42
39: ; preds = %37
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 -1
store i8* %41, i8** %4
br label %28
42: ; preds = %37
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %4
%45 = load i8*, i8** %3
%46 = ptrtoint i8* %44 to i64
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %46, %47
%49 = trunc i64 %48 to i32
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
%52 = call i8* @malloc(i32 %51)
store i8* %52, i8** %6
%53 = load i8*, i8** %6
%54 = load i8*, i8** %3
%55 = load i32, i32* %5
%56 = call i32 @strncpy(i8* %53, i8* %54, i32 %55)
%57 = load i8*, i8** %6
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** %6
store i8* %61, i8** %2
br label %62
62: ; preds = %42, %20
%63 = load i8*, i8** %2
ret i8* %63
}

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