#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*/ isspace(char const);
int strlen(char *);
char *xstrdup(char const *);
char *trim(const char *str) {
  int i;
  char *copy;
  char *tail;
  while (*str)
    if (!isspace(*str))
      break;
    else
      str++;
  copy = xstrdup(str);
  for (tail = copy + strlen(copy) - 1;
       (unsigned long)copy <= (unsigned long)tail; tail--) {
    if (!isspace(*tail))
      break;
    *tail = '\0';
  }
  return copy;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1174 <trim+0x44>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%eax
jne    1163 <trim+0x33>
jmp    1174 <trim+0x44>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    113c <trim+0xc>
mov    -0x8(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    %rcx,%rax
ja     11e9 <trim+0xb9>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%eax
jne    11d1 <trim+0xa1>
jmp    11e9 <trim+0xb9>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    11a6 <trim+0x76>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%al
test   %al,%al
je     1158 <trim+0x28>
nopl   (%rax)
movsbl %al,%edi
call   1050 <isspace@plt>
test   %eax,%eax
je     1158 <trim+0x28>
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1140 <trim+0x10>
mov    %rbx,%rdi
call   1040 <xstrdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
cltq
lea    (%r14,%rax,1),%rbx
add    $0xffffffffffffffff,%rbx
cmp    %rbx,%r14
ja     1198 <trim+0x68>
nopw   0x0(%rax,%rax,1)
movsbl (%rbx),%edi
call   1050 <isspace@plt>
test   %eax,%eax
je     1198 <trim+0x68>
movb   $0x0,(%rbx)
add    $0xffffffffffffffff,%rbx
cmp    %rbx,%r14
jbe    1180 <trim+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @trim(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %13, label %4
4: ; preds = %1, %9
%5 = phi i8 [ %11, %9 ], [ %2, %1 ]
%6 = phi i8* [ %10, %9 ], [ %0, %1 ]
%7 = tail call i32 @isspace(i8 signext %5)
%8 = icmp eq i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %4
13: ; preds = %9, %4, %1
%14 = phi i8* [ %0, %1 ], [ %6, %4 ], [ %10, %9 ]
%15 = tail call i8* @xstrdup(i8* %14)
%16 = tail call i32 @strlen(i8* %15)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = getelementptr inbounds i8, i8* %18, i64 -1
%20 = icmp ugt i8* %15, %19
br i1 %20, label %29, label %21
21: ; preds = %13, %26
%22 = phi i8* [ %27, %26 ], [ %19, %13 ]
%23 = load i8, i8* %22
%24 = tail call i32 @isspace(i8 signext %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
store i8 0, i8* %22
%27 = getelementptr inbounds i8, i8* %22, i64 -1
%28 = icmp ugt i8* %15, %27
br i1 %28, label %29, label %21
29: ; preds = %26, %21, %13
ret i8* %15
}