#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 */
int /*<<< orphan*/ list_append(struct list *, void *);
void *list_get(struct list const *, unsigned int);
struct list *list_new(int /*<<< orphan*/);
unsigned int list_size(struct list const *);
struct list *list_map_r(const struct list *input_list,
                        void *(*fun)(void *, void *), void *args) {
  unsigned si = list_size(input_list);
  struct list *ret = list_new(0);
  for (unsigned i = 1; i <= si; ++i) {
    void *oldval = list_get(input_list, i);
    void *newval = fun(oldval, args);
    list_append(ret, newval);
  }
  return ret;
}


x86 assembly (clang, -O0):
<list_map_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <list_size@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1050 <list_new@plt>
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
ja     11bb <list_map_r+0x7b>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <list_get@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <list_append@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1172 <list_map_r+0x32>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_map_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <list_size@plt>
mov    %eax,%r13d
xor    %edi,%edi
call   1050 <list_new@plt>
mov    %rax,%rbx
test   %r13d,%r13d
je     1196 <list_map_r+0x56>
mov    $0x1,%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <list_get@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   *%r15
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <list_append@plt>
add    $0x1,%ebp
cmp    %r13d,%ebp
jbe    1170 <list_map_r+0x30>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.list = type { i32 }
define %struct.list* @list_map_r(%struct.list* %0, i8* (i8*, i8*)* %1, i8* %2) {
%4 = alloca %struct.list*
%5 = alloca i8* (i8*, i8*)*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.list*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store %struct.list* %0, %struct.list** %4
store i8* (i8*, i8*)* %1, i8* (i8*, i8*)** %5
store i8* %2, i8** %6
%12 = load %struct.list*, %struct.list** %4
%13 = call i32 @list_size(%struct.list* %12)
store i32 %13, i32* %7
%14 = call %struct.list* @list_new(i32 0)
store %struct.list* %14, %struct.list** %8
store i32 1, i32* %9
br label %15
15: ; preds = %30, %3
%16 = load i32, i32* %9
%17 = load i32, i32* %7
%18 = icmp ule i32 %16, %17
br i1 %18, label %19, label %33
19: ; preds = %15
%20 = load %struct.list*, %struct.list** %4
%21 = load i32, i32* %9
%22 = call i8* @list_get(%struct.list* %20, i32 %21)
store i8* %22, i8** %10
%23 = load i8* (i8*, i8*)*, i8* (i8*, i8*)** %5
%24 = load i8*, i8** %10
%25 = load i8*, i8** %6
%26 = call i8* %23(i8* %24, i8* %25)
store i8* %26, i8** %11
%27 = load %struct.list*, %struct.list** %8
%28 = load i8*, i8** %11
%29 = call i32 @list_append(%struct.list* %27, i8* %28)
br label %30
30: ; preds = %19
%31 = load i32, i32* %9
%32 = add i32 %31, 1
store i32 %32, i32* %9
br label %15
33: ; preds = %15
%34 = load %struct.list*, %struct.list** %8
ret %struct.list* %34
}

LLVM IR (-O2):
%struct.list = type { i32 }
define %struct.list* @list_map_r(%struct.list* %0, i8* (i8*, i8*)* nocapture %1, i8* %2) {
%4 = tail call i32 @list_size(%struct.list* %0)
%5 = tail call %struct.list* @list_new(i32 0)
%6 = icmp eq i32 %4, 0
br i1 %6, label %7, label %8
7: ; preds = %8, %3
ret %struct.list* %5
8: ; preds = %3, %8
%9 = phi i32 [ %13, %8 ], [ 1, %3 ]
%10 = tail call i8* @list_get(%struct.list* %0, i32 %9)
%11 = tail call i8* %1(i8* %10, i8* %2)
%12 = tail call i32 @list_append(%struct.list* %5, i8* %11)
%13 = add i32 %9, 1
%14 = icmp ugt i32 %13, %4
br i1 %14, label %7, label %8
}