void *kmalloc_dup(void *src, int len, unsigned gfp) {
  char *ptr = kmalloc(len, gfp);
  if (ptr == ((void *)0)) {
    printk("out of memory in kmalloc_dup\n");
    return ((void *)0);
  }
  (__builtin_constant_p(len) ? __constant_memcpy((ptr), (src), (len))
                             : __memcpy((ptr), (src), (len)));
  return ptr;
}


x86 assembly (clang, -O0):
<kmalloc_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x0,%al
call   1050 <kmalloc@plt>
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117b <kmalloc_dup+0x4b>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
movq   $0x0,-0x8(%rbp)
jmp    1195 <kmalloc_dup+0x65>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1040 <__memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmalloc_dup>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
mov    %edx,%esi
xor    %eax,%eax
call   1050 <kmalloc@plt>
test   %eax,%eax
je     115c <kmalloc_dup+0x2c>
movslq %eax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <__memcpy@plt>
jmp    116c <kmalloc_dup+0x3c>
lea    0xe9d(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printk@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"out of memory in kmalloc_dup\0A\00"
define i8* @kmalloc_dup(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %6
%10 = load i32, i32* %7
%11 = call i32 (i32, i32, ...) bitcast (i32 (...)* @kmalloc to i32 (i32, i32, ...)*)(i32 %9, i32 %10)
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 (i8*, ...) bitcast (i32 (...)* @printk to i32 (i8*, ...)*)(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %34
18: ; preds = %3
%19 = load i32, i32* %6
%20 = call i1 @llvm.is.constant.i32(i32 %19)
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8*, i8** %8
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @__constant_memcpy to i32 (i8*, i8*, i32, ...)*)(i8* %22, i8* %23, i32 %24)
br label %31
26: ; preds = %18
%27 = load i8*, i8** %8
%28 = load i8*, i8** %5
%29 = load i32, i32* %6
%30 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @__memcpy to i32 (i8*, i8*, i32, ...)*)(i8* %27, i8* %28, i32 %29)
br label %31
31: ; preds = %26, %21
%32 = phi i32 [ %25, %21 ], [ %30, %26 ]
%33 = load i8*, i8** %8
store i8* %33, i8** %4
br label %34
34: ; preds = %31, %16
%35 = load i8*, i8** %4
ret i8* %35
}

LLVM IR (-O2):
@.str = [30 x i8] c"out of memory in kmalloc_dup\0A\00"
define i8* @kmalloc_dup(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @kmalloc to i32 (i32, i32, ...)*)(i32 %1, i32 %2)
%5 = sext i32 %4 to i64
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i32 %4, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i8*, ...) bitcast (i32 (...)* @printk to i32 (i8*, ...)*)(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %3
%11 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @__memcpy to i32 (i8*, i8*, i32, ...)*)(i8* %6, i8* %0, i32 %1)
br label %12
12: ; preds = %10, %8
%13 = phi i8* [ null, %8 ], [ %6, %10 ]
ret i8* %13
}