#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 */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ isspace(char);
scalar_t__ malloc(int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *trim(char *string) {
  char *fstr = string, *rstr, ch;
  char *pnew;
  int len = strlen(string);
  while (isspace(*fstr))
    fstr++;
  rstr = string + len - 1;
  while (isspace(*rstr))
    rstr--;
  len = rstr - fstr + 1;
  pnew = (char *)malloc(len);
  assert(pnew != NULL);
  strncpy(pnew, fstr, len);
  return pnew;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     1197 <trim+0x47>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1170 <trim+0x20>
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     11d1 <trim+0x81>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11aa <trim+0x5a>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r12d
lea    0x2(%r12),%ebp
lea    -0x1(%r15),%r14
nopl   0x0(%rax)
movsbl 0x1(%r14),%edi
add    $0x1,%r14
call   1050 <isspace@plt>
add    $0xffffffff,%ebp
test   %rax,%rax
jne    1170 <trim+0x20>
movslq %r12d,%rax
lea    (%rax,%r15,1),%rbx
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%rbx),%edi
call   1050 <isspace@plt>
add    $0xffffffff,%ebp
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    11a0 <trim+0x50>
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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