#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(unsigned char);
int strnlen(char *, size_t);
size_t trim(unsigned char *buf, size_t n) {
  size_t i;
  for (i = strnlen((char *)buf, n) - 1; i >= 0; i--) {
    if (!isspace(buf[i])) {
      break;
    }
  }
  i++;
  buf[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strnlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jb     1186 <trim+0x66>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
jne    1170 <trim+0x50>
jmp    1186 <trim+0x66>
jmp    1175 <trim+0x55>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1146 <trim+0x26>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strnlen@plt>
movslq %eax,%rbx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl -0x2(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
add    $0xffffffffffffffff,%rbx
test   %eax,%eax
jne    1140 <trim+0x20>
movb   $0x0,(%r14,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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