#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;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int conn_nonb(int, 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 *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned short);
int /*<<< orphan*/ stderr;
int open_client_socket(const char *hostname, unsigned short int Port) {
  int sockfd;
  struct addrinfo hints, *servinfo, *p;
  int rv;
  char port[8];
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  sprintf(port, "%hd", Port);
  if ((rv = getaddrinfo(hostname, port, &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("socket");
      continue;
    }
    if (conn_nonb(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      perror("conn_nonb");
      printf("host:%s:%s\n", hostname, port);
      continue;
    }
    break;
  }
  if (p == NULL) {
    freeaddrinfo(servinfo);
    return -1;
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<open_client_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x5c(%rbp),%rdi
lea    0xdfe(%rip),%rsi
movzwl -0x12(%rbp),%edx
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x5c(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     125e <open_client_socket+0xae>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x54(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xdb7(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <open_client_socket+0x188>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1309 <open_client_socket+0x159>
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   10d0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12a8 <open_client_socket+0xf8>
lea    0xd77(%rip),%rdi
call   1090 <perror@plt>
jmp    12f8 <open_client_socket+0x148>
mov    -0x18(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <conn_nonb@plt>
cmp    $0xffffffff,%eax
jne    12f3 <open_client_socket+0x143>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xd47(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rsi
lea    -0x5c(%rbp),%rdx
lea    0xd3d(%rip),%rdi
call   1030 <printf@plt>
jmp    12f8 <open_client_socket+0x148>
jmp    1309 <open_client_socket+0x159>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1266 <open_client_socket+0xb6>
cmpq   $0x0,-0x50(%rbp)
jne    1329 <open_client_socket+0x179>
mov    -0x48(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <open_client_socket+0x188>
mov    -0x48(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_client_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0xdfb(%rip),%rsi
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
mov    %rsp,%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1254 <open_client_socket+0xa4>
mov    0x2da0(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdc4(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12d9 <open_client_socket+0x129>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     12ea <open_client_socket+0x13a>
lea    0xdb4(%rip),%r15
lea    0xdb7(%rip),%r12
lea    0x8(%rsp),%r13
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c2 <open_client_socket+0x112>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <conn_nonb@plt>
cmp    $0xffffffff,%eax
jne    12d0 <open_client_socket+0x120>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15,%rdi
call   1090 <perror@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <printf@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1274 <open_client_socket+0xc4>
jmp    12f6 <open_client_socket+0x146>
lea    0xd4c(%rip),%rdi
call   1090 <perror@plt>
jmp    12b7 <open_client_socket+0x107>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10c0 <freeaddrinfo@plt>
jmp    124a <open_client_socket+0x9a>
mov    (%rsp),%rdi
jmp    12ec <open_client_socket+0x13c>

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

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