#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 safestrlen(char *);
char *trunc_str(char *s) {
  char *t;
  if (s && *s) {
    for (t = s + safestrlen(s);
         t > s && isspace((int)(*(unsigned const char *)(t - 1))); --t)
      ;
    *t = 0;
  }
  return (s);
}


x86 assembly (clang, -O0):
<trunc_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11bf <trunc_str+0x9f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11bf <trunc_str+0x9f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <safestrlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jbe    1192 <trunc_str+0x72>
mov    -0x10(%rbp),%rax
movzbl -0x1(%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11a2 <trunc_str+0x82>
jmp    11b8 <trunc_str+0x98>
jmp    11a7 <trunc_str+0x87>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1168 <trunc_str+0x48>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trunc_str>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     116d <trunc_str+0x4d>
cmpb   $0x0,(%r14)
je     116d <trunc_str+0x4d>
mov    %r14,%rdi
call   1040 <safestrlen@plt>
movslq %eax,%r15
add    %r14,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rbx
cmp    %r14,%r15
jbe    116a <trunc_str+0x4a>
lea    -0x1(%rbx),%r15
movzbl -0x1(%rbx),%edi
call   1030 <isspace@plt>
test   %rax,%rax
jne    1150 <trunc_str+0x30>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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