#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_remove(unsigned i, struct linkedlist *l) {
  long new_head;
  struct linkedlist *rec;
  if (i == 0)
    return linkedlist_tail(l);
  new_head = linkedlist_head(l);
  rec = linkedlist_remove(i - 1, linkedlist_tail(l));
  return linkedlist_cons(new_head, rec);
}


x86 assembly (clang, -O0):
<linkedlist_remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    116b <linkedlist_remove+0x2b>
mov    -0x18(%rbp),%rdi
call   1040 <linkedlist_tail@plt>
mov    %rax,-0x8(%rbp)
jmp    11aa <linkedlist_remove+0x6a>
mov    -0x18(%rbp),%rdi
call   1050 <linkedlist_head@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <linkedlist_tail@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rsi
call   1060 <linkedlist_remove@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <linkedlist_cons@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linkedlist_remove>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   %edi,%edi
je     117e <linkedlist_remove+0x3e>
mov    %edi,%ebx
mov    %r14,%rdi
call   1050 <linkedlist_head@plt>
mov    %rax,%r15
add    $0xffffffff,%ebx
mov    %r14,%rdi
call   1040 <linkedlist_tail@plt>
mov    %ebx,%edi
mov    %rax,%rsi
call   1060 <linkedlist_remove@plt>
mov    %r15,%rdi
mov    %rax,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <linkedlist_cons@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <linkedlist_tail@plt>

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

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