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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(char *server, int port) {
  int sd, ret;
  struct hostent *host;
  struct sockaddr_in sa;
  host = gethostbyname(server);
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd == -1) {
    exit(1);
  }
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(sa.sin_zero), 8);
  ret = connect(sd, (struct sockaddr *)&sa, sizeof(struct sockaddr));
  if (ret == 0) {
  } else {
    exit(2);
  }
  return sd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11ac <tcp_connect+0x4c>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x8,%esi
call   1050 <bzero@plt>
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1200 <tcp_connect+0xa0>
jmp    120a <tcp_connect+0xaa>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
call   1040 <gethostbyname@plt>
mov    %rax,%rbp
mov    0x2e6c(%rip),%r15
mov    (%r15),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11e5 <tcp_connect+0x85>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   1050 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %eax,%eax
jne    11ef <tcp_connect+0x8f>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x2,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @tcp_connect(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 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to i32*
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%21 = load i32, i32* %19
store i32 %21, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%23 = call i32 @bzero(i32* %22, i32 8)
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @connect(i32 %8, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %12
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %12
ret i32 %8
}