#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 int);
int /*<<< orphan*/ memmove(void *, void *, unsigned int);
int snmp_clone_mem(void **dstPtr, void *srcPtr, unsigned len) {
  *dstPtr = 0;
  if (srcPtr) {
    *dstPtr = malloc(len + 1);
    if (!*dstPtr) {
      return 1;
    }
    memmove(*dstPtr, srcPtr, len);
    ((char *)*dstPtr)[len] = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<snmp_clone_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     1199 <snmp_clone_mem+0x79>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1178 <snmp_clone_mem+0x58>
movl   $0x1,-0x4(%rbp)
jmp    11a0 <snmp_clone_mem+0x80>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<snmp_clone_mem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%rdi)
xor    %r14d,%r14d
test   %rsi,%rsi
je     1172 <snmp_clone_mem+0x52>
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x1(%r15),%edi
call   1030 <malloc@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     116c <snmp_clone_mem+0x4c>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <memmove@plt>
mov    0x0(%rbp),%rax
mov    %r15d,%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    1172 <snmp_clone_mem+0x52>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @snmp_clone_mem(i8** nocapture %0, i8* %1, i32 %2) {
store i8* null, i8** %0
%4 = icmp eq i8* %1, null
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = add i32 %2, 1
%7 = tail call i8* @malloc(i32 %6)
store i8* %7, i8** %0
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call i32 @memmove(i8* %7, i8* %1, i32 %2)
%11 = load i8*, i8** %0
%12 = zext i32 %2 to i64
%13 = getelementptr inbounds i8, i8* %11, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %3, %9, %5
%15 = phi i32 [ 1, %5 ], [ 0, %9 ], [ 0, %3 ]
ret i32 %15
}