#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 /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_server(char *server, int port) {
  int sock;
  struct hostent *hostinfo;
  struct sockaddr_in saddr;
  if ((hostinfo = gethostbyname(server)) != NULL) {
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
      bzero(&saddr, sizeof(struct sockaddr_in));
      saddr.sin_family = AF_INET;
      saddr.sin_port = htons(port);
      memcpy(&saddr.sin_addr.s_addr, hostinfo->h_addr, 4);
      if (connect(sock, (struct sockaddr *)&saddr,
                  sizeof(struct sockaddr_in)) != -1) {
        return sock;
      }
      close(sock);
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<connect_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1226 <connect_server+0xb6>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1221 <connect_server+0xb1>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1219 <connect_server+0xa9>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122d <connect_server+0xbd>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    1226 <connect_server+0xb6>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
call   1050 <gethostbyname@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     120a <connect_server+0x9a>
mov    %rax,%rbx
mov    0x2e4d(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
jle    120a <connect_server+0x9a>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%esi
mov    %r12,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1203 <connect_server+0x93>
mov    %ebp,%r15d
jmp    120a <connect_server+0x9a>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %7
%11 = icmp ne %struct.hostent* %10, null
br i1 %11, label %12, label %40
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %12
%18 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = load %struct.hostent*, %struct.hostent** %7
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i32* %25, i32 %28, i32 4)
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%32 = call i32 @connect(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %17
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %41
36: ; preds = %17
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %12
br label %40
40: ; preds = %39, %2
store i32 -1, i32* %3
br label %41
41: ; preds = %40, %34
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_server(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %7
%13 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 %1)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i32* %18, i32 %20, i32 4)
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i32 @connect(i32 %10, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = call i32 @close(i32 %10)
br label %27
27: ; preds = %2, %25, %7, %12
%28 = phi i32 [ %10, %12 ], [ -1, %7 ], [ -1, %25 ], [ -1, %2 ]
ret i32 %28
}