#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 */
void *malloc(unsigned long);
int /*<<< orphan*/ memcpy(void *, void *, unsigned long);
void *sea_platform_buffer_construct_with_buffer_no_copy(void *, unsigned long);
void *sea_platform_buffer_construct_with_buffer(void *data,
                                                unsigned long size) {
  void *copy = malloc(size);
  if (copy) {
    memcpy(copy, data, size);
    return sea_platform_buffer_construct_with_buffer_no_copy(copy, size);
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<sea_platform_buffer_construct_with_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117f <sea_platform_buffer_construct_with_buffer+0x4f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <sea_platform_buffer_construct_with_buffer_no_copy@plt>
mov    %rax,-0x8(%rbp)
jmp    1187 <sea_platform_buffer_construct_with_buffer+0x57>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sea_platform_buffer_construct_with_buffer>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1169 <sea_platform_buffer_construct_with_buffer+0x39>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <sea_platform_buffer_construct_with_buffer_no_copy@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @sea_platform_buffer_construct_with_buffer(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = call i8* @malloc(i64 %7)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i8*, i8** %6
%13 = load i8*, i8** %4
%14 = load i64, i64* %5
%15 = call i32 @memcpy(i8* %12, i8* %13, i64 %14)
%16 = load i8*, i8** %6
%17 = load i64, i64* %5
%18 = call i8* @sea_platform_buffer_construct_with_buffer_no_copy(i8* %16, i64 %17)
store i8* %18, i8** %3
br label %20
19: ; preds = %2
store i8* null, i8** %3
br label %20
20: ; preds = %19, %11
%21 = load i8*, i8** %3
ret i8* %21
}

LLVM IR (-O2):
define i8* @sea_platform_buffer_construct_with_buffer(i8* %0, i64 %1) {
%3 = tail call i8* @malloc(i64 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @memcpy(i8* %3, i8* %0, i64 %1)
%7 = tail call i8* @sea_platform_buffer_construct_with_buffer_no_copy(i8* %3, i64 %1)
br label %8
8: ; preds = %2, %5
%9 = phi i8* [ %7, %5 ], [ null, %2 ]
ret i8* %9
}