#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*/ assert(int /*<<< orphan*/);
size_t strlen(char *);
char *_trim_spaces(char *beginning) {
  size_t len = 0;
  unsigned int leading_space_count = 0;
  char *end;
  assert(beginning != NULL);
  len = strlen(beginning);
  if (!len)
    return NULL;
  while (*beginning == ' ') {
    beginning++;
    leading_space_count++;
  }
  if (leading_space_count >= len)
    return NULL;
  end = beginning + len - 1;
  while (*end == ' ') {
    *end = '\0';
    end--;
  }
  return beginning;
}


x86 assembly (clang, -O0):
<_trim_spaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1172 <_trim_spaces+0x52>
movq   $0x0,-0x8(%rbp)
jmp    11fb <_trim_spaces+0xdb>
jmp    1177 <_trim_spaces+0x57>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    11a1 <_trim_spaces+0x81>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1177 <_trim_spaces+0x57>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jb     11bb <_trim_spaces+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    11fb <_trim_spaces+0xdb>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    11f3 <_trim_spaces+0xd3>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    11cb <_trim_spaces+0xab>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_trim_spaces>:
push   %rbx
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     118f <_trim_spaces+0x6f>
cmpb   $0x20,(%rbx)
jne    1165 <_trim_spaces+0x45>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
cmpb   $0x20,0x1(%rbx,%rcx,1)
lea    0x1(%rcx),%rcx
je     1150 <_trim_spaces+0x30>
mov    %ecx,%edx
cmp    %rdx,%rax
jbe    118f <_trim_spaces+0x6f>
add    %rcx,%rbx
cmpb   $0x20,-0x1(%rbx,%rax,1)
jne    1191 <_trim_spaces+0x71>
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movb   $0x0,(%rax)
cmpb   $0x20,-0x1(%rax)
lea    -0x1(%rax),%rax
je     1180 <_trim_spaces+0x60>
jmp    1191 <_trim_spaces+0x71>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

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