#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  unsigned long s_addr;
};
struct sockaddr_in {
  int sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int /*<<< orphan*/ sa_family;
};
struct addrinfo {
  int ai_addrlen;
  int ai_socktype;
  struct sockaddr *ai_addr;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct addrinfo *malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
struct addrinfo *malloc_ai(int port, int type, unsigned long addr) {
  struct addrinfo *ai;
  ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
  if (ai == NULL)
    return (NULL);
  memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
  ai->ai_addr = (struct sockaddr *)(ai + 1);
  ai->ai_addrlen = sizeof(struct sockaddr_in);
  ai->ai_addr->sa_family = ai->ai_family = AF_INET;
  ai->ai_socktype = type;
  ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
  ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
  return (ai);
}


x86 assembly (clang, -O0):
<malloc_ai>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x28,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1158 <malloc_ai+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11cb <malloc_ai+0xab>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0x20(%rbp),%rcx
add    $0x18,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
movl   $0x10,(%rax)
mov    0x2e5f(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc_ai>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
mov    $0x28,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1178 <malloc_ai+0x58>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    %rbx,%rax
add    $0x18,%rax
mov    %rax,0x8(%rbx)
movl   $0x10,(%rbx)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rbx)
mov    %r15d,0x4(%rbx)
mov    %ebp,0x18(%rbx)
mov    %r14,0x20(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.sockaddr*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define %struct.addrinfo* @malloc_ai(i32 %0, i32 %1, i64 %2) {
%4 = tail call %struct.addrinfo* @malloc(i32 40)
%5 = icmp eq %struct.addrinfo* %4, null
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = tail call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 1
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
%10 = bitcast %struct.sockaddr** %9 to %struct.addrinfo**
store %struct.addrinfo* %8, %struct.addrinfo** %10
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 16, i32* %11
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %1, i32* %14
%15 = getelementptr %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 0
store i32 %0, i32* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 1, i32 2
%17 = bitcast %struct.sockaddr** %16 to i64*
store i64 %2, i64* %17
br label %18
18: ; preds = %3, %6
ret %struct.addrinfo* %4
}