#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 */
struct zip_error {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ _zip_error_set(struct zip_error *, int, int /*<<< orphan*/);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void const *, size_t);
void *_zip_memdup(const void *mem, size_t len, struct zip_error *error) {
  void *ret;
  if (len == 0)
    return NULL;
  ret = malloc(len);
  if (!ret) {
    _zip_error_set(error, 14, 0);
    return NULL;
  }
  memcpy(ret, mem, len);
  return ret;
}


x86 assembly (clang, -O0):
<_zip_memdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115c <_zip_memdup+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11aa <_zip_memdup+0x7a>
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1191 <_zip_memdup+0x61>
mov    -0x20(%rbp),%rdi
mov    $0xe,%esi
xor    %edx,%edx
call   1050 <_zip_error_set@plt>
movq   $0x0,-0x8(%rbp)
jmp    11aa <_zip_memdup+0x7a>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_zip_memdup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1166 <_zip_memdup+0x36>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     116b <_zip_memdup+0x3b>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
jmp    117d <_zip_memdup+0x4d>
xor    %r14d,%r14d
jmp    117d <_zip_memdup+0x4d>
xor    %r14d,%r14d
mov    %r15,%rdi
mov    $0xe,%esi
xor    %edx,%edx
call   1050 <_zip_error_set@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.zip_error = type { i32 }
define i8* @_zip_memdup(i8* %0, i64 %1, %struct.zip_error* %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = tail call i8* @malloc(i64 %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @_zip_error_set(%struct.zip_error* %2, i32 14, i32 0)
br label %12
10: ; preds = %5
%11 = tail call i32 @memcpy(i8* %6, i8* %0, i64 %1)
br label %12
12: ; preds = %3, %10, %8
%13 = phi i8* [ %6, %10 ], [ null, %8 ], [ null, %3 ]
ret i8* %13
}