#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__ cstring_at_black(char *);
char *cstring_skip_white(char *);
char *cstring_rstrip(char *str) {
  char *dst = str;
  char *end = str;
  char *src = cstring_skip_white(str);
  while (*src != 0) {
    if (cstring_at_black(src)) {
      end = src + 1;
    }
    *dst++ = *src++;
  }
  *end = 0;
  return str;
}


x86 assembly (clang, -O0):
<cstring_rstrip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <cstring_skip_white@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119f <cstring_rstrip+0x7f>
mov    -0x20(%rbp),%rdi
call   1030 <cstring_at_black@plt>
cmp    $0x0,%rax
je     1178 <cstring_rstrip+0x58>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    1149 <cstring_rstrip+0x29>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstring_rstrip>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <cstring_skip_white@plt>
cmpb   $0x0,(%rax)
mov    %r14,%r15
je     117a <cstring_rstrip+0x5a>
mov    %rax,%r12
mov    %r14,%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <cstring_at_black@plt>
lea    0x1(%r12),%rcx
test   %rax,%rax
cmovne %rcx,%r15
movzbl (%r12),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
cmpb   $0x0,0x1(%r12)
mov    %rcx,%r12
jne    1150 <cstring_rstrip+0x30>
movb   $0x0,(%r15)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @cstring_rstrip(i8* returned %0) {
%2 = tail call i8* @cstring_skip_white(i8* %0)
%3 = load i8, i8* %2
%4 = icmp eq i8 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %1, %5
%6 = phi i8* [ %11, %5 ], [ %2, %1 ]
%7 = phi i8* [ %12, %5 ], [ %0, %1 ]
%8 = phi i8* [ %14, %5 ], [ %0, %1 ]
%9 = tail call i64 @cstring_at_black(i8* %6)
%10 = icmp eq i64 %9, 0
%11 = getelementptr inbounds i8, i8* %6, i64 1
%12 = select i1 %10, i8* %7, i8* %11
%13 = load i8, i8* %6
%14 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %13, i8* %8
%15 = load i8, i8* %11
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %5
17: ; preds = %5, %1
%18 = phi i8* [ %0, %1 ], [ %12, %5 ]
store i8 0, i8* %18
ret i8* %0
}