#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 strlen(char *);
void trail_space(char *s) {
  int i;
  while (isspace(*s)) {
    char *copy = s;
    do {
      copy[0] = copy[1];
      copy++;
    } while (*copy);
  }
  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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1175 <trail_space+0x55>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    114a <trail_space+0x2a>
jmp    112c <trail_space+0xc>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x19(%rbp)
jle    11ae <trail_space+0x8e>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11be <trail_space+0x9e>
jmp    11d9 <trail_space+0xb9>
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    1184 <trail_space+0x64>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trail_space>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
movsbl (%rdi),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     115f <trail_space+0x3f>
mov    %r14,%rcx
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rcx),%eax
mov    %al,(%rcx)
add    $0x1,%rcx
test   %al,%al
jne    1140 <trail_space+0x20>
movsbl (%r14),%edi
call   1040 <isspace@plt>
mov    %r14,%rcx
test   %rax,%rax
jne    1140 <trail_space+0x20>
mov    %r14,%rdi
call   1030 <strlen@plt>
cmp    $0x2,%eax
jl     11a2 <trail_space+0x82>
mov    %eax,%ebx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    -0x2(%rbx),%r15d
movsbl (%r14,%r15,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     11a2 <trail_space+0x82>
movb   $0x0,(%r14,%r15,1)
add    $0xffffffffffffffff,%rbx
cmp    $0x2,%rbx
ja     1180 <trail_space+0x60>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @trail_space(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
br label %5
5: ; preds = %24, %1
%6 = load i8*, i8** %2
%7 = load i8, i8* %6
%8 = call i64 @isspace(i8 signext %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %5
%11 = load i8*, i8** %2
store i8* %11, i8** %4
br label %12
12: ; preds = %20, %10
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 %15, i8* %17
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %4
br label %20
20: ; preds = %12
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %12, label %24
24: ; preds = %20
br label %5
25: ; preds = %5
%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) {
%2 = load i8, i8* %0
%3 = tail call i64 @isspace(i8 signext %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %16, label %9
5: ; preds = %9
%6 = load i8, i8* %0
%7 = tail call i64 @isspace(i8 signext %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, label %14
9: ; preds = %1, %14
%10 = phi i8* [ %15, %14 ], [ %0, %1 ]
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %11
store i8 %12, i8* %10
%13 = icmp eq i8 %12, 0
br i1 %13, label %5, label %14
14: ; preds = %9, %5
%15 = phi i8* [ %11, %9 ], [ %0, %5 ]
br label %9
16: ; preds = %5, %1
%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
}