#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 */
typedef int /*<<< orphan*/ u_int8_t;

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ log_error(char *, unsigned long);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int ipsec_clone_id(u_int8_t **did, size_t *did_len, u_int8_t *id,
                   size_t id_len) {
  free(*did);
  if (!id_len || !id) {
    *did = 0;
    *did_len = 0;
    return 0;
  }
  *did = malloc(id_len);
  if (!*did) {
    *did_len = 0;
    log_error("ipsec_clone_id: malloc(%lu) failed", (unsigned long)id_len);
    return -1;
  }
  *did_len = id_len;
  memcpy(*did, id, id_len);
  return 0;
}


x86 assembly (clang, -O0):
<ipsec_clone_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
je     117a <ipsec_clone_id+0x3a>
cmpq   $0x0,-0x20(%rbp)
jne    119c <ipsec_clone_id+0x5c>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    120a <ipsec_clone_id+0xca>
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e4 <ipsec_clone_id+0xa4>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <ipsec_clone_id+0xca>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipsec_clone_id>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1030 <free@plt>
test   %r14,%r14
je     1189 <ipsec_clone_id+0x49>
test   %r12,%r12
je     1189 <ipsec_clone_id+0x49>
mov    %r12,%rdi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11a5 <ipsec_clone_id+0x65>
mov    %r12,(%r15)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
jmp    1197 <ipsec_clone_id+0x57>
movq   $0x0,(%rbx)
movq   $0x0,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movq   $0x0,(%r15)
lea    0xe4d(%rip),%rdi
mov    %r12,%rsi
call   1040 <log_error@plt>
mov    $0xffffffff,%eax
jmp    1199 <ipsec_clone_id+0x59>

LLVM IR (-O0):
@.str = [35 x i8] c"ipsec_clone_id: malloc(%lu) failed\00"
define i32 @ipsec_clone_id(i32** %0, i64* %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i64
store i32** %0, i32*** %6
store i64* %1, i64** %7
store i32* %2, i32** %8
store i64 %3, i64* %9
%10 = load i32**, i32*** %6
%11 = load i32*, i32** %10
%12 = call i32 @free(i32* %11)
%13 = load i64, i64* %9
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %15, %4
%19 = load i32**, i32*** %6
store i32* null, i32** %19
%20 = load i64*, i64** %7
store i64 0, i64* %20
store i32 0, i32* %5
br label %40
21: ; preds = %15
%22 = load i64, i64* %9
%23 = call i32* @malloc(i64 %22)
%24 = load i32**, i32*** %6
store i32* %23, i32** %24
%25 = load i32**, i32*** %6
%26 = load i32*, i32** %25
%27 = icmp ne i32* %26, null
br i1 %27, label %32, label %28
28: ; preds = %21
%29 = load i64*, i64** %7
store i64 0, i64* %29
%30 = load i64, i64* %9
%31 = call i32 @log_error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %30)
store i32 -1, i32* %5
br label %40
32: ; preds = %21
%33 = load i64, i64* %9
%34 = load i64*, i64** %7
store i64 %33, i64* %34
%35 = load i32**, i32*** %6
%36 = load i32*, i32** %35
%37 = load i32*, i32** %8
%38 = load i64, i64* %9
%39 = call i32 @memcpy(i32* %36, i32* %37, i64 %38)
store i32 0, i32* %5
br label %40
40: ; preds = %32, %28, %18
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
@.str = [35 x i8] c"ipsec_clone_id: malloc(%lu) failed\00"
define i32 @ipsec_clone_id(i32** nocapture %0, i64* nocapture writeonly %1, i32* %2, i64 %3) {
%5 = load i32*, i32** %0
%6 = tail call i32 @free(i32* %5)
%7 = icmp ne i64 %3, 0
%8 = icmp ne i32* %2, null
%9 = and i1 %8, %7
br i1 %9, label %11, label %10
10: ; preds = %4
store i32* null, i32** %0
store i64 0, i64* %1
br label %18
11: ; preds = %4
%12 = tail call i32* @malloc(i64 %3)
store i32* %12, i32** %0
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
store i64 0, i64* %1
%15 = tail call i32 @log_error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %3)
br label %18
16: ; preds = %11
store i64 %3, i64* %1
%17 = tail call i32 @memcpy(i32* %12, i32* %2, i64 %3)
br label %18
18: ; preds = %16, %14, %10
%19 = phi i32 [ 0, %16 ], [ -1, %14 ], [ 0, %10 ]
ret i32 %19
}