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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int NET_establish(char *addr, char *port) {
  int sockdes = 0, errno = 0;
  struct addrinfo hints;
  struct addrinfo *res;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  getaddrinfo(addr, port, &hints, &res);
  sockdes = socket(res->ai_family, res->ai_socktype, 0);
  errno = connect(sockdes, res->ai_addr, res->ai_addrlen);
  if (errno != 0) {
    printf("%d\n", errno);
  }
  return sockdes;
}


x86 assembly (clang, -O0):
<NET_establish>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11f4 <NET_establish+0xa4>
mov    -0x18(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NET_establish>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%esi
mov    0xc(%rax),%edi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1050 <connect@plt>
test   %eax,%eax
je     11d7 <NET_establish+0x87>
lea    0xe30(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @NET_establish(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
%9 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 16)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i8*, i8** %3
%15 = load i8*, i8** %4
%16 = call i32 @getaddrinfo(i8* %14, i8* %15, %struct.addrinfo* %7, %struct.addrinfo** %8)
%17 = load %struct.addrinfo*, %struct.addrinfo** %8
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 3
%19 = load i32, i32* %18
%20 = load %struct.addrinfo*, %struct.addrinfo** %8
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 2
%22 = load i32, i32* %21
%23 = call i32 @socket(i32 %19, i32 %22, i32 0)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = load %struct.addrinfo*, %struct.addrinfo** %8
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load %struct.addrinfo*, %struct.addrinfo** %8
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @connect(i32 %24, i32 %27, i32 %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %2
%35 = load i32, i32* %6
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %2
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @NET_establish(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 16)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = load %struct.addrinfo*, %struct.addrinfo** %4
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 3
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 2
%17 = load i32, i32* %16
%18 = call i32 @socket(i32 %15, i32 %17, i32 0)
%19 = load %struct.addrinfo*, %struct.addrinfo** %4
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @connect(i32 %18, i32 %21, i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %2
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %28
28: ; preds = %26, %2
ret i32 %18
}