#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(int);
int strlen(char *);
void str_trim_ws(char *string) {
  int i = strlen(string);
  while (i && isspace((int)string[i - 1]))
    string[--i] = '\0';
}


x86 assembly (clang, -O0):
<str_trim_ws>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
je     1167 <str_trim_ws+0x47>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1177 <str_trim_ws+0x57>
jmp    1190 <str_trim_ws+0x70>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1138 <str_trim_ws+0x18>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_trim_ws>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
je     116d <str_trim_ws+0x4d>
mov    %eax,%r15d
cltq
neg    %r15d
add    %rax,%r14
add    $0xffffffffffffffff,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     116d <str_trim_ws+0x4d>
movb   $0x0,(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
cmp    %ebx,%r15d
jne    1150 <str_trim_ws+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @str_trim_ws(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
br label %6
6: ; preds = %21, %1
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = load i8*, i8** %2
%11 = load i32, i32* %3
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = call i64 @isspace(i32 %16)
%18 = icmp ne i64 %17, 0
br label %19
19: ; preds = %9, %6
%20 = phi i1 [ false, %6 ], [ %18, %9 ]
br i1 %20, label %21, label %27
21: ; preds = %19
%22 = load i8*, i8** %2
%23 = load i32, i32* %3
%24 = add nsw i32 %23, -1
store i32 %24, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 0, i8* %26
br label %6
27: ; preds = %19
ret void
}

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