#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 vecnode {
  int dummy;
};
struct node {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
size_t vecnode_count(struct vecnode *);
int /*<<< orphan*/ vecnode_destroy(struct vecnode *);
struct node **vecnode_get(struct vecnode *, int);
struct node *vecnode_pop(struct vecnode *);
__attribute__((__warn_unused_result__)) ssize_t
vecnode_remove_replace_with_last(struct vecnode *v, ssize_t n) {
  const size_t count = vecnode_count(v);
  if (count == 1) {
    vecnode_destroy(v);
    return 0;
  }
  struct node *last = vecnode_pop(v);
  if (n + 1 == count) {
    (void)last;
    return 0;
  } else {
    struct node **loc = vecnode_get(v, n);
    *loc = last;
    return -1;
  }
}


x86 assembly (clang, -O0):
<vecnode_remove_replace_with_last>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <vecnode_count@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jne    117c <vecnode_remove_replace_with_last+0x3c>
mov    -0x10(%rbp),%rdi
call   1030 <vecnode_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <vecnode_remove_replace_with_last+0x89>
mov    -0x10(%rbp),%rdi
call   1050 <vecnode_pop@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
cmp    -0x20(%rbp),%rax
jne    11a7 <vecnode_remove_replace_with_last+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <vecnode_remove_replace_with_last+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <vecnode_get@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vecnode_remove_replace_with_last>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r12
call   1040 <vecnode_count@plt>
cmp    $0x1,%rax
jne    1165 <vecnode_remove_replace_with_last+0x25>
mov    %r12,%rdi
call   1030 <vecnode_destroy@plt>
xor    %eax,%eax
jmp    1194 <vecnode_remove_replace_with_last+0x54>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <vecnode_pop@plt>
mov    %rax,%r15
lea    0x1(%r14),%eax
movslq %eax,%rcx
xor    %eax,%eax
cmp    %rcx,%rbx
je     1194 <vecnode_remove_replace_with_last+0x54>
mov    %r12,%rdi
mov    %r14d,%esi
call   1060 <vecnode_get@plt>
mov    %r15,(%rax)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.vecnode = type { i32 }
%struct.node = type { i32 }
define i32 @vecnode_remove_replace_with_last(%struct.vecnode* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.vecnode*
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.node*
%8 = alloca %struct.node**
store %struct.vecnode* %0, %struct.vecnode** %4
store i32 %1, i32* %5
%9 = load %struct.vecnode*, %struct.vecnode** %4
%10 = call i64 @vecnode_count(%struct.vecnode* %9)
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp eq i64 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load %struct.vecnode*, %struct.vecnode** %4
%15 = call i32 @vecnode_destroy(%struct.vecnode* %14)
store i32 0, i32* %3
br label %32
16: ; preds = %2
%17 = load %struct.vecnode*, %struct.vecnode** %4
%18 = call %struct.node* @vecnode_pop(%struct.vecnode* %17)
store %struct.node* %18, %struct.node** %7
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = load i64, i64* %6
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = load %struct.node*, %struct.node** %7
store i32 0, i32* %3
br label %32
26: ; preds = %16
%27 = load %struct.vecnode*, %struct.vecnode** %4
%28 = load i32, i32* %5
%29 = call %struct.node** @vecnode_get(%struct.vecnode* %27, i32 %28)
store %struct.node** %29, %struct.node*** %8
%30 = load %struct.node*, %struct.node** %7
%31 = load %struct.node**, %struct.node*** %8
store %struct.node* %30, %struct.node** %31
store i32 -1, i32* %3
br label %32
32: ; preds = %26, %24, %13
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.vecnode = type { i32 }
%struct.node = type { i32 }
define i32 @vecnode_remove_replace_with_last(%struct.vecnode* %0, i32 %1) {
%3 = tail call i64 @vecnode_count(%struct.vecnode* %0)
%4 = icmp eq i64 %3, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @vecnode_destroy(%struct.vecnode* %0)
br label %14
7: ; preds = %2
%8 = tail call %struct.node* @vecnode_pop(%struct.vecnode* %0)
%9 = add nsw i32 %1, 1
%10 = sext i32 %9 to i64
%11 = icmp eq i64 %3, %10
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = tail call %struct.node** @vecnode_get(%struct.vecnode* %0, i32 %1)
store %struct.node* %8, %struct.node** %13
br label %14
14: ; preds = %12, %7, %5
%15 = phi i32 [ 0, %5 ], [ -1, %12 ], [ 0, %7 ]
ret i32 %15
}