#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*/ ENOMEM;
int /*<<< orphan*/ errno;
void *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
extern void *vec_dup(void *vec) {
  void *dup = malloc((sizeof(size_t) * 3) +
                     (*(size_t *)(((char *)vec - (sizeof(size_t) * 3)))) *
                         (*(size_t *)(((char *)vec - (sizeof(size_t) * 3)) +
                                      sizeof(size_t) * 2)));
  if (dup == NULL) {
    errno = ENOMEM;
    return NULL;
  }
  dup = (char *)dup + (sizeof(size_t) * 3);
  memcpy(((char *)dup - (sizeof(size_t) * 3)),
         ((char *)vec - (sizeof(size_t) * 3)),
         ((sizeof(size_t) * 3) +
          (*(size_t *)(((char *)vec - (sizeof(size_t) * 3)))) *
              (*(size_t *)(((char *)vec - (sizeof(size_t) * 3)) +
                           sizeof(size_t) * 2))));
  return dup;
}


x86 assembly (clang, -O0):
<vec_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rax),%rax
mov    -0x10(%rbp),%rcx
imul   -0x8(%rcx),%rax
add    $0x18,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1176 <vec_dup+0x56>
mov    0x2e82(%rip),%rax
mov    (%rax),%ecx
mov    0x2e71(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11b6 <vec_dup+0x96>
mov    -0x18(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0xffffffffffffffe8,%rdi
mov    -0x10(%rbp),%rsi
add    $0xffffffffffffffe8,%rsi
mov    -0x10(%rbp),%rax
mov    -0x18(%rax),%rax
mov    -0x10(%rbp),%rcx
imul   -0x8(%rcx),%rax
add    $0x18,%rax
mov    %eax,%edx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vec_dup>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    -0x8(%rdi),%ebp
imul   -0x18(%rdi),%ebp
add    $0x18,%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1157 <vec_dup+0x37>
add    $0xffffffffffffffe8,%rbx
mov    %rax,%r14
add    $0x18,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
jmp    116c <vec_dup+0x4c>
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    0x2e71(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @vec_dup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i64 -24
%7 = bitcast i8* %6 to i64*
%8 = load i64, i64* %7
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 -24
%11 = getelementptr inbounds i8, i8* %10, i64 16
%12 = bitcast i8* %11 to i64*
%13 = load i64, i64* %12
%14 = mul i64 %8, %13
%15 = add i64 24, %14
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = load i32, i32* @ENOMEM
store i32 %21, i32* @errno
store i8* null, i8** %2
br label %43
22: ; preds = %1
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 24
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 -24
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 -24
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 -24
%31 = bitcast i8* %30 to i64*
%32 = load i64, i64* %31
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 -24
%35 = getelementptr inbounds i8, i8* %34, i64 16
%36 = bitcast i8* %35 to i64*
%37 = load i64, i64* %36
%38 = mul i64 %32, %37
%39 = add i64 24, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @memcpy(i8* %26, i8* %28, i32 %40)
%42 = load i8*, i8** %4
store i8* %42, i8** %2
br label %43
43: ; preds = %22, %20
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @vec_dup(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 -24
%3 = bitcast i8* %2 to i64*
%4 = load i64, i64* %3
%5 = getelementptr inbounds i8, i8* %0, i64 -8
%6 = bitcast i8* %5 to i64*
%7 = load i64, i64* %6
%8 = mul i64 %7, %4
%9 = trunc i64 %8 to i32
%10 = add i32 %9, 24
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* @ENOMEM
store i32 %14, i32* @errno
br label %18
15: ; preds = %1
%16 = getelementptr inbounds i8, i8* %11, i64 24
%17 = tail call i32 @memcpy(i8* %11, i8* %2, i32 %10)
br label %18
18: ; preds = %15, %13
%19 = phi i8* [ null, %13 ], [ %16, %15 ]
ret i8* %19
}