#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 linkedlist {
  int dummy;
};

/* Variables and functions */
struct linkedlist *linkedlist_cons(long, struct linkedlist *);
long linkedlist_head(struct linkedlist *);
struct linkedlist *linkedlist_tail(struct linkedlist *);
struct linkedlist *linkedlist_insert(long n, unsigned i, struct linkedlist *l) {
  long new_head;
  struct linkedlist *rec;
  if (i == 0)
    return linkedlist_cons(n, l);
  new_head = linkedlist_head(l);
  rec = linkedlist_insert(n, i - 1, linkedlist_tail(l));
  return linkedlist_cons(new_head, rec);
}


x86 assembly (clang, -O0):
<linkedlist_insert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1173 <linkedlist_insert+0x33>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <linkedlist_cons@plt>
mov    %rax,-0x8(%rbp)
jmp    11be <linkedlist_insert+0x7e>
mov    -0x20(%rbp),%rdi
call   1050 <linkedlist_head@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <linkedlist_tail@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %rax,%rdx
call   1060 <linkedlist_insert@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <linkedlist_cons@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linkedlist_insert>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rdi,%r14
test   %esi,%esi
je     117f <linkedlist_insert+0x3f>
mov    %esi,%ebx
mov    %r15,%rdi
call   1050 <linkedlist_head@plt>
mov    %rax,%r12
add    $0xffffffff,%ebx
mov    %r15,%rdi
call   1040 <linkedlist_tail@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1060 <linkedlist_insert@plt>
mov    %r12,%rdi
mov    %rax,%rsi
jmp    1185 <linkedlist_insert+0x45>
mov    %r14,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <linkedlist_cons@plt>

LLVM IR (-O0):
%struct.linkedlist = type { i32 }
define %struct.linkedlist* @linkedlist_insert(i64 %0, i32 %1, %struct.linkedlist* %2) {
%4 = alloca %struct.linkedlist*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.linkedlist*
%8 = alloca i64
%9 = alloca %struct.linkedlist*
store i64 %0, i64* %5
store i32 %1, i32* %6
store %struct.linkedlist* %2, %struct.linkedlist** %7
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i64, i64* %5
%14 = load %struct.linkedlist*, %struct.linkedlist** %7
%15 = call %struct.linkedlist* @linkedlist_cons(i64 %13, %struct.linkedlist* %14)
store %struct.linkedlist* %15, %struct.linkedlist** %4
br label %28
16: ; preds = %3
%17 = load %struct.linkedlist*, %struct.linkedlist** %7
%18 = call i64 @linkedlist_head(%struct.linkedlist* %17)
store i64 %18, i64* %8
%19 = load i64, i64* %5
%20 = load i32, i32* %6
%21 = sub i32 %20, 1
%22 = load %struct.linkedlist*, %struct.linkedlist** %7
%23 = call %struct.linkedlist* @linkedlist_tail(%struct.linkedlist* %22)
%24 = call %struct.linkedlist* @linkedlist_insert(i64 %19, i32 %21, %struct.linkedlist* %23)
store %struct.linkedlist* %24, %struct.linkedlist** %9
%25 = load i64, i64* %8
%26 = load %struct.linkedlist*, %struct.linkedlist** %9
%27 = call %struct.linkedlist* @linkedlist_cons(i64 %25, %struct.linkedlist* %26)
store %struct.linkedlist* %27, %struct.linkedlist** %4
br label %28
28: ; preds = %16, %12
%29 = load %struct.linkedlist*, %struct.linkedlist** %4
ret %struct.linkedlist* %29
}

LLVM IR (-O2):
%struct.linkedlist = type { i32 }
define %struct.linkedlist* @linkedlist_insert(i64 %0, i32 %1, %struct.linkedlist* %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = tail call %struct.linkedlist* @linkedlist_cons(i64 %0, %struct.linkedlist* %2)
br label %7
7: ; preds = %5, %9
%8 = phi %struct.linkedlist* [ %14, %9 ], [ %6, %5 ]
ret %struct.linkedlist* %8
9: ; preds = %3
%10 = tail call i64 @linkedlist_head(%struct.linkedlist* %2)
%11 = add i32 %1, -1
%12 = tail call %struct.linkedlist* @linkedlist_tail(%struct.linkedlist* %2)
%13 = tail call %struct.linkedlist* @linkedlist_insert(i64 %0, i32 %11, %struct.linkedlist* %12)
%14 = tail call %struct.linkedlist* @linkedlist_cons(i64 %10, %struct.linkedlist* %13)
br label %7
}