#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 */
typedef void sl_node;

/* Variables and functions */
void *sl_pop(void **);
void *sl_unshift(void *, void *);
void *sl_filter(void *root, int (*func)(void *, void *), void *data) {
  struct sl_node **pp = root;
  struct sl_node *p = *pp;
  struct sl_node *r = NULL, *head = NULL;
  while (p) {
    struct sl_node *q = sl_pop(&p);
    int val = func(q, data);
    if (!val) {
      head = sl_unshift(head, q);
    } else {
      r = sl_unshift(r, q);
      if (val < 0) {
        break;
      }
    }
  }
  *pp = sl_unshift(head, p);
  return r;
}


x86 assembly (clang, -O0):
<sl_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ca <sl_filter+0xaa>
lea    -0x28(%rbp),%rdi
call   1040 <sl_pop@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    11a0 <sl_filter+0x80>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1030 <sl_unshift@plt>
mov    %rax,-0x38(%rbp)
jmp    11c5 <sl_filter+0xa5>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1030 <sl_unshift@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11c0 <sl_filter+0xa0>
jmp    11ca <sl_filter+0xaa>
jmp    11c5 <sl_filter+0xa5>
jmp    1157 <sl_filter+0x37>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <sl_unshift@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sl_filter>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %rdi,0x8(%rsp)
mov    (%rdi),%rsi
mov    %rsi,0x10(%rsp)
test   %rsi,%rsi
je     119d <sl_filter+0x7d>
mov    %rdx,%r12
xor    %ebx,%ebx
xor    %r15d,%r15d
jmp    1168 <sl_filter+0x48>
nopl   (%rax)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <sl_unshift@plt>
mov    %rax,%r15
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     11a2 <sl_filter+0x82>
lea    0x10(%rsp),%rdi
call   1040 <sl_pop@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
call   *%r13
test   %eax,%eax
je     1150 <sl_filter+0x30>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <sl_unshift@plt>
mov    %rax,%rbx
test   %ebp,%ebp
jns    115e <sl_filter+0x3e>
mov    0x10(%rsp),%rsi
jmp    11a2 <sl_filter+0x82>
xor    %r15d,%r15d
xor    %ebx,%ebx
mov    0x8(%rsp),%rbp
mov    %r15,%rdi
call   1030 <sl_unshift@plt>
mov    %rax,0x0(%rbp)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sl_node = type opaque
define i8* @sl_filter(i8* %0, i32 (i8*, i8*)* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32 (i8*, i8*)*
%6 = alloca i8*
%7 = alloca %struct.sl_node**
%8 = alloca %struct.sl_node*
%9 = alloca %struct.sl_node*
%10 = alloca %struct.sl_node*
%11 = alloca %struct.sl_node*
%12 = alloca i32
store i8* %0, i8** %4
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %5
store i8* %2, i8** %6
%13 = load i8*, i8** %4
%14 = bitcast i8* %13 to %struct.sl_node**
store %struct.sl_node** %14, %struct.sl_node*** %7
%15 = load %struct.sl_node**, %struct.sl_node*** %7
%16 = load %struct.sl_node*, %struct.sl_node** %15
store %struct.sl_node* %16, %struct.sl_node** %8
store %struct.sl_node* null, %struct.sl_node** %9
store %struct.sl_node* null, %struct.sl_node** %10
br label %17
17: ; preds = %49, %3
%18 = load %struct.sl_node*, %struct.sl_node** %8
%19 = icmp ne %struct.sl_node* %18, null
br i1 %19, label %20, label %50
20: ; preds = %17
%21 = bitcast %struct.sl_node** %8 to i8**
%22 = call i8* @sl_pop(i8** %21)
%23 = bitcast i8* %22 to %struct.sl_node*
store %struct.sl_node* %23, %struct.sl_node** %11
%24 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %5
%25 = load %struct.sl_node*, %struct.sl_node** %11
%26 = bitcast %struct.sl_node* %25 to i8*
%27 = load i8*, i8** %6
%28 = call i32 %24(i8* %26, i8* %27)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp ne i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %20
%32 = load %struct.sl_node*, %struct.sl_node** %10
%33 = bitcast %struct.sl_node* %32 to i8*
%34 = load %struct.sl_node*, %struct.sl_node** %11
%35 = bitcast %struct.sl_node* %34 to i8*
%36 = call i8* @sl_unshift(i8* %33, i8* %35)
%37 = bitcast i8* %36 to %struct.sl_node*
store %struct.sl_node* %37, %struct.sl_node** %10
br label %49
38: ; preds = %20
%39 = load %struct.sl_node*, %struct.sl_node** %9
%40 = bitcast %struct.sl_node* %39 to i8*
%41 = load %struct.sl_node*, %struct.sl_node** %11
%42 = bitcast %struct.sl_node* %41 to i8*
%43 = call i8* @sl_unshift(i8* %40, i8* %42)
%44 = bitcast i8* %43 to %struct.sl_node*
store %struct.sl_node* %44, %struct.sl_node** %9
%45 = load i32, i32* %12
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %38
br label %50
48: ; preds = %38
br label %49
49: ; preds = %48, %31
br label %17
50: ; preds = %47, %17
%51 = load %struct.sl_node*, %struct.sl_node** %10
%52 = bitcast %struct.sl_node* %51 to i8*
%53 = load %struct.sl_node*, %struct.sl_node** %8
%54 = bitcast %struct.sl_node* %53 to i8*
%55 = call i8* @sl_unshift(i8* %52, i8* %54)
%56 = bitcast i8* %55 to %struct.sl_node*
%57 = load %struct.sl_node**, %struct.sl_node*** %7
store %struct.sl_node* %56, %struct.sl_node** %57
%58 = load %struct.sl_node*, %struct.sl_node** %9
%59 = bitcast %struct.sl_node* %58 to i8*
ret i8* %59
}

