#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_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int udp_server_connect(char *argv[], struct addrinfo *p) {
  int sockfd;
  struct addrinfo hints, *servinfo;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  if ((rv = getaddrinfo(argv[2], argv[3], &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("proxy: socket");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "proxy: failed to create socket\n");
    return -1;
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<udp_server_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1205 <udp_server_connect+0x95>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <udp_server_connect+0x131>
mov    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1264 <udp_server_connect+0xf4>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    124e <udp_server_connect+0xde>
lea    0xdcd(%rip),%rdi
call   1060 <perror@plt>
jmp    1253 <udp_server_connect+0xe3>
jmp    1264 <udp_server_connect+0xf4>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    120d <udp_server_connect+0x9d>
cmpq   $0x0,-0x18(%rbp)
jne    1292 <udp_server_connect+0x122>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <udp_server_connect+0x131>
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_server_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x10(%rbx),%rdi
mov    0x18(%rbx),%rsi
mov    %rsp,%rcx
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11ea <udp_server_connect+0x7a>
mov    0x2e09(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1246 <udp_server_connect+0xd6>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     121d <udp_server_connect+0xad>
lea    0xe17(%rip),%r14
mov    0x8(%rbx),%edi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
jne    123b <udp_server_connect+0xcb>
mov    %r14,%rdi
call   1060 <perror@plt>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    11fa <udp_server_connect+0x8a>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1246 <udp_server_connect+0xd6>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [14 x i8] c"proxy: socket\00"
@.str.2 = [32 x i8] c"proxy: failed to create socket\0A\00"
define i32 @udp_server_connect(i8** %0, %struct.addrinfo* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca i32
store i8** %0, i8*** %4
store %struct.addrinfo* %1, %struct.addrinfo** %5
%10 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 24)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = load i8**, i8*** %4
%19 = getelementptr inbounds i8*, i8** %18, i64 3
%20 = load i8*, i8** %19
%21 = call i32 @getaddrinfo(i8* %17, i8* %20, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %21, i32* %9
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = load i32, i32* %9
%26 = call i8* @gai_strerror(i32 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %26)
store i32 -1, i32* %3
br label %62
28: ; preds = %2
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %29, %struct.addrinfo** %5
br label %30
30: ; preds = %48, %28
%31 = load %struct.addrinfo*, %struct.addrinfo** %5
%32 = icmp ne %struct.addrinfo* %31, null
br i1 %32, label %33, label %52
33: ; preds = %30
%34 = load %struct.addrinfo*, %struct.addrinfo** %5
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %5
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %5
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @socket(i32 %36, i32 %39, i32 %42)
store i32 %43, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %33
%46 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %48
47: ; preds = %33
br label %52
48: ; preds = %45
%49 = load %struct.addrinfo*, %struct.addrinfo** %5
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 3
%51 = load %struct.addrinfo*, %struct.addrinfo** %50
store %struct.addrinfo* %51, %struct.addrinfo** %5
br label %30
52: ; preds = %47, %30
%53 = load %struct.addrinfo*, %struct.addrinfo** %5
%54 = icmp eq %struct.addrinfo* %53, null
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
58: ; preds = %52
%59 = load %struct.addrinfo*, %struct.addrinfo** %8
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
%61 = load i32, i32* %6
store i32 %61, i32* %3
br label %62
62: ; preds = %58, %55, %23
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [14 x i8] c"proxy: socket\00"
@.str.2 = [32 x i8] c"proxy: failed to create socket\0A\00"
define i32 @udp_server_connect(i8** nocapture %0, %struct.addrinfo* nocapture readnone %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_DGRAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds i8*, i8** %0, i64 2
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %0, i64 3
%15 = load i8*, i8** %14
%16 = call i32 @getaddrinfo(i8* %13, i8* %15, %struct.addrinfo* %3, %struct.addrinfo** %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %4
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %40, label %25
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i8* @gai_strerror(i32 %16)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %46
25: ; preds = %18, %35
%26 = phi %struct.addrinfo* [ %38, %35 ], [ %19, %18 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %43
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%38 = load %struct.addrinfo*, %struct.addrinfo** %37
%39 = icmp eq %struct.addrinfo* %38, null
br i1 %39, label %40, label %25
40: ; preds = %35, %18
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %46
43: ; preds = %25
%44 = load %struct.addrinfo*, %struct.addrinfo** %4
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
br label %46
46: ; preds = %43, %40, %21
%47 = phi i32 [ -1, %21 ], [ -1, %40 ], [ %33, %43 ]
ret i32 %47
}