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


x86 assembly (clang, -O0):
<str_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
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,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     1198 <str_trim+0x58>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1171 <str_trim+0x31>
jmp    119d <str_trim+0x5d>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     11c4 <str_trim+0x84>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    119d <str_trim+0x5d>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_trim>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
movslq %eax,%rbp
lea    -0x1(%rbx),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x1(%r14),%edi
add    $0x1,%r14
call   1050 <isspace@plt>
test   %rax,%rax
jne    1160 <str_trim+0x20>
add    %rbp,%rbx
cs nopw 0x0(%rax,%rax,1)
movsbl -0x1(%rbx),%edi
add    $0xffffffffffffffff,%rbx
call   1050 <isspace@plt>
test   %rax,%rax
jne    1180 <str_trim+0x40>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
add    $0x1,%ebp
movslq %ebp,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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