#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(unsigned char);
char *mh_xstrdup(char *);
int strlen(char *);
char *trimcpy(char *cp) {
  char *sp;
  while (isspace((unsigned char)*cp))
    cp++;
  for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
    if (isspace((unsigned char)*sp))
      *sp = '\0';
    else
      break;
  }
  for (sp = cp; *sp; sp++) {
    if (isspace((unsigned char)*sp))
      *sp = ' ';
  }
  return mh_xstrdup(cp);
}


x86 assembly (clang, -O0):
<trimcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1163 <trimcpy+0x33>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    113c <trimcpy+0xc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jb     11d3 <trimcpy+0xa3>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11b8 <trimcpy+0x88>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11bd <trimcpy+0x8d>
jmp    11d3 <trimcpy+0xa3>
jmp    11c2 <trimcpy+0x92>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1188 <trimcpy+0x58>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     121b <trimcpy+0xeb>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1205 <trimcpy+0xd5>
mov    -0x10(%rbp),%rax
movb   $0x20,(%rax)
jmp    120a <trimcpy+0xda>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11db <trimcpy+0xab>
mov    -0x8(%rbp),%rdi
call   1050 <mh_xstrdup@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trimcpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
nopl   0x0(%rax,%rax,1)
movzbl (%rbx),%edi
call   1040 <isspace@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1140 <trimcpy+0x10>
lea    -0x1(%rbx),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%r15
add    $0xfffffffffffffffe,%r15
cmp    %r14,%r15
jb     118b <trimcpy+0x5b>
nopl   0x0(%rax)
movzbl (%r15),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     118b <trimcpy+0x5b>
movb   $0x0,(%r15)
add    $0xffffffffffffffff,%r15
cmp    %r14,%r15
jae    1170 <trimcpy+0x40>
mov    (%r14),%al
jmp    1197 <trimcpy+0x67>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11ae <trimcpy+0x7e>
movzbl %al,%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1190 <trimcpy+0x60>
movb   $0x20,-0x1(%rbx)
jmp    1190 <trimcpy+0x60>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <mh_xstrdup@plt>

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

LLVM IR (-O2):
define i8* @trimcpy(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 zeroext %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
%12 = getelementptr inbounds i8, i8* %11, i64 -1
%13 = icmp ult i8* %12, %3
br i1 %13, label %22, label %14
14: ; preds = %8, %19
%15 = phi i8* [ %20, %19 ], [ %12, %8 ]
%16 = load i8, i8* %15
%17 = tail call i64 @isspace(i8 zeroext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
store i8 0, i8* %15
%20 = getelementptr inbounds i8, i8* %15, i64 -1
%21 = icmp ult i8* %20, %3
br i1 %21, label %22, label %14
22: ; preds = %19, %14, %8
%23 = load i8, i8* %3
%24 = icmp eq i8 %23, 0
br i1 %24, label %35, label %25
25: ; preds = %22, %31
%26 = phi i8 [ %33, %31 ], [ %23, %22 ]
%27 = phi i8* [ %32, %31 ], [ %3, %22 ]
%28 = tail call i64 @isspace(i8 zeroext %26)
%29 = icmp eq i64 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %25
store i8 32, i8* %27
br label %31
31: ; preds = %25, %30
%32 = getelementptr inbounds i8, i8* %27, i64 1
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %25
35: ; preds = %31, %22
%36 = tail call i8* @mh_xstrdup(i8* %3)
ret i8* %36
}