#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;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char const *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ print_error_info(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to(const char *ip_addr, const char *port) {
  int sock_fd;
  struct sockaddr_in server;
  sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_fd == -1) {
    print_error_info("[ERROR] Could not create socket.");
  }
  server.sin_family = AF_INET;
  server.sin_port = htons(atoi(port));
  server.sin_addr.s_addr = inet_addr(ip_addr);
  if (connect(sock_fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    print_error_info("[ERROR] Cannot connect to server.");
    close(sock_fd);
    exit(-1);
  }
  return sock_fd;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c2 <connect_to+0x42>
lea    0xe43(%rip),%rdi
call   1030 <print_error_info@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1226 <connect_to+0xa6>
lea    0xe12(%rip),%rdi
call   1030 <print_error_info@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e50(%rip),%r12
mov    (%r12),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bf <connect_to+0x3f>
lea    0xe46(%rip),%rdi
call   1030 <print_error_info@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     120a <connect_to+0x8a>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe10(%rip),%rdi
call   1030 <print_error_info@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%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
@.str = [33 x i8] c"[ERROR] Could not create socket.\00"
@.str.1 = [34 x i8] c"[ERROR] Cannot connect to server.\00"
define i32 @connect_to(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @print_error_info(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %4
%18 = call i32 @atoi(i8* %17)
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i8*, i8** %3
%22 = call i32 @inet_addr(i8* %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %5
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %14
%30 = call i32 @print_error_info(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %14
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
%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
@.str = [33 x i8] c"[ERROR] Could not create socket.\00"
@.str.1 = [34 x i8] c"[ERROR] Cannot connect to server.\00"
define i32 @connect_to(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @print_error_info(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @atoi(i8* %1)
%15 = tail call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = tail call i32 @inet_addr(i8* %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i64 @connect(i32 %7, %struct.sockaddr* %19, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %11
%23 = call i32 @print_error_info(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @close(i32 %7)
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %11
ret i32 %7
}