#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 *str_trim(char str[]) {
  char *buffer;
  int len;
  while (isspace(*str))
    ++str;
  char *end = str + strlen(str) - 1;
  while (isspace(*end)) {
    --end;
  }
  len = strlen(str) - strlen(end);
  buffer = malloc(len * sizeof(char));
  strncpy(buffer, str, len + 1);
  return buffer;
}


x86 assembly (clang, -O0):
<str_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     1173 <str_trim+0x33>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    114c <str_trim+0xc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     11bf <str_trim+0x7f>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1198 <str_trim+0x58>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_trim>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
add    $0xffffffffffffffff,%r14
xchg   %ax,%ax
movsbl 0x1(%r14),%edi
add    $0x1,%r14
call   1050 <isspace@plt>
test   %rax,%rax
jne    1150 <str_trim+0x10>
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl -0x1(%rbx,%rbp,1),%edi
call   1050 <isspace@plt>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1180 <str_trim+0x40>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
add    %rbp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
add    $0x1,%r15d
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @str_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 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %3, i64 %10
br label %12
12: ; preds = %12, %8
%13 = phi i8* [ %11, %8 ], [ %14, %12 ]
%14 = getelementptr inbounds i8, i8* %13, i64 -1
%15 = load i8, i8* %14
%16 = tail call i64 @isspace(i8 signext %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %12
18: ; preds = %12
%19 = tail call i32 @strlen(i8* %3)
%20 = tail call i32 @strlen(i8* %14)
%21 = sub nsw i32 %19, %20
%22 = tail call i8* @malloc(i32 %21)
%23 = add nsw i32 %21, 1
%24 = tail call i32 @strncpy(i8* %22, i8* %3, i32 %23)
ret i8* %22
}