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

/* Variables and functions */
struct list *cctools_list_create();
struct list_cursor *cctools_list_cursor_create(struct list *);
int /*<<< orphan*/ cctools_list_cursor_destroy(struct list_cursor *);
scalar_t__ cctools_list_get(struct list_cursor *, void **);
int /*<<< orphan*/ cctools_list_insert(struct list_cursor *, void *);
int /*<<< orphan*/ cctools_list_next(struct list_cursor *);
int /*<<< orphan*/ cctools_list_seek(struct list_cursor *, int /*<<< orphan*/);
struct list *list_duplicate(struct list *src) {
  void *item;
  struct list *dst = cctools_list_create();
  struct list_cursor *src_cur = cctools_list_cursor_create(src);
  struct list_cursor *dst_cur = cctools_list_cursor_create(dst);
  for (cctools_list_seek(src_cur, 0); cctools_list_get(src_cur, &item);
       cctools_list_next(src_cur)) {
    cctools_list_insert(dst_cur, item);
  }
  cctools_list_cursor_destroy(src_cur);
  cctools_list_cursor_destroy(dst_cur);
  return dst;
}


x86 assembly (clang, -O0):
<list_duplicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <cctools_list_create@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <cctools_list_cursor_create@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <cctools_list_cursor_create@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1070 <cctools_list_seek@plt>
mov    -0x20(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <cctools_list_get@plt>
cmp    $0x0,%rax
je     11de <list_duplicate+0x6e>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <cctools_list_insert@plt>
mov    -0x20(%rbp),%rdi
call   1090 <cctools_list_next@plt>
jmp    11ac <list_duplicate+0x3c>
mov    -0x20(%rbp),%rdi
call   1060 <cctools_list_cursor_destroy@plt>
mov    -0x28(%rbp),%rdi
call   1060 <cctools_list_cursor_destroy@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_duplicate>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <cctools_list_create@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1080 <cctools_list_cursor_create@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1080 <cctools_list_cursor_create@plt>
mov    %rax,%r15
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <cctools_list_seek@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <cctools_list_get@plt>
test   %rax,%rax
je     11e4 <list_duplicate+0x74>
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rsi
mov    %r15,%rdi
call   1030 <cctools_list_insert@plt>
mov    %rbx,%rdi
call   1090 <cctools_list_next@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <cctools_list_get@plt>
test   %rax,%rax
jne    11c0 <list_duplicate+0x50>
mov    %rbx,%rdi
call   1060 <cctools_list_cursor_destroy@plt>
mov    %r15,%rdi
call   1060 <cctools_list_cursor_destroy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.list = type { i32 }
%struct.list_cursor = type { i32 }
define %struct.list* @list_duplicate(%struct.list* %0) {
%2 = alloca %struct.list*
%3 = alloca i8*
%4 = alloca %struct.list*
%5 = alloca %struct.list_cursor*
%6 = alloca %struct.list_cursor*
store %struct.list* %0, %struct.list** %2
%7 = call %struct.list* (...) @cctools_list_create()
store %struct.list* %7, %struct.list** %4
%8 = load %struct.list*, %struct.list** %2
%9 = call %struct.list_cursor* @cctools_list_cursor_create(%struct.list* %8)
store %struct.list_cursor* %9, %struct.list_cursor** %5
%10 = load %struct.list*, %struct.list** %4
%11 = call %struct.list_cursor* @cctools_list_cursor_create(%struct.list* %10)
store %struct.list_cursor* %11, %struct.list_cursor** %6
%12 = load %struct.list_cursor*, %struct.list_cursor** %5
%13 = call i32 @cctools_list_seek(%struct.list_cursor* %12, i32 0)
br label %14
14: ; preds = %22, %1
%15 = load %struct.list_cursor*, %struct.list_cursor** %5
%16 = call i64 @cctools_list_get(%struct.list_cursor* %15, i8** %3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load %struct.list_cursor*, %struct.list_cursor** %6
%20 = load i8*, i8** %3
%21 = call i32 @cctools_list_insert(%struct.list_cursor* %19, i8* %20)
br label %22
22: ; preds = %18
%23 = load %struct.list_cursor*, %struct.list_cursor** %5
%24 = call i32 @cctools_list_next(%struct.list_cursor* %23)
br label %14
25: ; preds = %14
%26 = load %struct.list_cursor*, %struct.list_cursor** %5
%27 = call i32 @cctools_list_cursor_destroy(%struct.list_cursor* %26)
%28 = load %struct.list_cursor*, %struct.list_cursor** %6
%29 = call i32 @cctools_list_cursor_destroy(%struct.list_cursor* %28)
%30 = load %struct.list*, %struct.list** %4
ret %struct.list* %30
}

LLVM IR (-O2):
%struct.list = type { i32 }
%struct.list_cursor = type { i32 }
define %struct.list* @list_duplicate(%struct.list* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call %struct.list* (...) @cctools_list_create()
%5 = tail call %struct.list_cursor* @cctools_list_cursor_create(%struct.list* %0)
%6 = tail call %struct.list_cursor* @cctools_list_cursor_create(%struct.list* %4)
%7 = tail call i32 @cctools_list_seek(%struct.list_cursor* %5, i32 0)
%8 = call i64 @cctools_list_get(%struct.list_cursor* %5, i8** %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %1, %10
%11 = load i8*, i8** %2
%12 = call i32 @cctools_list_insert(%struct.list_cursor* %6, i8* %11)
%13 = call i32 @cctools_list_next(%struct.list_cursor* %5)
%14 = call i64 @cctools_list_get(%struct.list_cursor* %5, i8** %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %10
16: ; preds = %10, %1
%17 = call i32 @cctools_list_cursor_destroy(%struct.list_cursor* %5)
%18 = call i32 @cctools_list_cursor_destroy(%struct.list_cursor* %6)
ret %struct.list* %4
}