#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 /*<<< orphan*/ assert(int);
int first(int);
int pair(int, int);
int rest(int);
int replace(int list, int i, int value) {
  int retval;
  if (i > 0)
    retval = pair(first(list), replace(rest(list), i - 1, value));
  else {
    assert(i == 0);
    retval = pair(value, rest(list));
  };
  return retval;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11a0 <replace+0x50>
mov    -0x4(%rbp),%edi
call   1070 <first@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <rest@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%esi
sub    $0x1,%esi
mov    -0xc(%rbp),%edx
call   1060 <replace@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%esi
call   1040 <pair@plt>
mov    %eax,-0x10(%rbp)
jmp    11cc <replace+0x7c>
cmpl   $0x0,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <rest@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%esi
call   1040 <pair@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %edi,%ebx
test   %esi,%esi
jle    1187 <replace+0x37>
mov    %esi,%ebp
mov    %ebx,%edi
call   1070 <first@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <rest@plt>
add    $0xffffffff,%ebp
mov    %eax,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1060 <replace@plt>
mov    %r15d,%edi
jmp    119c <replace+0x4c>
sete   %al
movzbl %al,%edi
call   1050 <assert@plt>
mov    %ebx,%edi
call   1030 <rest@plt>
mov    %r14d,%edi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <pair@plt>

LLVM IR (-O0):
define i32 @replace(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32, i32* %5
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %3
%11 = load i32, i32* %4
%12 = call i32 @first(i32 %11)
%13 = load i32, i32* %4
%14 = call i32 @rest(i32 %13)
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = load i32, i32* %6
%18 = call i32 @replace(i32 %14, i32 %16, i32 %17)
%19 = call i32 @pair(i32 %12, i32 %18)
store i32 %19, i32* %7
br label %29
20: ; preds = %3
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* %6
%26 = load i32, i32* %4
%27 = call i32 @rest(i32 %26)
%28 = call i32 @pair(i32 %25, i32 %27)
store i32 %28, i32* %7
br label %29
29: ; preds = %20, %10
%30 = load i32, i32* %7
ret i32 %30
}

LLVM IR (-O2):
define i32 @replace(i32 %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %7, label %13
5: ; preds = %13, %7
%6 = phi i32 [ %12, %7 ], [ %18, %13 ]
ret i32 %6
7: ; preds = %3
%8 = tail call i32 @first(i32 %0)
%9 = tail call i32 @rest(i32 %0)
%10 = add nsw i32 %1, -1
%11 = tail call i32 @replace(i32 %9, i32 %10, i32 %2)
%12 = tail call i32 @pair(i32 %8, i32 %11)
br label %5
13: ; preds = %3
%14 = icmp eq i32 %1, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = tail call i32 @rest(i32 %0)
%18 = tail call i32 @pair(i32 %2, i32 %17)
br label %5
}