#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 zap_trailing_spaces(char *str) {
  char *p = &str[strlen(str) - 1];
  while (p >= str && isspace(*p))
    *p-- = '\0';
}


x86 assembly (clang, -O0):
<zap_trailing_spaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jb     1179 <zap_trailing_spaces+0x59>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1189 <zap_trailing_spaces+0x69>
jmp    11a0 <zap_trailing_spaces+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,(%rax)
jmp    1150 <zap_trailing_spaces+0x30>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zap_trailing_spaces>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    1159 <zap_trailing_spaces+0x39>
mov    %eax,%ebx
add    $0xffffffff,%ebx
add    %r14,%rbx
nopl   0x0(%rax,%rax,1)
movsbl (%rbx),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1159 <zap_trailing_spaces+0x39>
movb   $0x0,(%rbx)
add    $0xffffffffffffffff,%rbx
cmp    %r14,%rbx
jae    1140 <zap_trailing_spaces+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @zap_trailing_spaces(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = sub nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %4, i64 %8
store i8* %9, i8** %3
br label %10
10: ; preds = %21, %1
%11 = load i8*, i8** %3
%12 = load i8*, i8** %2
%13 = icmp uge i8* %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = call i64 @isspace(i8 signext %16)
%18 = icmp ne i64 %17, 0
br label %19
19: ; preds = %14, %10
%20 = phi i1 [ false, %10 ], [ %18, %14 ]
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i32 -1
store i8* %23, i8** %3
store i8 0, i8* %22
br label %10
24: ; preds = %19
ret void
}

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