#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  int sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  scalar_t__ sa_family;
};
struct hostent {
  scalar_t__ h_addrtype;
  scalar_t__ h_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ EPROTOTYPE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int htons(int);
int inet_addr(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server(char *host, int port) {
  struct sockaddr_in inaddr;
  struct sockaddr *addr;
  struct hostent *host_ptr;
  int addrlen;
  extern char *getenv();
  extern struct hostent *gethostbyname();
  int fd;
  if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1) {
    if ((host_ptr = gethostbyname(host)) == NULL) {
      errno = EINVAL;
      return (0);
    }
    if (host_ptr->h_addrtype != AF_INET) {
      errno = EPROTOTYPE;
      return (0);
    }
    inaddr.sin_family = host_ptr->h_addrtype;
    memcpy((char *)&inaddr.sin_addr, (char *)host_ptr->h_addr,
           sizeof(inaddr.sin_addr));
  } else
    inaddr.sin_family = AF_INET;
  addr = (struct sockaddr *)&inaddr;
  addrlen = sizeof(struct sockaddr_in);
  inaddr.sin_port = port;
  inaddr.sin_port = htons(inaddr.sin_port);
  if ((fd = socket((int)addr->sa_family, SOCK_STREAM, 0)) < 0)
    return (0);
  if (connect(fd, addr, addrlen) == -1) {
    (void)close(fd);
    return (0);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    122a <connect_to_server+0xba>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11cb <connect_to_server+0x5b>
mov    0x2e0c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0b(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12bb <connect_to_server+0x14b>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
je     1200 <connect_to_server+0x90>
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd6(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12bb <connect_to_server+0x14b>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
add    $0xc,%rdi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    1238 <connect_to_server+0xc8>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x10,-0x3c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jge    1289 <connect_to_server+0x119>
movl   $0x0,-0x4(%rbp)
jmp    12bb <connect_to_server+0x14b>
mov    -0x40(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b5 <connect_to_server+0x145>
mov    -0x40(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bb <connect_to_server+0x14b>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     11e1 <connect_to_server+0x71>
mov    0x2e56(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    %ebp,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rsp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1250 <connect_to_server+0xe0>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     121e <connect_to_server+0xae>
mov    %ebp,%r14d
jmp    1250 <connect_to_server+0xe0>
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1227 <connect_to_server+0xb7>
mov    (%rax),%rcx
mov    0x2deb(%rip),%rdx
cmp    (%rdx),%rcx
jne    123b <connect_to_server+0xcb>
lea    0xc(%rsp),%rdi
mov    %rcx,(%rsp)
mov    0x8(%rax),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    1199 <connect_to_server+0x29>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1250 <connect_to_server+0xe0>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    0x2d91(%rip),%rcx
mov    %eax,(%rcx)
jmp    1250 <connect_to_server+0xe0>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    0x2d7d(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i64 }
%struct.hostent = type { i64, i64 }
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET = global i64 0
@EPROTOTYPE = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_to_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr*
%8 = alloca %struct.hostent*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = call i32 @inet_addr(i8* %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = icmp eq i32 %12, -1
br i1 %15, label %16, label %42
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %17)
store %struct.hostent* %18, %struct.hostent** %8
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i32, i32* @EINVAL
store i32 %21, i32* @errno
store i32 0, i32* %3
br label %72
22: ; preds = %16
%23 = load %struct.hostent*, %struct.hostent** %8
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @AF_INET
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* @EPROTOTYPE
store i32 %29, i32* @errno
store i32 0, i32* %3
br label %72
30: ; preds = %22
%31 = load %struct.hostent*, %struct.hostent** %8
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i64 %33, i64* %34
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%36 = bitcast %struct.TYPE_2__* %35 to i8*
%37 = load %struct.hostent*, %struct.hostent** %8
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 1
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to i8*
%41 = call i32 @memcpy(i8* %36, i8* %40, i32 4)
br label %45
42: ; preds = %2
%43 = load i64, i64* @AF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i64 %43, i64* %44
br label %45
45: ; preds = %42, %30
%46 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
store %struct.sockaddr* %46, %struct.sockaddr** %7
store i32 16, i32* %9
%47 = load i32, i32* %5
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i32 @htons(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %51, i32* %52
%53 = load %struct.sockaddr*, %struct.sockaddr** %7
%54 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %53, i32 0, i32 0
%55 = load i64, i64* %54
%56 = trunc i64 %55 to i32
%57 = load i32, i32* @SOCK_STREAM
%58 = call i32 @socket(i32 %56, i32 %57, i32 0)
store i32 %58, i32* %10
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %45
store i32 0, i32* %3
br label %72
61: ; preds = %45
%62 = load i32, i32* %10
%63 = load %struct.sockaddr*, %struct.sockaddr** %7
%64 = load i32, i32* %9
%65 = call i32 @connect(i32 %62, %struct.sockaddr* %63, i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %61
%68 = load i32, i32* %10
%69 = call i32 @close(i32 %68)
store i32 0, i32* %3
br label %72
70: ; preds = %61
%71 = load i32, i32* %10
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %67, %60, %28, %20
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64, i64 }
%struct.sockaddr = type { i64 }
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET = global i64 0
@EPROTOTYPE = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_to_server(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i32 @inet_addr(i8* %0)
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%7 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %6, i64 0, i32 0
store i32 %5, i32* %7
%8 = icmp eq i32 %5, -1
br i1 %8, label %9, label %28
9: ; preds = %2
%10 = tail call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %0)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
br label %46
14: ; preds = %9
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @AF_INET
%18 = icmp eq i64 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = load i32, i32* @EPROTOTYPE
store i32 %20, i32* @errno
br label %46
21: ; preds = %14
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i64 %16, i64* %22
%23 = bitcast %struct.TYPE_2__* %6 to i8*
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to i8*
%27 = call i32 @memcpy(i8* %23, i8* %26, i32 4)
br label %31
28: ; preds = %2
%29 = load i64, i64* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i64 %29, i64* %30
br label %31
31: ; preds = %28, %21
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %1, i32* %32
%33 = call i32 @htons(i32 %1)
store i32 %33, i32* %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i32
%37 = load i32, i32* @SOCK_STREAM
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
%39 = icmp slt i32 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %31
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i32 @connect(i32 %38, %struct.sockaddr* %41, i32 16)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @close(i32 %38)
br label %46
46: ; preds = %40, %31, %44, %19, %12
%47 = phi i32 [ 0, %12 ], [ 0, %19 ], [ 0, %44 ], [ 0, %31 ], [ %38, %40 ]
ret i32 %47
}