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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int __socket(struct addrinfo *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int socket_udp_connect(const char *address, const char *port,
                       struct sockaddr_storage *addr) {
  struct addrinfo hints;
  struct addrinfo *info;
  struct addrinfo *p;
  int status;
  int sock;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = IPPROTO_UDP;
  if ((status = getaddrinfo(address, port, &hints, &info)) != 0) {
    fprintf(stderr, "getaddrinfo(): %s", gai_strerror(status));
    return (-1);
  }
  for (p = info; p != NULL; p = p->ai_next) {
    if ((sock = __socket(p, 0, 0)) < 0) {
      perror("socket()");
      continue;
    }
    if (addr != NULL)
      memcpy(addr, p->ai_addr, p->ai_addrlen);
    break;
  }
  freeaddrinfo(info);
  return (sock);
}


x86 assembly (clang, -O0):
<socket_udp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     1227 <socket_udp_connect+0xa7>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x54(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdea(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <socket_udp_connect+0x12a>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     129b <socket_udp_connect+0x11b>
mov    -0x50(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <__socket@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
jge    1264 <socket_udp_connect+0xe4>
lea    0xdb8(%rip),%rdi
call   1070 <perror@plt>
jmp    128a <socket_udp_connect+0x10a>
cmpq   $0x0,-0x20(%rbp)
je     1285 <socket_udp_connect+0x105>
mov    -0x20(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
jmp    129b <socket_udp_connect+0x11b>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    122f <socket_udp_connect+0xaf>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_udp_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1218 <socket_udp_connect+0x98>
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdf9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    126a <socket_udp_connect+0xea>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1261 <socket_udp_connect+0xe1>
lea    0xdea(%rip),%r15
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <__socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    124d <socket_udp_connect+0xcd>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    1228 <socket_udp_connect+0xa8>
jmp    1261 <socket_udp_connect+0xe1>
test   %r14,%r14
je     1261 <socket_udp_connect+0xe1>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1060 <memcpy@plt>
jmp    1261 <socket_udp_connect+0xe1>
mov    (%rsp),%rdi
call   10a0 <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 { i32, i32, %struct.addrinfo*, i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"getaddrinfo(): %s\00"
@.str.1 = [9 x i8] c"socket()\00"
define i32 @socket_udp_connect(i8* %0, i8* %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%13 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 6
store i32 %14, i32* %15
%16 = load i32, i32* @AF_UNSPEC
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_DGRAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %18, i32* %19
%20 = load i32, i32* @IPPROTO_UDP
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %20, i32* %21
%22 = load i8*, i8** %5
%23 = load i8*, i8** %6
%24 = call i32 @getaddrinfo(i8* %22, i8* %23, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %24, i32* %11
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %3
%27 = load i32, i32* @stderr
%28 = load i32, i32* %11
%29 = call i8* @gai_strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %29)
store i32 -1, i32* %4
br label %63
31: ; preds = %3
%32 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %32, %struct.addrinfo** %10
br label %33
33: ; preds = %55, %31
%34 = load %struct.addrinfo*, %struct.addrinfo** %10
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %59
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %10
%38 = call i32 @__socket(%struct.addrinfo* %37, i32 0, i32 0)
store i32 %38, i32* %12
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %55
42: ; preds = %36
%43 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%44 = icmp ne %struct.sockaddr_storage* %43, null
br i1 %44, label %45, label %54
45: ; preds = %42
%46 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%47 = load %struct.addrinfo*, %struct.addrinfo** %10
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %10
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @memcpy(%struct.sockaddr_storage* %46, i32 %49, i32 %52)
br label %54
54: ; preds = %45, %42
br label %59
55: ; preds = %40
%56 = load %struct.addrinfo*, %struct.addrinfo** %10
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 2
%58 = load %struct.addrinfo*, %struct.addrinfo** %57
store %struct.addrinfo* %58, %struct.addrinfo** %10
br label %33
59: ; preds = %54, %33
%60 = load %struct.addrinfo*, %struct.addrinfo** %9
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %60)
%62 = load i32, i32* %12
store i32 %62, i32* %4
br label %63
63: ; preds = %59, %26
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"getaddrinfo(): %s\00"
@.str.1 = [9 x i8] c"socket()\00"
define i32 @socket_udp_connect(i8* %0, i8* %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 32)
%9 = load i32, i32* @AI_PASSIVE
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %9, i32* %10
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @IPPROTO_UDP
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
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 %19, label %22
19: ; preds = %3
%20 = load %struct.addrinfo*, %struct.addrinfo** %5
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %43, label %26
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = call i8* @gai_strerror(i32 %17)
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %47
26: ; preds = %19, %30
%27 = phi %struct.addrinfo* [ %33, %30 ], [ %20, %19 ]
%28 = call i32 @__socket(%struct.addrinfo* %27, i32 0, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 2
%33 = load %struct.addrinfo*, %struct.addrinfo** %32
%34 = icmp eq %struct.addrinfo* %33, null
br i1 %34, label %43, label %26
35: ; preds = %26
%36 = icmp eq %struct.sockaddr_storage* %2, null
br i1 %36, label %43, label %37
37: ; preds = %35
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @memcpy(%struct.sockaddr_storage* %2, i32 %39, i32 %41)
br label %43
43: ; preds = %30, %19, %35, %37
%44 = phi i32 [ %28, %37 ], [ %28, %35 ], [ undef, %19 ], [ %28, %30 ]
%45 = load %struct.addrinfo*, %struct.addrinfo** %5
%46 = call i32 @freeaddrinfo(%struct.addrinfo* %45)
br label %47
47: ; preds = %43, %22
%48 = phi i32 [ -1, %22 ], [ %44, %43 ]
ret i32 %48
}