#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(unsigned char);
char *wg_trim(char *dest, const char *src) {
  char *lastnonspace = &dest[-1];
  const char *p = src;
  char *w = dest;
  while (isspace((unsigned char)*p)) {
    p++;
  }
  while (*p) {
    if (!isspace((unsigned char)*p)) {
      lastnonspace = w;
    }
    *w++ = *p++;
  }
  lastnonspace[1] = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<wg_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
je     1163 <wg_trim+0x53>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    113c <wg_trim+0x2c>
jmp    1168 <wg_trim+0x58>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ba <wg_trim+0xaa>
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
jne    1193 <wg_trim+0x83>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    1168 <wg_trim+0x58>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wg_trim>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
nop
movzbl (%r12),%edi
call   1030 <isspace@plt>
add    $0x1,%r12
test   %rax,%rax
jne    1120 <wg_trim+0x10>
lea    -0x1(%r14),%r15
mov    -0x1(%r12),%al
test   %al,%al
je     117a <wg_trim+0x6a>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%rbx,1),%r13
movzbl %al,%edi
call   1030 <isspace@plt>
test   %rax,%rax
cmove  %r13,%r15
movzbl -0x1(%r12,%rbx,1),%eax
mov    %al,(%r14,%rbx,1)
movzbl (%r12,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    1150 <wg_trim+0x40>
movb   $0x0,0x1(%r15)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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