#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);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void trail_space(char *s) {
  int i = 0;
  while (isspace(s[i]))
    ++i;
  if (i)
    strcpy(s, s + i);
  i = strlen(s) - 1;
  while (i > 0 && isspace(s[i]))
    s[i--] = '\0';
}


x86 assembly (clang, -O0):
<trail_space>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     116c <trail_space+0x3c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1143 <trail_space+0x13>
cmpl   $0x0,-0xc(%rbp)
je     118a <trail_space+0x5a>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jle    11c3 <trail_space+0x93>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    11d3 <trail_space+0xa3>
jmp    11ee <trail_space+0xbe>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1199 <trail_space+0x69>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trail_space>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0xffffffffffffffff,%rbx
nop
movsbl 0x1(%r14,%rbx,1),%edi
call   1050 <isspace@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1140 <trail_space+0x10>
test   %ebx,%ebx
je     1165 <trail_space+0x35>
mov    %ebx,%esi
add    %r14,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     11a2 <trail_space+0x72>
mov    %eax,%ebx
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbx),%r15d
movsbl (%r14,%r15,1),%edi
call   1050 <isspace@plt>
test   %rax,%rax
je     11a2 <trail_space+0x72>
movb   $0x0,(%r14,%r15,1)
add    $0xffffffffffffffff,%rbx
cmp    $0x2,%rbx
ja     1180 <trail_space+0x50>
pop    %rbx
pop    %r14
pop    %r15
ret

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

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