#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ __die__(char *);
int connect(int, struct sockaddr *, int);
scalar_t__ get_ip_from_host(char *, char const *, int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server(const char *host, unsigned short port) {
  int sockfd;
  struct sockaddr_in address;
  int conn_ret;
  char ip[128] = {0};
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  address.sin_family = AF_INET;
  if (get_ip_from_host(ip, host, 128) != 0) {
    __die__("unknown error.");
  }
  address.sin_addr.s_addr = inet_addr(ip);
  address.sin_port = htons(port);
  conn_ret = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
  if (conn_ret == 0)
    return sockfd;
  else
    __die__("connect to server error.");
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x80,%edx
call   1070 <get_ip_from_host@plt>
cmp    $0x0,%rax
je     11ec <connect_to_server+0x7c>
lea    0xe19(%rip),%rdi
call   1060 <__die__@plt>
lea    -0xb0(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1230 <connect_to_server+0xc0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <connect_to_server+0xcc>
lea    0xdd8(%rip),%rdi
call   1060 <__die__@plt>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
xorps  %xmm0,%xmm0
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    0x2e40(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    %r15,%rsi
mov    $0x80,%edx
call   1060 <get_ip_from_host@plt>
test   %rax,%rax
je     11e7 <connect_to_server+0x87>
lea    0xe1e(%rip),%rdi
call   1050 <__die__@plt>
lea    0x10(%rsp),%rdi
call   1040 <inet_addr@plt>
mov    %eax,0x4(%rsp)
movzwl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %eax,%eax
je     1220 <connect_to_server+0xc0>
lea    0xdf4(%rip),%rdi
call   1050 <__die__@plt>
mov    %ebx,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"unknown error.\00"
@.str.1 = [25 x i8] c"connect to server error.\00"
define i32 @connect_to_server(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca [128 x i8]
store i8* %0, i8** %4
store i16 %1, i16* %5
%10 = bitcast [128 x i8]* %9 to i8*
%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* %6
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%17 = load i8*, i8** %4
%18 = call i64 @get_ip_from_host(i8* %16, i8* %17, i32 128)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @__die__(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %2
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%24 = call i32 @inet_addr(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i16, i16* %5
%28 = call i32 @htons(i16 zeroext %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%32 = call i32 @connect(i32 %30, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %39
37: ; preds = %22
%38 = call i32 @__die__(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %35, %37
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"unknown error.\00"
@.str.1 = [25 x i8] c"connect to server error.\00"
define i32 @connect_to_server(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [128 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i64 @get_ip_from_host(i8* %6, i8* %0, i32 128)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @__die__(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = call i32 @inet_addr(i8* %6)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = call i32 @htons(i16 zeroext %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @connect(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %16
%25 = call i32 @__die__(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %16, %24
ret i32 %9
}