#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 */
typedef struct TYPE_6__ TYPE_1__;

/* Type definitions */
struct TYPE_6__ {
  int /*<<< orphan*/ max;
  int /*<<< orphan*/ length;
  int /*<<< orphan*/ data;
};
typedef TYPE_1__ BUF_MEM;

/* Variables and functions */
TYPE_1__ *BUF_MEM_create(int /*<<< orphan*/);
int /*<<< orphan*/ BUF_MEM_free(TYPE_1__ *);
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
BUF_MEM *BUF_MEM_dup(const BUF_MEM *in) {
  BUF_MEM *out = NULL;
  if (!in)
    return NULL;
  out = BUF_MEM_create(in->length);
  if (!(out)) {
    fprintf(
        stderr,
        "[ERROR] (%s:%d ) "
        "Failed to allocate memory"
        "\n",
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/d0/dotfiles/refs/heads/master/src/misc.c",
        162);
    ERR_print_errors_fp(stderr);
    goto err;
  };
  memcpy(out->data, in->data, in->length);
  out->max = in->max;
  return out;
err:
  if (out)
    BUF_MEM_free(out);
  return NULL;
}


x86 assembly (clang, -O0):
<BUF_MEM_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117c <BUF_MEM_dup+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    121a <BUF_MEM_dup+0xca>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <BUF_MEM_create@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cb <BUF_MEM_dup+0x7b>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
lea    0xe7e(%rip),%rdx
mov    $0xa2,%ecx
call   1040 <fprintf@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1070 <ERR_print_errors_fp@plt>
jmp    11fe <BUF_MEM_dup+0xae>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121a <BUF_MEM_dup+0xca>
cmpq   $0x0,-0x18(%rbp)
je     1212 <BUF_MEM_dup+0xc2>
mov    -0x18(%rbp),%rdi
call   1060 <BUF_MEM_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BUF_MEM_dup>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1199 <BUF_MEM_dup+0x59>
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
call   1030 <BUF_MEM_create@plt>
test   %rax,%rax
je     1171 <BUF_MEM_dup+0x31>
mov    %rax,%r14
mov    0x8(%rax),%edi
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
call   1050 <memcpy@plt>
mov    (%rbx),%eax
mov    %eax,(%r14)
jmp    119c <BUF_MEM_dup+0x5c>
mov    0x2e68(%rip),%rbx
mov    (%rbx),%edi
lea    0xe7f(%rip),%rsi
lea    0xea4(%rip),%rdx
mov    $0xa2,%ecx
call   1040 <fprintf@plt>
mov    (%rbx),%edi
call   1060 <ERR_print_errors_fp@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TYPE_6__ = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [44 x i8] c"[ERROR] (%s:%d ) Failed to allocate memory\0A\00"
@.str.1 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/d0/dotfiles/refs/heads/master/src/misc.c\00"
define %struct.TYPE_6__* @BUF_MEM_dup(%struct.TYPE_6__* %0) {
%2 = alloca %struct.TYPE_6__*
%3 = alloca %struct.TYPE_6__*
%4 = alloca %struct.TYPE_6__*
store %struct.TYPE_6__* %0, %struct.TYPE_6__** %3
store %struct.TYPE_6__* null, %struct.TYPE_6__** %4
%5 = load %struct.TYPE_6__*, %struct.TYPE_6__** %3
%6 = icmp ne %struct.TYPE_6__* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store %struct.TYPE_6__* null, %struct.TYPE_6__** %2
br label %44
8: ; preds = %1
%9 = load %struct.TYPE_6__*, %struct.TYPE_6__** %3
%10 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %9, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call %struct.TYPE_6__* @BUF_MEM_create(i32 %11)
store %struct.TYPE_6__* %12, %struct.TYPE_6__** %4
%13 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%14 = icmp ne %struct.TYPE_6__* %13, null
br i1 %14, label %20, label %15
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.1, i64 0, i64 0), i32 162)
%18 = load i32, i32* @stderr
%19 = call i32 @ERR_print_errors_fp(i32 %18)
br label %37
20: ; preds = %8
%21 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%22 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %21, i32 0, i32 2
%23 = load i32, i32* %22
%24 = load %struct.TYPE_6__*, %struct.TYPE_6__** %3
%25 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %24, i32 0, i32 2
%26 = load i32, i32* %25
%27 = load %struct.TYPE_6__*, %struct.TYPE_6__** %3
%28 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = call i32 @memcpy(i32 %23, i32 %26, i32 %29)
%31 = load %struct.TYPE_6__*, %struct.TYPE_6__** %3
%32 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%35 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
store %struct.TYPE_6__* %36, %struct.TYPE_6__** %2
br label %44
37: ; preds = %15
%38 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%39 = icmp ne %struct.TYPE_6__* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%42 = call i32 @BUF_MEM_free(%struct.TYPE_6__* %41)
br label %43
43: ; preds = %40, %37
store %struct.TYPE_6__* null, %struct.TYPE_6__** %2
br label %44
44: ; preds = %43, %20, %7
%45 = load %struct.TYPE_6__*, %struct.TYPE_6__** %2
ret %struct.TYPE_6__* %45
}

LLVM IR (-O2):
%struct.TYPE_6__ = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [44 x i8] c"[ERROR] (%s:%d ) Failed to allocate memory\0A\00"
@.str.1 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/d0/dotfiles/refs/heads/master/src/misc.c\00"
define %struct.TYPE_6__* @BUF_MEM_dup(%struct.TYPE_6__* %0) {
%2 = icmp eq %struct.TYPE_6__* %0, null
br i1 %2, label %23, label %3
3: ; preds = %1
%4 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %0, i64 0, i32 1
%5 = load i32, i32* %4
%6 = tail call %struct.TYPE_6__* @BUF_MEM_create(i32 %5)
%7 = icmp eq %struct.TYPE_6__* %6, null
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.1, i64 0, i64 0), i32 162)
%11 = load i32, i32* @stderr
%12 = tail call i32 @ERR_print_errors_fp(i32 %11)
br label %23
13: ; preds = %3
%14 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %6, i64 0, i32 2
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %0, i64 0, i32 2
%17 = load i32, i32* %16
%18 = load i32, i32* %4
%19 = tail call i32 @memcpy(i32 %15, i32 %17, i32 %18)
%20 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %0, i64 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %6, i64 0, i32 0
store i32 %21, i32* %22
br label %23
23: ; preds = %1, %8, %13
%24 = phi %struct.TYPE_6__* [ %6, %13 ], [ null, %8 ], [ null, %1 ]
ret %struct.TYPE_6__* %24
}