#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 strlen(char *);
char *string_trim(char *s, int func(int)) {
  char *p;
  while (func(*s))
    s++;
  p = s + strlen(s) - 1;
  while (func(*p))
    p--;
  *(p + 1) = 0;
  return s;
}


x86 assembly (clang, -O0):
<string_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movsbl (%rcx),%edi
call   *%rax
cmp    $0x0,%eax
je     1147 <string_trim+0x37>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1120 <string_trim+0x10>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%edi
call   *%rax
cmp    $0x0,%eax
je     1193 <string_trim+0x83>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    116c <string_trim+0x5c>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_trim>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
add    $0xffffffffffffffff,%r15
nop
movsbl 0x1(%r15),%edi
add    $0x1,%r15
call   *%r14
test   %eax,%eax
jne    1120 <string_trim+0x10>
mov    %r15,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
nopl   0x0(%rax,%rax,1)
movsbl -0x1(%r15,%rbx,1),%edi
call   *%r14
add    $0xffffffffffffffff,%rbx
test   %eax,%eax
jne    1140 <string_trim+0x30>
movb   $0x0,0x1(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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