#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 sa_family;
};
struct addrinfo {
  size_t ai_addrlen;
  int ai_family;
  struct sockaddr *ai_addr;
};

/* Variables and functions */
int AF_INET6;
scalar_t__ calloc(int, size_t);
int /*<<< orphan*/ free(struct addrinfo *);
struct addrinfo *_allocaddrinfo(int family) {
  struct addrinfo *ret;
  size_t addrlen = ((family == AF_INET6) ? sizeof(struct sockaddr_storage)
                                         : sizeof(struct sockaddr_storage));
  ret = (struct addrinfo *)calloc(1, sizeof(*ret));
  if (ret == NULL) {
    return NULL;
  }
  ret->ai_addr = (struct sockaddr *)calloc(1, addrlen);
  if (ret->ai_addr == NULL) {
    free(ret);
    return NULL;
  }
  ret->ai_addrlen = addrlen;
  ret->ai_family = family;
  ret->ai_addr->sa_family = family;
  return ret;
}


x86 assembly (clang, -O0):
<_allocaddrinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    0x2eb3(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%eax
cmp    %edx,%ecx
cmove  %rax,%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
mov    $0x18,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <_allocaddrinfo+0x51>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <_allocaddrinfo+0xb9>
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x10(%rax)
jne    11af <_allocaddrinfo+0x8f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <_allocaddrinfo+0xb9>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_allocaddrinfo>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x18,%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
test   %rax,%rax
je     116a <_allocaddrinfo+0x4a>
mov    %rax,%rbx
mov    $0x4,%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,0x10(%rbx)
test   %rax,%rax
je     1162 <_allocaddrinfo+0x42>
movq   $0x4,(%rbx)
mov    %ebp,0x8(%rbx)
mov    %ebp,(%rax)
jmp    116c <_allocaddrinfo+0x4c>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, %struct.sockaddr* }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
define %struct.addrinfo* @_allocaddrinfo(i32 %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = alloca %struct.addrinfo*
%5 = alloca i64
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* @AF_INET6
%8 = icmp eq i32 %6, %7
%9 = zext i1 %8 to i64
%10 = select i1 %8, i64 4, i64 4
store i64 %10, i64* %5
%11 = call i64 @calloc(i32 1, i64 24)
%12 = inttoptr i64 %11 to %struct.addrinfo*
store %struct.addrinfo* %12, %struct.addrinfo** %4
%13 = load %struct.addrinfo*, %struct.addrinfo** %4
%14 = icmp eq %struct.addrinfo* %13, null
br i1 %14, label %15, label %16
15: ; preds = %1
store %struct.addrinfo* null, %struct.addrinfo** %2
br label %42
16: ; preds = %1
%17 = load i64, i64* %5
%18 = call i64 @calloc(i32 1, i64 %17)
%19 = inttoptr i64 %18 to %struct.sockaddr*
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 2
store %struct.sockaddr* %19, %struct.sockaddr** %21
%22 = load %struct.addrinfo*, %struct.addrinfo** %4
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 2
%24 = load %struct.sockaddr*, %struct.sockaddr** %23
%25 = icmp eq %struct.sockaddr* %24, null
br i1 %25, label %26, label %29
26: ; preds = %16
%27 = load %struct.addrinfo*, %struct.addrinfo** %4
%28 = call i32 @free(%struct.addrinfo* %27)
store %struct.addrinfo* null, %struct.addrinfo** %2
br label %42
29: ; preds = %16
%30 = load i64, i64* %5
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
store i64 %30, i64* %32
%33 = load i32, i32* %3
%34 = load %struct.addrinfo*, %struct.addrinfo** %4
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 1
store i32 %33, i32* %35
%36 = load i32, i32* %3
%37 = load %struct.addrinfo*, %struct.addrinfo** %4
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 2
%39 = load %struct.sockaddr*, %struct.sockaddr** %38
%40 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %39, i32 0, i32 0
store i32 %36, i32* %40
%41 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %41, %struct.addrinfo** %2
br label %42
42: ; preds = %29, %26, %15
%43 = load %struct.addrinfo*, %struct.addrinfo** %2
ret %struct.addrinfo* %43
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, %struct.sockaddr* }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
define %struct.addrinfo* @_allocaddrinfo(i32 %0) {
%2 = tail call i64 @calloc(i32 1, i64 24)
%3 = inttoptr i64 %2 to %struct.addrinfo*
%4 = icmp eq i64 %2, 0
br i1 %4, label %16, label %5
5: ; preds = %1
%6 = tail call i64 @calloc(i32 1, i64 4)
%7 = inttoptr i64 %6 to %struct.sockaddr*
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store %struct.sockaddr* %7, %struct.sockaddr** %8
%9 = icmp eq i64 %6, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @free(%struct.addrinfo* %3)
br label %16
12: ; preds = %5
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 4, i64* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %0, i32* %14
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i64 0, i32 0
store i32 %0, i32* %15
br label %16
16: ; preds = %1, %12, %10
%17 = phi %struct.addrinfo* [ null, %10 ], [ %3, %12 ], [ null, %1 ]
ret %struct.addrinfo* %17
}