#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 /*<<< orphan*/ isspace(char);
void trim_right(char *s) {
  char *p;
  char *herep;
  enum { BLANKS, NONBLANKS } state = NONBLANKS;
  for (herep = 0, p = s; *p != 0; p++) {
    switch (state) {
    case BLANKS:
      if (!isspace(*p)) {
        state = NONBLANKS;
      }
      break;
    case NONBLANKS:
      if (isspace(*p)) {
        herep = p;
        state = BLANKS;
      }
    }
  }
  if (state == BLANKS)
    *herep = 0;
}


x86 assembly (clang, -O0):
<trim_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x1c(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c7 <trim_right+0xb7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
test   %eax,%eax
je     1167 <trim_right+0x57>
jmp    1156 <trim_right+0x46>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
je     1188 <trim_right+0x78>
jmp    11b1 <trim_right+0xa1>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%eax
jne    1183 <trim_right+0x73>
movl   $0x1,-0x1c(%rbp)
jmp    11b1 <trim_right+0xa1>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%eax
je     11ac <trim_right+0x9c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    11b1 <trim_right+0xa1>
jmp    11b6 <trim_right+0xa6>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <trim_right+0x23>
cmpl   $0x0,-0x1c(%rbp)
jne    11d8 <trim_right+0xc8>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_right>:
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
test   %al,%al
je     1171 <trim_right+0x61>
mov    %rdi,%rbx
mov    $0x1,%ecx
xor    %r14d,%r14d
jmp    114f <trim_right+0x3f>
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <isspace@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
cmovne %rbx,%r14
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1169 <trim_right+0x59>
cmp    $0x1,%ecx
je     1130 <trim_right+0x20>
test   %ecx,%ecx
jne    1143 <trim_right+0x33>
movsbl %al,%edi
call   1030 <isspace@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
jmp    1143 <trim_right+0x33>
test   %ecx,%ecx
jne    1171 <trim_right+0x61>
movb   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @trim_right(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
store i32 1, i32* %5
store i8* null, i8** %4
%6 = load i8*, i8** %2
store i8* %6, i8** %3
br label %7
7: ; preds = %30, %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %7
%13 = load i32, i32* %5
switch i32 %13, label %29 [
i32 0, label %14
i32 1, label %21
]
14: ; preds = %12
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = call i32 @isspace(i8 signext %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 1, i32* %5
br label %20
20: ; preds = %19, %14
br label %29
21: ; preds = %12
%22 = load i8*, i8** %3
%23 = load i8, i8* %22
%24 = call i32 @isspace(i8 signext %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i8*, i8** %3
store i8* %27, i8** %4
store i32 0, i32* %5
br label %28
28: ; preds = %26, %21
br label %29
29: ; preds = %28, %12, %20
br label %30
30: ; preds = %29
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %3
br label %7
33: ; preds = %7
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load i8*, i8** %4
store i8 0, i8* %37
br label %38
38: ; preds = %36, %33
ret void
}

LLVM IR (-O2):
define void @trim_right(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %27, label %4
4: ; preds = %1, %18
%5 = phi i8 [ %22, %18 ], [ %2, %1 ]
%6 = phi i32 [ %20, %18 ], [ 1, %1 ]
%7 = phi i8* [ %19, %18 ], [ null, %1 ]
%8 = phi i8* [ %21, %18 ], [ %0, %1 ]
switch i32 %6, label %18 [
i32 0, label %9
i32 1, label %13
]
9: ; preds = %4
%10 = tail call i32 @isspace(i8 signext %5)
%11 = icmp eq i32 %10, 0
%12 = zext i1 %11 to i32
br label %18
13: ; preds = %4
%14 = tail call i32 @isspace(i8 signext %5)
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i8* %7, i8* %8
%17 = zext i1 %15 to i32
br label %18
18: ; preds = %13, %9, %4
%19 = phi i8* [ %7, %4 ], [ %7, %9 ], [ %16, %13 ]
%20 = phi i32 [ %6, %4 ], [ %12, %9 ], [ %17, %13 ]
%21 = getelementptr inbounds i8, i8* %8, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %4
24: ; preds = %18
%25 = icmp eq i32 %20, 0
br i1 %25, label %26, label %27
26: ; preds = %24
store i8 0, i8* %19
br label %27
27: ; preds = %1, %26, %24
ret void
}