#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_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           unsigned short, int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to(const char *ip, unsigned short port) {
  struct sockaddr_in server_addr = {.sin_family = AF_INET,
                                    .sin_port = htons(port)};
  server_addr.sin_addr.s_addr = inet_addr(ip);
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == fd) {
    perror("create socket fail");
    return -1;
  }
  if (-1 ==
      (connect(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))) {
    fprintf(stderr, "connect to %s:%d fail, err %d.\n", ip, port, errno);
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    1209 <connect_to+0x89>
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <connect_to+0xed>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1267 <connect_to+0xe7>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x12(%rbp),%ax
mov    0x2d87(%rip),%rcx
mov    (%rcx),%r8d
lea    0xdc8(%rip),%rsi
movzwl %ax,%ecx
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <connect_to+0xed>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e59(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1209 <connect_to+0x99>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    11fe <connect_to+0x8e>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%r8d
movzwl %bp,%ecx
lea    0xe29(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
jmp    11f9 <connect_to+0x89>

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 = [19 x i8] c"create socket fail\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"connect to %s:%d fail, err %d.\0A\00"
@errno = global i32 0
define i32 @connect_to(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%9 = bitcast %struct.TYPE_2__* %8 to i8*
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%11 = load i16, i16* %5
%12 = call i32 @htons(i16 zeroext %11)
store i32 %12, i32* %10
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%14 = load i32, i32* @AF_INET
store i32 %14, i32* %13
%15 = load i8*, i8** %4
%16 = call i32 @inet_addr(i8* %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
26: ; preds = %2
%27 = load i32, i32* %7
%28 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%29 = call i32 @connect(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 -1, %29
br i1 %30, label %31, label %39
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %4
%34 = load i16, i16* %5
%35 = load i32, i32* @errno
%36 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %33, i16 zeroext %34, i32 %35)
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %3
br label %41
39: ; preds = %26
%40 = load i32, i32* %7
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %31, %24
%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.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"create socket fail\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"connect to %s:%d fail, err %d.\0A\00"
@errno = global i32 0
define i32 @connect_to(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%7 = tail call i32 @htons(i16 zeroext %1)
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%9 = load i32, i32* @AF_INET
store i32 %9, i32* %8
%10 = tail call i32 @inet_addr(i8* %0)
store i32 %10, i32* %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %26
17: ; preds = %2
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%19 = call i32 @connect(i32 %13, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i16 zeroext %1, i32 %23)
%25 = call i32 @close(i32 %13)
br label %26
26: ; preds = %17, %21, %15
%27 = phi i32 [ -1, %15 ], [ -1, %21 ], [ %13, %17 ]
ret i32 %27
}