#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;
  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*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int get_socket(char *host, char *port) {
  int status, fd;
  struct addrinfo hints, *servinfo, *p;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  if ((status = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo() error: %s\n", gai_strerror(status));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      fprintf(stderr, "Socket error: %s\n", strerror(errno));
      continue;
    }
    break;
  }
  freeaddrinfo(servinfo);
  if (p == NULL) {
    fprintf(stderr, "Failed to create socket\n");
    return -1;
  }
  if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {
    fprintf(stderr, "Connect error: %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<get_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     121d <get_socket+0x8d>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <get_socket+0x1ad>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     129f <get_socket+0x10f>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1289 <get_socket+0xf9>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    128e <get_socket+0xfe>
jmp    129f <get_socket+0x10f>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1225 <get_socket+0x95>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x50(%rbp)
jne    12d6 <get_socket+0x146>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <get_socket+0x1ad>
mov    -0x20(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1337 <get_socket+0x1a7>
mov    0x2cd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xd28(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <get_socket+0x1ad>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     120f <get_socket+0x7f>
mov    0x2de2(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe02(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    128c <get_socket+0xfc>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     126e <get_socket+0xde>
mov    0x2db0(%rip),%r12
mov    0x2da1(%rip),%r15
lea    0xdeb(%rip),%r14
xchg   %ax,%ax
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
jne    12a0 <get_socket+0x110>
mov    (%r12),%ebx
mov    (%r15),%edi
call   1090 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
jne    1230 <get_socket+0xa0>
mov    0x8(%rsp),%rdi
jmp    1270 <get_socket+0xe0>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   1070 <connect@plt>
test   %rax,%rax
jns    1291 <get_socket+0x101>
mov    (%r12),%ebp
mov    (%r15),%edi
call   1090 <strerror@plt>
lea    0xd73(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    128c <get_socket+0xfc>

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

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