#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 sockaddr_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ socklen_t;

/* Variables and functions */
void *calloc(int, int);
struct sockaddr *memcpy(void *, struct sockaddr *, scalar_t__);
scalar_t__ sockaddr_len(struct sockaddr *);
struct sockaddr *sockaddr_dup(struct sockaddr *addr) {
  socklen_t addrlen = sockaddr_len(addr);
  if (addrlen == 0)
    return NULL;
  void *dst = calloc(sizeof(struct sockaddr_storage), 1);
  return memcpy(dst, addr, addrlen);
}


x86 assembly (clang, -O0):
<sockaddr_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <sockaddr_len@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1161 <sockaddr_dup+0x31>
movq   $0x0,-0x8(%rbp)
jmp    1189 <sockaddr_dup+0x59>
mov    $0x4,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_dup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <sockaddr_len@plt>
test   %rax,%rax
je     1168 <sockaddr_dup+0x38>
mov    %rax,%rbx
mov    $0x4,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <memcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
define %struct.sockaddr* @sockaddr_dup(%struct.sockaddr* %0) {
%2 = alloca %struct.sockaddr*
%3 = alloca %struct.sockaddr*
%4 = alloca i64
%5 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %3
%6 = load %struct.sockaddr*, %struct.sockaddr** %3
%7 = call i64 @sockaddr_len(%struct.sockaddr* %6)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store %struct.sockaddr* null, %struct.sockaddr** %2
br label %17
11: ; preds = %1
%12 = call i8* @calloc(i32 4, i32 1)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = load %struct.sockaddr*, %struct.sockaddr** %3
%15 = load i64, i64* %4
%16 = call %struct.sockaddr* @memcpy(i8* %13, %struct.sockaddr* %14, i64 %15)
store %struct.sockaddr* %16, %struct.sockaddr** %2
br label %17
17: ; preds = %11, %10
%18 = load %struct.sockaddr*, %struct.sockaddr** %2
ret %struct.sockaddr* %18
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define %struct.sockaddr* @sockaddr_dup(%struct.sockaddr* %0) {
%2 = tail call i64 @sockaddr_len(%struct.sockaddr* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(4) i8* @calloc(i32 4, i32 1)
%6 = tail call %struct.sockaddr* @memcpy(i8* %5, %struct.sockaddr* %0, i64 %2)
br label %7
7: ; preds = %1, %4
%8 = phi %struct.sockaddr* [ %6, %4 ], [ null, %1 ]
ret %struct.sockaddr* %8
}