#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 tree_it {
  int dummy;
};
struct tree {
  int dummy;
};

/* Variables and functions */
void *object_copy(int /*<<< orphan*/);
struct tree *tree_create();
int /*<<< orphan*/ tree_insert(struct tree *, void *);
struct tree_it *tree_it(struct tree *);
int /*<<< orphan*/ tree_it_data(struct tree_it *);
struct tree_it *tree_it_next(struct tree_it *);
struct tree *tree_map(struct tree *tree, void (*f)(void *)) {
  struct tree *result = tree_create();
  struct tree_it *it;
  for (it = tree_it(tree); it != NULL; it = tree_it_next(it)) {
    void *obj = object_copy(tree_it_data(it));
    f(obj);
    tree_insert(result, obj);
  }
  return result;
}


x86 assembly (clang, -O0):
<tree_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <tree_create@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <tree_it@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d0 <tree_map+0x70>
mov    -0x20(%rbp),%rdi
call   1030 <tree_it_data@plt>
mov    %eax,%edi
call   1070 <object_copy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rdi
call   *%rax
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <tree_insert@plt>
mov    -0x20(%rbp),%rdi
call   1050 <tree_it_next@plt>
mov    %rax,-0x20(%rbp)
jmp    1188 <tree_map+0x28>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tree_map>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <tree_create@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <tree_it@plt>
test   %rax,%rax
je     11c3 <tree_map+0x63>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <tree_it_data@plt>
mov    %eax,%edi
call   1070 <object_copy@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   *%r14
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <tree_insert@plt>
mov    %rbx,%rdi
call   1050 <tree_it_next@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <tree_map+0x30>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tree = type { i32 }
%struct.tree_it = type { i32 }
define %struct.tree* @tree_map(%struct.tree* %0, void (i8*)* %1) {
%3 = alloca %struct.tree*
%4 = alloca void (i8*)*
%5 = alloca %struct.tree*
%6 = alloca %struct.tree_it*
%7 = alloca i8*
store %struct.tree* %0, %struct.tree** %3
store void (i8*)* %1, void (i8*)** %4
%8 = call %struct.tree* (...) @tree_create()
store %struct.tree* %8, %struct.tree** %5
%9 = load %struct.tree*, %struct.tree** %3
%10 = call %struct.tree_it* @tree_it(%struct.tree* %9)
store %struct.tree_it* %10, %struct.tree_it** %6
br label %11
11: ; preds = %23, %2
%12 = load %struct.tree_it*, %struct.tree_it** %6
%13 = icmp ne %struct.tree_it* %12, null
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = load %struct.tree_it*, %struct.tree_it** %6
%16 = call i32 @tree_it_data(%struct.tree_it* %15)
%17 = call i8* @object_copy(i32 %16)
store i8* %17, i8** %7
%18 = load void (i8*)*, void (i8*)** %4
%19 = load i8*, i8** %7
%20 = load %struct.tree*, %struct.tree** %5
%21 = load i8*, i8** %7
%22 = call i32 @tree_insert(%struct.tree* %20, i8* %21)
br label %23
23: ; preds = %14
%24 = load %struct.tree_it*, %struct.tree_it** %6
%25 = call %struct.tree_it* @tree_it_next(%struct.tree_it* %24)
store %struct.tree_it* %25, %struct.tree_it** %6
br label %11
26: ; preds = %11
%27 = load %struct.tree*, %struct.tree** %5
ret %struct.tree* %27
}

LLVM IR (-O2):
%struct.tree = type { i32 }
%struct.tree_it = type { i32 }
define %struct.tree* @tree_map(%struct.tree* %0, void (i8*)* nocapture %1) {
%3 = tail call %struct.tree* (...) @tree_create()
%4 = tail call %struct.tree_it* @tree_it(%struct.tree* %0)
%5 = icmp eq %struct.tree_it* %4, null
br i1 %5, label %13, label %6
6: ; preds = %2, %6
%7 = phi %struct.tree_it* [ %11, %6 ], [ %4, %2 ]
%8 = tail call i32 @tree_it_data(%struct.tree_it* %7)
%9 = tail call i8* @object_copy(i32 %8)
tail call void %1(i8* %9)
%10 = tail call i32 @tree_insert(%struct.tree* %3, i8* %9)
%11 = tail call %struct.tree_it* @tree_it_next(%struct.tree_it* %7)
%12 = icmp eq %struct.tree_it* %11, null
br i1 %12, label %13, label %6
13: ; preds = %6, %2
ret %struct.tree* %3
}