#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 */

/* Variables and functions */
int *malloc(int);
int *ft_map(int *tab, int length, int (*f)(int)) {
  int *tab_copy;
  tab_copy = malloc(sizeof(int) * length);
  while (length-- > 0)
    *tab_copy++ = f(*tab++);
  return (tab_copy);
}


x86 assembly (clang, -O0):
<ft_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
jle    1179 <ft_map+0x69>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x4,%rdx
mov    %rdx,-0x8(%rbp)
mov    (%rcx),%edi
call   *%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x20(%rbp)
mov    %ecx,(%rax)
jmp    1136 <ft_map+0x26>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_map>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r15
lea    0x0(,%r12,4),%edi
call   1030 <malloc@plt>
mov    %rax,%rbx
test   %r12d,%r12d
jle    115a <ft_map+0x4a>
add    $0x1,%r12d
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
add    $0x4,%r15
call   *%r14
mov    %eax,(%rbx)
add    $0x4,%rbx
add    $0xffffffff,%r12d
cmp    $0x1,%r12d
ja     1140 <ft_map+0x30>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @ft_map(i32* %0, i32 %1, i32 (i32)* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32 (i32)*
%7 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 (i32)* %2, i32 (i32)** %6
%8 = load i32, i32* %5
%9 = sext i32 %8 to i64
%10 = mul i64 4, %9
%11 = trunc i64 %10 to i32
%12 = call i32* @malloc(i32 %11)
store i32* %12, i32** %7
br label %13
13: ; preds = %17, %3
%14 = load i32, i32* %5
%15 = add nsw i32 %14, -1
store i32 %15, i32* %5
%16 = icmp sgt i32 %14, 0
br i1 %16, label %17, label %25
17: ; preds = %13
%18 = load i32 (i32)*, i32 (i32)** %6
%19 = load i32*, i32** %4
%20 = getelementptr inbounds i32, i32* %19, i32 1
store i32* %20, i32** %4
%21 = load i32, i32* %19
%22 = call i32 %18(i32 %21)
%23 = load i32*, i32** %7
%24 = getelementptr inbounds i32, i32* %23, i32 1
store i32* %24, i32** %7
store i32 %22, i32* %23
br label %13
25: ; preds = %13
%26 = load i32*, i32** %7
ret i32* %26
}

LLVM IR (-O2):
define i32* @ft_map(i32* nocapture %0, i32 %1, i32 (i32)* nocapture %2) {
%4 = shl i32 %1, 2
%5 = tail call i32* @malloc(i32 %4)
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %17
7: ; preds = %3, %7
%8 = phi i32* [ %15, %7 ], [ %5, %3 ]
%9 = phi i32* [ %12, %7 ], [ %0, %3 ]
%10 = phi i32 [ %11, %7 ], [ %1, %3 ]
%11 = add nsw i32 %10, -1
%12 = getelementptr inbounds i32, i32* %9, i64 1
%13 = load i32, i32* %9
%14 = tail call i32 %2(i32 %13)
%15 = getelementptr inbounds i32, i32* %8, i64 1
store i32 %14, i32* %8
%16 = icmp ugt i32 %10, 1
br i1 %16, label %7, label %17
17: ; preds = %7, %3
%18 = phi i32* [ %5, %3 ], [ %15, %7 ]
ret i32* %18
}