#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);
int strlen(char *);
char *droplastword(char *str) {
  char *pch = (char *)(str + (strlen(str) - 1));
  if (*pch == '\n')
    pch--;
  while (!isspace(*pch))
    pch--;
  *pch = '\0';
  return (str);
}


x86 assembly (clang, -O0):
<droplastword>:
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),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    116c <droplastword+0x4c>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1171 <droplastword+0x51>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1192 <droplastword+0x72>
jmp    11a3 <droplastword+0x83>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1171 <droplastword+0x51>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<droplastword>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
cltq
xor    %ecx,%ecx
cmpb   $0xa,-0x1(%r14,%rax,1)
lea    -0x1(%rax),%rax
sete   %cl
sub    %rcx,%rax
lea    (%rax,%r14,1),%rbx
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
movsbl -0x1(%rbx),%edi
add    $0xffffffffffffffff,%rbx
call   1040 <isspace@plt>
test   %eax,%eax
je     1150 <droplastword+0x30>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @droplastword(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
%10 = load i8*, i8** %3
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 10
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i32 -1
store i8* %16, i8** %3
br label %17
17: ; preds = %14, %1
br label %18
18: ; preds = %24, %17
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = call i32 @isspace(i8 signext %20)
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %3
br label %18
27: ; preds = %18
%28 = load i8*, i8** %3
store i8 0, i8* %28
%29 = load i8*, i8** %2
ret i8* %29
}

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