LLVM IR (-O2):
%struct.sl_node = type opaque
define i8* @sl_filter(i8* nocapture %0, i32 (i8*, i8*)* nocapture %1, i8* %2) {
%4 = alloca %struct.sl_node*
%5 = bitcast i8* %0 to %struct.sl_node**
%6 = bitcast %struct.sl_node** %4 to i8*
%7 = load %struct.sl_node*, %struct.sl_node** %5
store %struct.sl_node* %7, %struct.sl_node** %4
%8 = icmp eq %struct.sl_node* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = bitcast %struct.sl_node* %7 to i8*
br label %33
11: ; preds = %3
%12 = bitcast %struct.sl_node** %4 to i8**
br label %18
13: ; preds = %24, %26
%14 = phi i8* [ %20, %26 ], [ %25, %24 ]
%15 = phi i8* [ %27, %26 ], [ %19, %24 ]
%16 = load %struct.sl_node*, %struct.sl_node** %4
%17 = icmp eq %struct.sl_node* %16, null
br i1 %17, label %31, label %18
18: ; preds = %11, %13
%19 = phi i8* [ null, %11 ], [ %15, %13 ]
%20 = phi i8* [ null, %11 ], [ %14, %13 ]
%21 = call i8* @sl_pop(i8** %12)
%22 = call i32 %1(i8* %21, i8* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i8* @sl_unshift(i8* %20, i8* %21)
br label %13
26: ; preds = %18
%27 = call i8* @sl_unshift(i8* %19, i8* %21)
%28 = icmp slt i32 %22, 0
br i1 %28, label %29, label %13
29: ; preds = %26
%30 = load i8*, i8** %12
br label %33
31: ; preds = %13
%32 = bitcast %struct.sl_node* %16 to i8*
br label %33
33: ; preds = %29, %31, %9
%34 = phi i8* [ %10, %9 ], [ %30, %29 ], [ %32, %31 ]
%35 = phi i8* [ null, %9 ], [ %20, %29 ], [ %14, %31 ]
%36 = phi i8* [ null, %9 ], [ %27, %29 ], [ %15, %31 ]
%37 = call i8* @sl_unshift(i8* %35, i8* %34)
%38 = bitcast i8* %0 to i8**
store i8* %37, i8** %38
ret i8* %36
}