#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);
size_t strlen(char *);
char *str_strip(char *p) {
  size_t len = strlen(p);
  while (len > 0 && isspace((unsigned char)p[len - 1]))
    p[--len] = '\0';
  while (isspace((unsigned char)*p))
    p++;
  return p;
}


x86 assembly (clang, -O0):
<str_strip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jbe    1168 <str_strip+0x48>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1178 <str_strip+0x58>
jmp    1195 <str_strip+0x75>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x10(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    1139 <str_strip+0x19>
jmp    119a <str_strip+0x7a>
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11c1 <str_strip+0xa1>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    119a <str_strip+0x7a>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_strip>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
je     115c <str_strip+0x3c>
mov    %rax,%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl -0x1(%rbx,%r14,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     115c <str_strip+0x3c>
movb   $0x0,-0x1(%rbx,%r14,1)
add    $0xffffffffffffffff,%r14
jne    1140 <str_strip+0x20>
add    $0xffffffffffffffff,%rbx
movzbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1160 <str_strip+0x40>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @str_strip(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @strlen(i8* %4)
store i64 %5, i64* %3
br label %6
6: ; preds = %19, %1
%7 = load i64, i64* %3
%8 = icmp ugt i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = load i8*, i8** %2
%11 = load i64, i64* %3
%12 = sub i64 %11, 1
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = call i64 @isspace(i8 zeroext %14)
%16 = icmp ne i64 %15, 0
br label %17
17: ; preds = %9, %6
%18 = phi i1 [ false, %6 ], [ %16, %9 ]
br i1 %18, label %19, label %24
19: ; preds = %17
%20 = load i8*, i8** %2
%21 = load i64, i64* %3
%22 = add i64 %21, -1
store i64 %22, i64* %3
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
br label %6
24: ; preds = %17
br label %25
25: ; preds = %30, %24
%26 = load i8*, i8** %2
%27 = load i8, i8* %26
%28 = call i64 @isspace(i8 zeroext %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i8*, i8** %2
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %2
br label %25
33: ; preds = %25
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
define i8* @str_strip(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %13, label %4
4: ; preds = %1, %11
%5 = phi i64 [ %6, %11 ], [ %2, %1 ]
%6 = add i64 %5, -1
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = tail call i64 @isspace(i8 zeroext %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %4
store i8 0, i8* %7
%12 = icmp eq i64 %6, 0
br i1 %12, label %13, label %4
13: ; preds = %4, %11, %1
br label %14
14: ; preds = %13, %14
%15 = phi i8* [ %19, %14 ], [ %0, %13 ]
%16 = load i8, i8* %15
%17 = tail call i64 @isspace(i8 zeroext %16)
%18 = icmp eq i64 %17, 0
%19 = getelementptr inbounds i8, i8* %15, i64 1
br i1 %18, label %20, label %14
20: ; preds = %14
ret i8* %15
}