#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(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *bit_array_dup(void *array) {
  if (array) {
    size_t *size = (size_t *)array;
    size_t bsize = ((*size + 7) >> 3) + sizeof(*size);
    void *ret = malloc(bsize);
    if (ret)
      memcpy(ret, array, bsize);
    return ret;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<bit_array_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118c <bit_array_dup+0x6c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
add    $0x7,%rax
shr    $0x3,%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     117f <bit_array_dup+0x5f>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1194 <bit_array_dup+0x74>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_array_dup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     115c <bit_array_dup+0x3c>
mov    %rdi,%r14
mov    (%rdi),%rbx
add    $0x7,%rbx
shr    $0x3,%rbx
add    $0x8,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     115c <bit_array_dup+0x3c>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
jmp    115f <bit_array_dup+0x3f>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @bit_array_dup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %28
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = bitcast i8* %10 to i64*
store i64* %11, i64** %4
%12 = load i64*, i64** %4
%13 = load i64, i64* %12
%14 = add i64 %13, 7
%15 = lshr i64 %14, 3
%16 = add i64 %15, 8
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %26
21: ; preds = %9
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = load i64, i64* %5
%25 = call i32 @memcpy(i8* %22, i8* %23, i64 %24)
br label %26
26: ; preds = %21, %9
%27 = load i8*, i8** %6
store i8* %27, i8** %2
br label %29
28: ; preds = %1
store i8* null, i8** %2
br label %29
29: ; preds = %28, %26
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
define i8* @bit_array_dup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = bitcast i8* %0 to i64*
%5 = load i64, i64* %4
%6 = add i64 %5, 7
%7 = lshr i64 %6, 3
%8 = add nuw nsw i64 %7, 8
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 @memcpy(i8* %9, i8* %0, i64 %8)
br label %13
13: ; preds = %1, %3, %11
%14 = phi i8* [ %9, %11 ], [ null, %3 ], [ null, %1 ]
ret i8* %14
}