#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 strlen(char *);
void strip_trailing_sp(char *str) {
  int sz;
  sz = strlen(str);
  if (sz == 0)
    return;
  while (str[sz - 1] == ' ') {
    str[sz - 1] = 0;
    --sz;
    if (sz == 0)
      return;
  }
}


x86 assembly (clang, -O0):
<strip_trailing_sp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1137 <strip_trailing_sp+0x27>
jmp    1184 <strip_trailing_sp+0x74>
jmp    113c <strip_trailing_sp+0x2c>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    1184 <strip_trailing_sp+0x74>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    117f <strip_trailing_sp+0x6f>
jmp    1184 <strip_trailing_sp+0x74>
jmp    113c <strip_trailing_sp+0x2c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strip_trailing_sp>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
je     1142 <strip_trailing_sp+0x32>
movslq %eax,%rcx
neg    %eax
add    %rbx,%rcx
add    $0xffffffffffffffff,%rcx
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
cmpb   $0x20,(%rcx,%rdx,1)
jne    1142 <strip_trailing_sp+0x32>
movb   $0x0,(%rcx,%rdx,1)
add    $0xffffffffffffffff,%rdx
cmp    %edx,%eax
jne    1130 <strip_trailing_sp+0x20>
pop    %rbx
ret

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

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