#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 addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ sa_family_t;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                         char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memmove(struct sockaddr_storage *, struct sockaddr_storage *,
                           int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int getaddrinfo_for_bind(char *port, sa_family_t family,
                         struct sockaddr_storage *saddr) {
  int status;
  struct addrinfo hints;
  struct addrinfo *addrinfos;
  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = family;
  status = getaddrinfo(NULL, port, &hints, &addrinfos);
  if (status != 0) {
    error(0, 0, "Failed to create socket address: %s", gai_strerror(status));
    goto out;
  }
  memmove(saddr, (struct sockaddr_storage *)addrinfos->ai_addr, sizeof(*saddr));
out:
  freeaddrinfo(addrinfos);
  return status;
}


x86 assembly (clang, -O0):
<getaddrinfo_for_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11e3 <getaddrinfo_for_bind+0x83>
mov    -0x1c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    %rax,%rcx
xor    %esi,%esi
lea    0xe29(%rip),%rdx
mov    %esi,%edi
call   1060 <error@plt>
jmp    11f8 <getaddrinfo_for_bind+0x98>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
call   1050 <memmove@plt>
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaddrinfo_for_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %ebp,0x18(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11d8 <getaddrinfo_for_bind+0x78>
mov    %ebp,%edi
call   1030 <gai_strerror@plt>
lea    0xe36(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
mov    %rax,%rcx
call   1060 <error@plt>
jmp    11ed <getaddrinfo_for_bind+0x8d>
mov    0x8(%rsp),%rax
mov    (%rax),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1050 <memmove@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i64, i32, i32, i32 }
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [36 x i8] c"Failed to create socket address: %s\00"
define i32 @getaddrinfo_for_bind(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_storage*
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %6
%10 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_PASSIVE
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* %5
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i8*, i8** %4
%18 = call i32 @getaddrinfo(i32* null, i8* %17, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %3
%22 = load i32, i32* %7
%23 = call i8* @gai_strerror(i32 %22)
%24 = call i32 @error(i32 0, i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %32
25: ; preds = %3
%26 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%27 = load %struct.addrinfo*, %struct.addrinfo** %9
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to %struct.sockaddr_storage*
%31 = call i32 @memmove(%struct.sockaddr_storage* %26, %struct.sockaddr_storage* %30, i32 4)
br label %32
32: ; preds = %25, %21
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
%34 = call i32 @freeaddrinfo(%struct.addrinfo* %33)
%35 = load i32, i32* %7
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i64, i32, i32, i32 }
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [36 x i8] c"Failed to create socket address: %s\00"
define i32 @getaddrinfo_for_bind(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 24)
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %1, i32* %13
%14 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %4, %struct.addrinfo** %5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = call i8* @gai_strerror(i32 %14)
%18 = call i32 @error(i32 0, i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %25
19: ; preds = %3
%20 = load %struct.addrinfo*, %struct.addrinfo** %5
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 0
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to %struct.sockaddr_storage*
%24 = call i32 @memmove(%struct.sockaddr_storage* %2, %struct.sockaddr_storage* %23, i32 4)
br label %25
25: ; preds = %19, %16
%26 = load %struct.addrinfo*, %struct.addrinfo** %5
%27 = call i32 @freeaddrinfo(%struct.addrinfo* %26)
ret i32 %14
}