#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(char);
size_t strlen(char *);
char *trim_one_space(char *buf) {
  size_t len;
  if (isspace(buf[0])) {
    buf += 1;
  }
  len = strlen(buf);
  if (len == 0) {
    return buf;
  }
  if (isspace(buf[len - 1])) {
    buf[len - 1] = '\0';
  }
  return buf;
}


x86 assembly (clang, -O0):
<trim_one_space>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     114e <trim_one_space+0x2e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1173 <trim_one_space+0x53>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11aa <trim_one_space+0x8a>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11a2 <trim_one_space+0x82>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_one_space>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1040 <isspace@plt>
cmp    $0x1,%rax
sbb    $0xffffffffffffffff,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     115c <trim_one_space+0x3c>
mov    %rax,%r14
movsbl -0x1(%rax,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     115c <trim_one_space+0x3c>
movb   $0x0,-0x1(%rbx,%r14,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @trim_one_space(i8* %0) {
%2 = load i8, i8* %0
%3 = tail call i64 @isspace(i8 signext %2)
%4 = icmp ne i64 %3, 0
%5 = zext i1 %4 to i64
%6 = getelementptr i8, i8* %0, i64 %5
%7 = tail call i64 @strlen(i8* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %1
%10 = add i64 %7, -1
%11 = getelementptr inbounds i8, i8* %6, i64 %10
%12 = load i8, i8* %11
%13 = tail call i64 @isspace(i8 signext %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %9
store i8 0, i8* %11
br label %16
16: ; preds = %9, %15, %1
ret i8* %6
}