#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__ is_space(char const);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *strstrip(const char *str) {
  const char *scan = str;
  while (is_space(scan[0])) {
    scan += 1;
  }
  size_t len = strlen(scan);
  while (len > 0 && is_space(scan[len - 1])) {
    len -= 1;
  }
  char *trimmed = malloc(len + 1);
  if (!trimmed) {
    return NULL;
  }
  memcpy(trimmed, scan, len);
  trimmed[len] = '\0';
  return trimmed;
}


x86 assembly (clang, -O0):
<strstrip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1060 <is_space@plt>
cmp    $0x0,%rax
je     117b <strstrip+0x3b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1154 <strstrip+0x14>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x29(%rbp)
jbe    11b7 <strstrip+0x77>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <is_space@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11c7 <strstrip+0x87>
jmp    11d8 <strstrip+0x98>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1188 <strstrip+0x48>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1201 <strstrip+0xc1>
movq   $0x0,-0x8(%rbp)
jmp    1226 <strstrip+0xe6>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstrip>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1060 <is_space@plt>
test   %rax,%rax
jne    1150 <strstrip+0x10>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
nopl   (%rax)
mov    %r15,%r14
sub    $0x1,%r15
jb     1189 <strstrip+0x49>
movsbl -0x1(%rbx,%r14,1),%edi
call   1060 <is_space@plt>
test   %rax,%rax
jne    1170 <strstrip+0x30>
lea    0x1(%r14),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11ad <strstrip+0x6d>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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