#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 value {
  int dummy;
};
struct list {
  int dummy;
};
struct function {
  int dummy;
};
struct cursor {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ cursor_delete(struct cursor *);
struct function *cursor_get(struct cursor *);
struct cursor *cursor_new_back(struct list *);
int /*<<< orphan*/ cursor_prev(struct cursor *);
scalar_t__ cursor_valid(struct cursor *);
struct value *function_exec(struct function *, struct value *);
struct value *compose(struct list *args, struct value *in) {
  struct value *current = in;
  struct value *last = in;
  struct function *f = NULL;
  struct cursor *c = NULL;
  for (c = cursor_new_back(args); cursor_valid(c); cursor_prev(c)) {
    f = cursor_get(c);
    last = current;
    current = function_exec(f, current);
  }
  cursor_delete(c);
  return current;
}


x86 assembly (clang, -O0):
<compose>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <cursor_new_back@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <cursor_valid@plt>
cmp    $0x0,%rax
je     11e4 <compose+0x84>
mov    -0x30(%rbp),%rdi
call   1060 <cursor_get@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <function_exec@plt>
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <cursor_prev@plt>
jmp    119d <compose+0x3d>
mov    -0x30(%rbp),%rdi
call   1070 <cursor_delete@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compose>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1040 <cursor_new_back@plt>
mov    %rax,%r14
mov    %rax,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1030 <cursor_valid@plt>
test   %rax,%rax
je     11ad <compose+0x4d>
mov    %r14,%rdi
call   1060 <cursor_get@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <function_exec@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1080 <cursor_prev@plt>
mov    %r14,%rdi
jmp    1180 <compose+0x20>
mov    %r14,%rdi
call   1070 <cursor_delete@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.list = type { i32 }
%struct.value = type { i32 }
%struct.function = type { i32 }
%struct.cursor = type { i32 }
define %struct.value* @compose(%struct.list* %0, %struct.value* %1) {
%3 = alloca %struct.list*
%4 = alloca %struct.value*
%5 = alloca %struct.value*
%6 = alloca %struct.value*
%7 = alloca %struct.function*
%8 = alloca %struct.cursor*
store %struct.list* %0, %struct.list** %3
store %struct.value* %1, %struct.value** %4
%9 = load %struct.value*, %struct.value** %4
store %struct.value* %9, %struct.value** %5
%10 = load %struct.value*, %struct.value** %4
store %struct.value* %10, %struct.value** %6
store %struct.function* null, %struct.function** %7
store %struct.cursor* null, %struct.cursor** %8
%11 = load %struct.list*, %struct.list** %3
%12 = call %struct.cursor* @cursor_new_back(%struct.list* %11)
store %struct.cursor* %12, %struct.cursor** %8
br label %13
13: ; preds = %24, %2
%14 = load %struct.cursor*, %struct.cursor** %8
%15 = call i64 @cursor_valid(%struct.cursor* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %13
%18 = load %struct.cursor*, %struct.cursor** %8
%19 = call %struct.function* @cursor_get(%struct.cursor* %18)
store %struct.function* %19, %struct.function** %7
%20 = load %struct.value*, %struct.value** %5
store %struct.value* %20, %struct.value** %6
%21 = load %struct.function*, %struct.function** %7
%22 = load %struct.value*, %struct.value** %5
%23 = call %struct.value* @function_exec(%struct.function* %21, %struct.value* %22)
store %struct.value* %23, %struct.value** %5
br label %24
24: ; preds = %17
%25 = load %struct.cursor*, %struct.cursor** %8
%26 = call i32 @cursor_prev(%struct.cursor* %25)
br label %13
27: ; preds = %13
%28 = load %struct.cursor*, %struct.cursor** %8
%29 = call i32 @cursor_delete(%struct.cursor* %28)
%30 = load %struct.value*, %struct.value** %5
ret %struct.value* %30
}

LLVM IR (-O2):
%struct.list = type { i32 }
%struct.value = type { i32 }
%struct.function = type { i32 }
%struct.cursor = type { i32 }
define %struct.value* @compose(%struct.list* %0, %struct.value* %1) {
%3 = tail call %struct.cursor* @cursor_new_back(%struct.list* %0)
%4 = tail call i64 @cursor_valid(%struct.cursor* %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %2, %6
%7 = phi %struct.value* [ %9, %6 ], [ %1, %2 ]
%8 = tail call %struct.function* @cursor_get(%struct.cursor* %3)
%9 = tail call %struct.value* @function_exec(%struct.function* %8, %struct.value* %7)
%10 = tail call i32 @cursor_prev(%struct.cursor* %3)
%11 = tail call i64 @cursor_valid(%struct.cursor* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %6
13: ; preds = %6, %2
%14 = phi %struct.value* [ %1, %2 ], [ %9, %6 ]
%15 = tail call i32 @cursor_delete(%struct.cursor* %3)
ret %struct.value* %14
}