void lnklist_destroy_with_destructor(struct lnklist *obj,
                                     void (*destructor)(void *)) {
  lnklist_iter_init(obj);
  while (lnklist_iter_hasnext(obj)) {
    destructor ? destructor(lnklist_iter_remove_next(obj))
               : lnklist_iter_remove_next(obj);
  }
  free(obj);
}


x86 assembly (clang, -O0):
<lnklist_destroy_with_destructor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1050 <lnklist_iter_init@plt>
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1040 <lnklist_iter_hasnext@plt>
cmp    $0x0,%eax
je     11ad <lnklist_destroy_with_destructor+0x6d>
cmpq   $0x0,-0x10(%rbp)
je     119d <lnklist_destroy_with_destructor+0x5d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1060 <lnklist_iter_remove_next@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rdi
call   *%rax
jmp    11a8 <lnklist_destroy_with_destructor+0x68>
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1060 <lnklist_iter_remove_next@plt>
jmp    115b <lnklist_destroy_with_destructor+0x1b>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lnklist_destroy_with_destructor>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <lnklist_iter_init@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <lnklist_iter_hasnext@plt>
test   %eax,%eax
je     1185 <lnklist_destroy_with_destructor+0x45>
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <lnklist_iter_remove_next@plt>
test   %r14,%r14
je     1160 <lnklist_destroy_with_destructor+0x20>
movslq %eax,%rdi
call   *%r14
jmp    1160 <lnklist_destroy_with_destructor+0x20>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.lnklist = type opaque
define void @lnklist_destroy_with_destructor(%struct.lnklist* %0, void (i8*)* %1) {
%3 = alloca %struct.lnklist*
%4 = alloca void (i8*)*
store %struct.lnklist* %0, %struct.lnklist** %3
store void (i8*)* %1, void (i8*)** %4
%5 = load %struct.lnklist*, %struct.lnklist** %3
%6 = call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_init to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %5)
br label %7
7: ; preds = %23, %2
%8 = load %struct.lnklist*, %struct.lnklist** %3
%9 = call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_hasnext to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %7
%12 = load void (i8*)*, void (i8*)** %4
%13 = icmp ne void (i8*)* %12, null
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = load void (i8*)*, void (i8*)** %4
%16 = load %struct.lnklist*, %struct.lnklist** %3
%17 = call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_remove_next to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %16)
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
br label %23
20: ; preds = %11
%21 = load %struct.lnklist*, %struct.lnklist** %3
%22 = call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_remove_next to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %21)
br label %23
23: ; preds = %20, %14
br label %7
24: ; preds = %7
%25 = load %struct.lnklist*, %struct.lnklist** %3
%26 = bitcast %struct.lnklist* %25 to i8*
ret void
}

LLVM IR (-O2):
%struct.lnklist = type opaque
define void @lnklist_destroy_with_destructor(%struct.lnklist* %0, void (i8*)* %1) {
%3 = tail call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_init to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %0)
%4 = tail call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_hasnext to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %2
%7 = icmp eq void (i8*)* %1, null
br label %8
8: ; preds = %6, %13
%9 = tail call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_remove_next to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %0)
br i1 %7, label %13, label %10
10: ; preds = %8
%11 = sext i32 %9 to i64
%12 = inttoptr i64 %11 to i8*
tail call void %1(i8* %12)
br label %13
13: ; preds = %8, %10
%14 = tail call i32 (%struct.lnklist*, ...) bitcast (i32 (...)* @lnklist_iter_hasnext to i32 (%struct.lnklist*, ...)*)(%struct.lnklist* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %13, %2
%17 = bitcast %struct.lnklist* %0 to i8*
tail call void @free(i8* %17)
ret void
}