#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 {
  int ai_addrlen;
  int /*<<< orphan*/ *ai_next;
  scalar_t__ ai_canonname;
  struct sockaddr *ai_addr;
};
struct addrinfo {
  int ai_addrlen;
  int /*<<< orphan*/ *ai_next;
  scalar_t__ ai_canonname;
  struct addrinfo *ai_addr;
};

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ memcpy(struct sockaddr *, struct sockaddr *, int);
scalar_t__ strdup(scalar_t__);
struct addrinfo *addrinfo_clone(struct addrinfo *addr) {
  struct addrinfo *ai;
  ai = (struct addrinfo *)calloc(sizeof(struct addrinfo) + addr->ai_addrlen, 1);
  if (ai) {
    memcpy(ai, addr, sizeof(struct addrinfo));
    ai->ai_addr = (struct sockaddr *)(ai + 1);
    memcpy(ai->ai_addr, addr->ai_addr, addr->ai_addrlen);
    if (addr->ai_canonname) {
      ai->ai_canonname = strdup(addr->ai_canonname);
    }
    ai->ai_next = NULL;
  }
  return ai;
}


x86 assembly (clang, -O0):
<addrinfo_clone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movslq (%rax),%rax
add    $0x20,%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d2 <addrinfo_clone+0xa2>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x20,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rcx
add    $0x20,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x8(%rbp),%rax
mov    0x18(%rax),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     11c6 <addrinfo_clone+0x96>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addrinfo_clone>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%edi
add    $0x20,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     118f <addrinfo_clone+0x5f>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
add    $0x20,%rdi
mov    %rdi,0x18(%rbx)
mov    0x18(%r14),%rsi
mov    (%r14),%edx
call   1040 <memcpy@plt>
mov    0x10(%r14),%rdi
test   %rdi,%rdi
je     1187 <addrinfo_clone+0x57>
call   1050 <strdup@plt>
mov    %rax,0x10(%rbx)
movq   $0x0,0x8(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32*, i64, %struct.addrinfo* }
%struct.sockaddr = type { i32, i32*, i64, %struct.sockaddr* }
define %struct.addrinfo* @addrinfo_clone(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo*
store %struct.addrinfo* %0, %struct.addrinfo** %2
%4 = load %struct.addrinfo*, %struct.addrinfo** %2
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 0
%6 = load i32, i32* %5
%7 = sext i32 %6 to i64
%8 = add i64 32, %7
%9 = trunc i64 %8 to i32
%10 = call i64 @calloc(i32 %9, i32 1)
%11 = inttoptr i64 %10 to %struct.addrinfo*
store %struct.addrinfo* %11, %struct.addrinfo** %3
%12 = load %struct.addrinfo*, %struct.addrinfo** %3
%13 = icmp ne %struct.addrinfo* %12, null
br i1 %13, label %14, label %52
14: ; preds = %1
%15 = load %struct.addrinfo*, %struct.addrinfo** %3
%16 = bitcast %struct.addrinfo* %15 to %struct.sockaddr*
%17 = load %struct.addrinfo*, %struct.addrinfo** %2
%18 = bitcast %struct.addrinfo* %17 to %struct.sockaddr*
%19 = call i32 @memcpy(%struct.sockaddr* %16, %struct.sockaddr* %18, i32 32)
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 1
%22 = bitcast %struct.addrinfo* %21 to %struct.sockaddr*
%23 = bitcast %struct.sockaddr* %22 to %struct.addrinfo*
%24 = load %struct.addrinfo*, %struct.addrinfo** %3
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i32 0, i32 3
store %struct.addrinfo* %23, %struct.addrinfo** %25
%26 = load %struct.addrinfo*, %struct.addrinfo** %3
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 3
%28 = load %struct.addrinfo*, %struct.addrinfo** %27
%29 = bitcast %struct.addrinfo* %28 to %struct.sockaddr*
%30 = load %struct.addrinfo*, %struct.addrinfo** %2
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 3
%32 = load %struct.addrinfo*, %struct.addrinfo** %31
%33 = bitcast %struct.addrinfo* %32 to %struct.sockaddr*
%34 = load %struct.addrinfo*, %struct.addrinfo** %2
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @memcpy(%struct.sockaddr* %29, %struct.sockaddr* %33, i32 %36)
%38 = load %struct.addrinfo*, %struct.addrinfo** %2
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i64, i64* %39
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %14
%43 = load %struct.addrinfo*, %struct.addrinfo** %2
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i64, i64* %44
%46 = call i64 @strdup(i64 %45)
%47 = load %struct.addrinfo*, %struct.addrinfo** %3
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 2
store i64 %46, i64* %48
br label %49
49: ; preds = %42, %14
%50 = load %struct.addrinfo*, %struct.addrinfo** %3
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 1
store i32* null, i32** %51
br label %52
52: ; preds = %49, %1
%53 = load %struct.addrinfo*, %struct.addrinfo** %3
ret %struct.addrinfo* %53
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32*, i64, %struct.addrinfo* }
%struct.sockaddr = type { i32, i32*, i64, %struct.sockaddr* }
define %struct.addrinfo* @addrinfo_clone(%struct.addrinfo* %0) {
%2 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = add i32 %3, 32
%5 = tail call i64 @calloc(i32 %4, i32 1)
%6 = inttoptr i64 %5 to %struct.addrinfo*
%7 = icmp eq i64 %5, 0
br i1 %7, label %28, label %8
8: ; preds = %1
%9 = inttoptr i64 %5 to %struct.sockaddr*
%10 = bitcast %struct.addrinfo* %0 to %struct.sockaddr*
%11 = tail call i32 @memcpy(%struct.sockaddr* %9, %struct.sockaddr* %10, i32 32)
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 1
%13 = bitcast %struct.addrinfo* %12 to %struct.sockaddr*
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 3
store %struct.addrinfo* %12, %struct.addrinfo** %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 3
%16 = bitcast %struct.addrinfo** %15 to %struct.sockaddr**
%17 = load %struct.sockaddr*, %struct.sockaddr** %16
%18 = load i32, i32* %2
%19 = tail call i32 @memcpy(%struct.sockaddr* %13, %struct.sockaddr* %17, i32 %18)
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %8
%24 = tail call i64 @strdup(i64 %21)
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 2
store i64 %24, i64* %25
br label %26
26: ; preds = %23, %8
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 1
store i32* null, i32** %27
br label %28
28: ; preds = %26, %1
ret %struct.addrinfo* %6
}