#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*/ free(char *);
scalar_t__ isspace(char);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *strtrim(char *s) {
  char *ptr_s, *tmp;
  int n;
  ptr_s = s;
  for (; isspace(*ptr_s); ptr_s++)
    ;
  for (n = strlen(ptr_s); n > 0 && isspace(*(ptr_s + n - 1)); n--)
    ;
  if (n < 0)
    n = 0;
  tmp = strdup(ptr_s);
  strcpy(s, tmp);
  *(s + n) = 0;
  free(tmp);
  return s;
}


x86 assembly (clang, -O0):
<strtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
je     1190 <strtrim+0x40>
jmp    117f <strtrim+0x2f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1164 <strtrim+0x14>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jle    11c7 <strtrim+0x77>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl -0x1(%rax,%rcx,1),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11d7 <strtrim+0x87>
jmp    11ea <strtrim+0x9a>
jmp    11dc <strtrim+0x8c>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    119c <strtrim+0x4c>
cmpl   $0x0,-0x1c(%rbp)
jge    11fb <strtrim+0xab>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtrim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    -0x1(%rdi),%rbx
nopl   0x0(%rax)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1060 <isspace@plt>
test   %rax,%rax
jne    1160 <strtrim+0x10>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11ad <strtrim+0x5d>
mov    %eax,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl -0x1(%rbx,%r15,1),%edi
call   1060 <isspace@plt>
test   %rax,%rax
je     11b0 <strtrim+0x60>
lea    -0x1(%r15),%rax
cmp    $0x1,%r15
mov    %rax,%r15
jg     1190 <strtrim+0x40>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %r15d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @strtrim(i8* returned %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 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 i32 @strlen(i8* %3)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds i8, i8* %3, i64 -1
br label %14
14: ; preds = %11, %20
%15 = phi i64 [ %12, %11 ], [ %21, %20 ]
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = tail call i64 @isspace(i8 signext %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %14
%21 = add nsw i64 %15, -1
%22 = icmp sgt i64 %15, 1
br i1 %22, label %14, label %23
23: ; preds = %14, %20, %8
%24 = phi i64 [ 0, %8 ], [ 0, %20 ], [ %15, %14 ]
%25 = tail call i8* @strdup(i8* %3)
%26 = tail call i32 @strcpy(i8* %0, i8* %25)
%27 = and i64 %24, 4294967295
%28 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 0, i8* %28
%29 = tail call i32 @free(i8* %25)
ret i8* %0
}