#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 */
typedef int /*<<< orphan*/ u_long;
struct TYPE_2__ {
  int /*<<< orphan*/ 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;
  int ai_protocol;
  struct sockaddr *ai_addr;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
struct addrinfo *malloc_ai(int port, u_long addr, int socktype, int proto) {
  struct addrinfo *ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) +
                                                  sizeof(struct sockaddr_in));
  if (ai) {
    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;
    ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
    ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
    ai->ai_socktype = socktype;
    ai->ai_protocol = proto;
    return ai;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<malloc_ai>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    $0x28,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11cd <malloc_ai+0xad>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0x20(%rbp),%rcx
add    $0x20,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rax
movl   $0x8,(%rax)
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d5 <malloc_ai+0xb5>
movq   $0x0,-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   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%ebp
mov    $0x28,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1182 <malloc_ai+0x62>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    %rbx,%rax
add    $0x20,%rax
mov    %rax,0x10(%rbx)
movl   $0x8,(%rbx)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rbx)
mov    %ebp,0x20(%rbx)
mov    %r12d,0x24(%rbx)
mov    %r15d,0x4(%rbx)
mov    %r14d,0x8(%rbx)
jmp    1184 <malloc_ai+0x64>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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