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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int clientUDPsock(const char *host, const char *service) {
  struct addrinfo *ainfo;
  struct addrinfo hints, *rp;
  int s, type;
  char msg[256];
  int errCode;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0)
    err_sys("connectsock() can't create socket");
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = 0;
  hints.ai_protocol = IPPROTO_UDP;
  errCode = getaddrinfo(host, service, &hints, &ainfo);
  if (errCode) {
    snprintf(msg, 256, "getaddrinfo() failed: %s\n", gai_strerror(errCode));
    err_quit(msg);
  } else {
    for (rp = ainfo; rp != NULL; rp = rp->ai_next)
      if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0)
        break;
    if (rp == NULL)
      err_quit("Could not connect; No gai() address succeeded\n");
  }
  freeaddrinfo(ainfo);
  return s;
}


x86 assembly (clang, -O0):
<clientUDPsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    11d5 <clientUDPsock+0x45>
lea    0xe30(%rip),%rdi
call   1030 <err_sys@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0x0,-0x154(%rbp)
je     127e <clientUDPsock+0xee>
lea    -0x150(%rbp),%rax
mov    %rax,-0x160(%rbp)
mov    -0x154(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x160(%rbp),%rdi
mov    %rax,%rcx
mov    $0x100,%esi
lea    0xdba(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x150(%rbp),%rdi
call   1070 <err_quit@plt>
jmp    12e7 <clientUDPsock+0x157>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12cb <clientUDPsock+0x13b>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jne    12b5 <clientUDPsock+0x125>
jmp    12cb <clientUDPsock+0x13b>
jmp    12ba <clientUDPsock+0x12a>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1286 <clientUDPsock+0xf6>
cmpq   $0x0,-0x48(%rbp)
jne    12e2 <clientUDPsock+0x152>
lea    0xd5f(%rip),%rdi
call   1070 <err_quit@plt>
jmp    12e7 <clientUDPsock+0x157>
mov    -0x18(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clientUDPsock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e3a(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e27(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11d5 <clientUDPsock+0x45>
lea    0xe30(%rip),%rdi
call   1030 <err_sys@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x2c(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     124b <clientUDPsock+0xbb>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdf1(%rip),%rdx
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
mov    %r14,%rdi
jmp    1282 <clientUDPsock+0xf2>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     127b <clientUDPsock+0xeb>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   1080 <connect@plt>
test   %rax,%rax
je     1287 <clientUDPsock+0xf7>
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
jne    1260 <clientUDPsock+0xd0>
lea    0xdba(%rip),%rdi
call   1070 <err_quit@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i64, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [34 x i8] c"connectsock() can't create socket\00"
@IPPROTO_UDP = global i32 0
@.str.1 = [26 x i8] c"getaddrinfo() failed: %s\0A\00"
@.str.2 = [47 x i8] c"Could not connect; No gai() address succeeded\0A\00"
define i32 @clientUDPsock(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @err_sys(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i32 %21, i32* %22
%23 = load i32, i32* @SOCK_DGRAM
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i64 0, i64* %25
%26 = load i32, i32* @IPPROTO_UDP
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %26, i32* %27
%28 = load i8*, i8** %3
%29 = load i8*, i8** %4
%30 = call i32 @getaddrinfo(i8* %28, i8* %29, %struct.addrinfo* %6, %struct.addrinfo** %5)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %19
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%35 = load i32, i32* %11
%36 = call i8* @gai_strerror(i32 %35)
%37 = call i32 @snprintf(i8* %34, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%39 = call i32 @err_quit(i8* %38)
br label %67
40: ; preds = %19
%41 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %41, %struct.addrinfo** %7
br label %42
42: ; preds = %57, %40
%43 = load %struct.addrinfo*, %struct.addrinfo** %7
%44 = icmp ne %struct.addrinfo* %43, null
br i1 %44, label %45, label %61
45: ; preds = %42
%46 = load i32, i32* %8
%47 = load %struct.addrinfo*, %struct.addrinfo** %7
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %7
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @connect(i32 %46, i32 %49, i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %45
br label %61
56: ; preds = %45
br label %57
57: ; preds = %56
%58 = load %struct.addrinfo*, %struct.addrinfo** %7
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 2
%60 = load %struct.addrinfo*, %struct.addrinfo** %59
store %struct.addrinfo* %60, %struct.addrinfo** %7
br label %42
61: ; preds = %55, %42
%62 = load %struct.addrinfo*, %struct.addrinfo** %7
%63 = icmp eq %struct.addrinfo* %62, null
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @err_quit(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
br label %67
67: ; preds = %66, %33
%68 = load %struct.addrinfo*, %struct.addrinfo** %5
%69 = call i32 @freeaddrinfo(%struct.addrinfo* %68)
%70 = load i32, i32* %8
ret i32 %70
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i64, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [34 x i8] c"connectsock() can't create socket\00"
@IPPROTO_UDP = global i32 0
@.str.1 = [26 x i8] c"getaddrinfo() failed: %s\0A\00"
@.str.2 = [47 x i8] c"Could not connect; No gai() address succeeded\0A\00"
define i32 @clientUDPsock(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = alloca [256 x i8]
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @err_sys(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_DGRAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i64 0, i64* %21
%22 = load i32, i32* @IPPROTO_UDP
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %22, i32* %23
%24 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %3)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = load %struct.addrinfo*, %struct.addrinfo** %3
%28 = icmp eq %struct.addrinfo* %27, null
br i1 %28, label %45, label %37
29: ; preds = %15
%30 = call i8* @gai_strerror(i32 %24)
%31 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = call i32 @err_quit(i8* %8)
br label %47
33: ; preds = %37
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 2
%35 = load %struct.addrinfo*, %struct.addrinfo** %34
%36 = icmp eq %struct.addrinfo* %35, null
br i1 %36, label %45, label %37
37: ; preds = %26, %33
%38 = phi %struct.addrinfo* [ %35, %33 ], [ %27, %26 ]
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @connect(i32 %11, i32 %40, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %33
45: ; preds = %33, %26
%46 = call i32 @err_quit(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %37, %45, %29
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
ret i32 %11
}