#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 */
struct list {
  int dummy;
};

/* Variables and functions */
struct list *list_begin(struct list *);
struct list *list_end(struct list *);
int /*<<< orphan*/ list_enqueue_before(struct list *, struct list *);
struct list *list_next(struct list *);
void list_enqueue_sorted(struct list *head, struct list *newitem,
                         int (*cmp)(struct list *, struct list *)) {
  struct list *item = list_begin(head);
  while (item != list_end(head)) {
    if (cmp(newitem, item) > 0) {
      break;
    }
    item = list_next(item);
  }
  list_enqueue_before(item, newitem);
}


x86 assembly (clang, -O0):
<list_enqueue_sorted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <list_begin@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <list_end@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     11b0 <list_enqueue_sorted+0x70>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jle    119e <list_enqueue_sorted+0x5e>
jmp    11b0 <list_enqueue_sorted+0x70>
mov    -0x20(%rbp),%rdi
call   1030 <list_next@plt>
mov    %rax,-0x20(%rbp)
jmp    1161 <list_enqueue_sorted+0x21>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <list_enqueue_before@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_enqueue_sorted>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <list_begin@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%rbx
mov    %r12,%rdi
call   1060 <list_end@plt>
cmp    %rax,%rbx
je     1187 <list_enqueue_sorted+0x47>
mov    %r14,%rdi
mov    %rbx,%rsi
call   *%r15
test   %eax,%eax
jg     1187 <list_enqueue_sorted+0x47>
mov    %rbx,%rdi
call   1030 <list_next@plt>
jmp    1160 <list_enqueue_sorted+0x20>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <list_enqueue_before@plt>

LLVM IR (-O0):
%struct.list = type { i32 }
define void @list_enqueue_sorted(%struct.list* %0, %struct.list* %1, i32 (%struct.list*, %struct.list*)* %2) {
%4 = alloca %struct.list*
%5 = alloca %struct.list*
%6 = alloca i32 (%struct.list*, %struct.list*)*
%7 = alloca %struct.list*
store %struct.list* %0, %struct.list** %4
store %struct.list* %1, %struct.list** %5
store i32 (%struct.list*, %struct.list*)* %2, i32 (%struct.list*, %struct.list*)** %6
%8 = load %struct.list*, %struct.list** %4
%9 = call %struct.list* @list_begin(%struct.list* %8)
store %struct.list* %9, %struct.list** %7
br label %10
10: ; preds = %22, %3
%11 = load %struct.list*, %struct.list** %7
%12 = load %struct.list*, %struct.list** %4
%13 = call %struct.list* @list_end(%struct.list* %12)
%14 = icmp ne %struct.list* %11, %13
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = load i32 (%struct.list*, %struct.list*)*, i32 (%struct.list*, %struct.list*)** %6
%17 = load %struct.list*, %struct.list** %5
%18 = load %struct.list*, %struct.list** %7
%19 = call i32 %16(%struct.list* %17, %struct.list* %18)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %25
22: ; preds = %15
%23 = load %struct.list*, %struct.list** %7
%24 = call %struct.list* @list_next(%struct.list* %23)
store %struct.list* %24, %struct.list** %7
br label %10
25: ; preds = %21, %10
%26 = load %struct.list*, %struct.list** %7
%27 = load %struct.list*, %struct.list** %5
%28 = call i32 @list_enqueue_before(%struct.list* %26, %struct.list* %27)
ret void
}

LLVM IR (-O2):
%struct.list = type { i32 }
define void @list_enqueue_sorted(%struct.list* %0, %struct.list* %1, i32 (%struct.list*, %struct.list*)* nocapture %2) {
%4 = tail call %struct.list* @list_begin(%struct.list* %0)
%5 = tail call %struct.list* @list_end(%struct.list* %0)
%6 = icmp eq %struct.list* %4, %5
br i1 %6, label %15, label %7
7: ; preds = %3, %11
%8 = phi %struct.list* [ %12, %11 ], [ %4, %3 ]
%9 = tail call i32 %2(%struct.list* %1, %struct.list* %8)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = tail call %struct.list* @list_next(%struct.list* %8)
%13 = tail call %struct.list* @list_end(%struct.list* %0)
%14 = icmp eq %struct.list* %12, %13
br i1 %14, label %15, label %7
15: ; preds = %11, %7, %3
%16 = phi %struct.list* [ %4, %3 ], [ %8, %7 ], [ %12, %11 ]
%17 = tail call i32 @list_enqueue_before(%struct.list* %16, %struct.list* %1)
ret void
}