#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 addrinfo {
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
struct addrinfo *prepare_addrinfo_tcp(const char *addr, const char *port,
                                      char *errorstr) {
  struct addrinfo hints, *res;
  int gai;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = AI_PASSIVE;
  if ((gai = getaddrinfo(addr, port, &hints, &res)) != 0) {
    if (errorstr != NULL)
      sprintf(errorstr, "gai: %s", gai_strerror(gai));
    return NULL;
  }
  return res;
}


x86 assembly (clang, -O0):
<prepare_addrinfo_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     11f0 <prepare_addrinfo_tcp+0xb0>
cmpq   $0x0,-0x20(%rbp)
je     11e3 <prepare_addrinfo_tcp+0xa3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xe22(%rip),%rsi
call   1050 <sprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f8 <prepare_addrinfo_tcp+0xb8>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_addrinfo_tcp>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11d2 <prepare_addrinfo_tcp+0x92>
test   %r14,%r14
je     11ce <prepare_addrinfo_tcp+0x8e>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe3d(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1050 <sprintf@plt>
xor    %eax,%eax
jmp    11d7 <prepare_addrinfo_tcp+0x97>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AI_PASSIVE = global i32 0
@.str = [8 x i8] c"gai: %s\00"
define %struct.addrinfo* @prepare_addrinfo_tcp(i8* %0, i8* %1, i8* writeonly %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 16)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @IPPROTO_TCP
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %15, i32* %16
%17 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %3
%20 = icmp eq i8* %2, null
br i1 %20, label %26, label %21
21: ; preds = %19
%22 = call i8* @gai_strerror(i32 %17)
%23 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %26
24: ; preds = %3
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %26
26: ; preds = %19, %21, %24
%27 = phi %struct.addrinfo* [ %25, %24 ], [ null, %21 ], [ null, %19 ]
ret %struct.addrinfo* %27
}