struct elf_file_t *elf_file_create_from_buffer(void *ptr, int size,
                                               char *name) {
  struct elf_file_t *elf_file;
  void *ptr_copy;
  ptr_copy = malloc(size);
  if (!ptr_copy)
    fatal("%s: out of memory", __FUNCTION__);
  memcpy(ptr_copy, ptr, size);
  elf_file = elf_file_create_from_allocated_buffer(ptr_copy, size, name);
  return elf_file;
}


x86 assembly (clang, -O0):
<elf_file_create_from_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0xc(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1180 <elf_file_create_from_buffer+0x40>
lea    0xe8e(%rip),%rdi
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1030 <elf_file_create_from_allocated_buffer@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elf_file_create_from_buffer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%r12
movslq %esi,%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1176 <elf_file_create_from_buffer+0x36>
lea    0xe98(%rip),%rdi
lea    0xea3(%rip),%rsi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <elf_file_create_from_allocated_buffer@plt>
cltq
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.elf_file_t = type opaque
@.str = [18 x i8] c"%s: out of memory\00"
@__FUNCTION__.elf_file_create_from_buffer = [28 x i8] c"elf_file_create_from_buffer\00"
define %struct.elf_file_t* @elf_file_create_from_buffer(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.elf_file_t*
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @__FUNCTION__.elf_file_create_from_buffer, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i8*, i8** %8
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = load i8*, i8** %8
%22 = load i32, i32* %5
%23 = load i8*, i8** %6
%24 = call i32 (i8*, i32, i8*, ...) bitcast (i32 (...)* @elf_file_create_from_allocated_buffer to i32 (i8*, i32, i8*, ...)*)(i8* %21, i32 %22, i8* %23)
%25 = sext i32 %24 to i64
%26 = inttoptr i64 %25 to %struct.elf_file_t*
store %struct.elf_file_t* %26, %struct.elf_file_t** %7
%27 = load %struct.elf_file_t*, %struct.elf_file_t** %7
ret %struct.elf_file_t* %27
}

LLVM IR (-O2):
%struct.elf_file_t = type opaque
@.str = [18 x i8] c"%s: out of memory\00"
@__FUNCTION__.elf_file_create_from_buffer = [28 x i8] c"elf_file_create_from_buffer\00"
define %struct.elf_file_t* @elf_file_create_from_buffer(i8* nocapture %0, i32 %1, i8* %2) {
%4 = sext i32 %1 to i64
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @fatal to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @__FUNCTION__.elf_file_create_from_buffer, i64 0, i64 0))
br label %9
9: ; preds = %7, %3
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %5, i8* align 1 %0, i64 %4, i1 false)
%10 = tail call i32 (i8*, i32, i8*, ...) bitcast (i32 (...)* @elf_file_create_from_allocated_buffer to i32 (i8*, i32, i8*, ...)*)(i8* %5, i32 %1, i8* %2)
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to %struct.elf_file_t*
ret %struct.elf_file_t* %12
}