#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__ strlen(char *);
char *remove_leading_and_trailing_spaces(char *s) {
  int start = 0;
  int end = (int)strlen(s);
  while (end > 0 && (s[end - 1] == ' ' || s[end - 1] == '\t')) {
    end--;
  }
  s[end] = '\0';
  while (start < end && (*s == ' ' || *s == '\t')) {
    s++;
    start++;
  }
  return s;
}


x86 assembly (clang, -O0):
<remove_leading_and_trailing_spaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jle    117d <remove_leading_and_trailing_spaces+0x6d>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x12(%rbp)
je     1177 <remove_leading_and_trailing_spaces+0x67>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    118d <remove_leading_and_trailing_spaces+0x7d>
jmp    119b <remove_leading_and_trailing_spaces+0x8b>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    112f <remove_leading_and_trailing_spaces+0x1f>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%ecx
xor    %eax,%eax
cmp    -0x10(%rbp),%ecx
mov    %al,-0x13(%rbp)
jge    11e3 <remove_leading_and_trailing_spaces+0xd3>
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x14(%rbp)
je     11dd <remove_leading_and_trailing_spaces+0xcd>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x14(%rbp)
mov    -0x14(%rbp),%al
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
test   $0x1,%al
jne    11f3 <remove_leading_and_trailing_spaces+0xe3>
jmp    120d <remove_leading_and_trailing_spaces+0xfd>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a7 <remove_leading_and_trailing_spaces+0x97>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_leading_and_trailing_spaces>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    11bb <remove_leading_and_trailing_spaces+0xab>
mov    %eax,%edx
mov    $0xffffffff,%ecx
add    %rdx,%rcx
mov    %rdx,%rsi
shl    $0x20,%rsi
add    $0xffffffffffffffff,%rdx
movabs $0xffffffff00000000,%rdi
ff ff ff
jmp    116b <remove_leading_and_trailing_spaces+0x5b>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%eax
add    $0xffffffffffffffff,%rcx
add    %rdi,%rsi
lea    -0x1(%rdx),%rbx
add    $0x1,%rdx
cmp    $0x1,%rdx
mov    %rbx,%rdx
jbe    11b9 <remove_leading_and_trailing_spaces+0xa9>
mov    %edx,%ebx
movzbl (%r14,%rbx,1),%ebx
cmp    $0x20,%bl
je     1150 <remove_leading_and_trailing_spaces+0x40>
cmp    $0x9,%bl
je     1150 <remove_leading_and_trailing_spaces+0x40>
sar    $0x20,%rsi
movb   $0x0,(%r14,%rsi,1)
test   %eax,%eax
jle    11c2 <remove_leading_and_trailing_spaces+0xb2>
mov    %ecx,%ecx
add    %r14,%rcx
add    $0x1,%rcx
jmp    11a9 <remove_leading_and_trailing_spaces+0x99>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r14
add    $0xffffffff,%eax
je     11cd <remove_leading_and_trailing_spaces+0xbd>
movzbl (%r14),%edx
cmp    $0x20,%dl
je     11a0 <remove_leading_and_trailing_spaces+0x90>
cmp    $0x9,%dl
je     11a0 <remove_leading_and_trailing_spaces+0x90>
jmp    11c2 <remove_leading_and_trailing_spaces+0xb2>
xor    %eax,%eax
cltq
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rcx,%r14
jmp    11c2 <remove_leading_and_trailing_spaces+0xb2>

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

LLVM IR (-O2):
define i8* @remove_leading_and_trailing_spaces(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = trunc i64 %2 to i32
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %15
5: ; preds = %1
%6 = and i64 %2, 4294967295
br label %7
7: ; preds = %5, %13
%8 = phi i64 [ %6, %5 ], [ %9, %13 ]
%9 = add nsw i64 %8, -1
%10 = and i64 %9, 4294967295
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
switch i8 %12, label %20 [
i8 32, label %13
i8 9, label %13
]
13: ; preds = %7, %7
%14 = icmp ugt i64 %8, 1
br i1 %14, label %7, label %15
15: ; preds = %13, %1
%16 = phi i64 [ %2, %1 ], [ 0, %13 ]
%17 = shl i64 %16, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
br label %39
20: ; preds = %7
%21 = trunc i64 %8 to i32
%22 = shl i64 %8, 32
%23 = ashr exact i64 %22, 32
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
%25 = icmp sgt i32 %21, 0
br i1 %25, label %26, label %39
26: ; preds = %20
%27 = add i64 %8, 4294967295
%28 = and i64 %27, 4294967295
%29 = add nuw nsw i64 %28, 1
%30 = getelementptr i8, i8* %0, i64 %29
br label %31
31: ; preds = %26, %35
%32 = phi i32 [ %37, %35 ], [ 0, %26 ]
%33 = phi i8* [ %36, %35 ], [ %0, %26 ]
%34 = load i8, i8* %33
switch i8 %34, label %39 [
i8 32, label %35
i8 9, label %35
]
35: ; preds = %31, %31
%36 = getelementptr inbounds i8, i8* %33, i64 1
%37 = add nuw nsw i32 %32, 1
%38 = icmp eq i32 %37, %21
br i1 %38, label %39, label %31
39: ; preds = %35, %31, %15, %20
%40 = phi i8* [ %0, %20 ], [ %0, %15 ], [ %33, %31 ], [ %30, %35 ]
ret i8* %40
}