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

/* Variables and functions */
size_t vectyploc_count(struct vectyploc *);
int /*<<< orphan*/ vectyploc_destroy(struct vectyploc *);
struct typ ***vectyploc_get(struct vectyploc *, int);
struct typ **vectyploc_pop(struct vectyploc *);
__attribute__((__warn_unused_result__)) ssize_t
vectyploc_remove_replace_with_last(struct vectyploc *v, ssize_t n) {
  const size_t count = vectyploc_count(v);
  if (count == 1) {
    vectyploc_destroy(v);
    return 0;
  }
  struct typ **last = vectyploc_pop(v);
  if (n + 1 == count) {
    (void)last;
    return 0;
  } else {
    struct typ ***loc = vectyploc_get(v, n);
    *loc = last;
    return -1;
  }
}


x86 assembly (clang, -O0):
<vectyploc_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 <vectyploc_count@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jne    117c <vectyploc_remove_replace_with_last+0x3c>
mov    -0x10(%rbp),%rdi
call   1060 <vectyploc_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <vectyploc_remove_replace_with_last+0x89>
mov    -0x10(%rbp),%rdi
call   1030 <vectyploc_pop@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
cmp    -0x20(%rbp),%rax
jne    11a7 <vectyploc_remove_replace_with_last+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <vectyploc_remove_replace_with_last+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <vectyploc_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):
<vectyploc_remove_replace_with_last>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r12
call   1040 <vectyploc_count@plt>
cmp    $0x1,%rax
jne    1165 <vectyploc_remove_replace_with_last+0x25>
mov    %r12,%rdi
call   1060 <vectyploc_destroy@plt>
xor    %eax,%eax
jmp    1194 <vectyploc_remove_replace_with_last+0x54>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <vectyploc_pop@plt>
mov    %rax,%r15
lea    0x1(%r14),%eax
movslq %eax,%rcx
xor    %eax,%eax
cmp    %rcx,%rbx
je     1194 <vectyploc_remove_replace_with_last+0x54>
mov    %r12,%rdi
mov    %r14d,%esi
call   1050 <vectyploc_get@plt>
mov    %r15,(%rax)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.vectyploc = type { i32 }
%struct.typ = type { i32 }
define i32 @vectyploc_remove_replace_with_last(%struct.vectyploc* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.vectyploc*
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.typ**
%8 = alloca %struct.typ***
store %struct.vectyploc* %0, %struct.vectyploc** %4
store i32 %1, i32* %5
%9 = load %struct.vectyploc*, %struct.vectyploc** %4
%10 = call i64 @vectyploc_count(%struct.vectyploc* %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.vectyploc*, %struct.vectyploc** %4
%15 = call i32 @vectyploc_destroy(%struct.vectyploc* %14)
store i32 0, i32* %3
br label %32
16: ; preds = %2
%17 = load %struct.vectyploc*, %struct.vectyploc** %4
%18 = call %struct.typ** @vectyploc_pop(%struct.vectyploc* %17)
store %struct.typ** %18, %struct.typ*** %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.typ**, %struct.typ*** %7
store i32 0, i32* %3
br label %32
26: ; preds = %16
%27 = load %struct.vectyploc*, %struct.vectyploc** %4
%28 = load i32, i32* %5
%29 = call %struct.typ*** @vectyploc_get(%struct.vectyploc* %27, i32 %28)
store %struct.typ*** %29, %struct.typ**** %8
%30 = load %struct.typ**, %struct.typ*** %7
%31 = load %struct.typ***, %struct.typ**** %8
store %struct.typ** %30, %struct.typ*** %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.vectyploc = type { i32 }
%struct.typ = type { i32 }
define i32 @vectyploc_remove_replace_with_last(%struct.vectyploc* %0, i32 %1) {
%3 = tail call i64 @vectyploc_count(%struct.vectyploc* %0)
%4 = icmp eq i64 %3, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @vectyploc_destroy(%struct.vectyploc* %0)
br label %14
7: ; preds = %2
%8 = tail call %struct.typ** @vectyploc_pop(%struct.vectyploc* %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.typ*** @vectyploc_get(%struct.vectyploc* %0, i32 %1)
store %struct.typ** %8, %struct.typ*** %13
br label %14
14: ; preds = %12, %7, %5
%15 = phi i32 [ 0, %5 ], [ -1, %12 ], [ 0, %7 ]
ret i32 %15
}