#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*/ socklen_t;
typedef int /*<<< orphan*/ dest;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_socket(const char *ip, struct sockaddr_in *dest, int port) {
  int my_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (my_socket < 0) {
    perror("socket allocation failed");
    return -1;
  }
  memset(dest, 0, sizeof(dest));
  dest->sin_family = AF_INET;
  dest->sin_port = htons(port);
  dest->sin_addr.s_addr = inet_addr(ip);
  if (connect(my_socket, (struct sockaddr *)dest, (socklen_t)sizeof(*dest)) ==
      -1)
    return -1;
  return my_socket;
}


x86 assembly (clang, -O0):
<connect_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11b1 <connect_to_socket+0x51>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <connect_to_socket+0xbf>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1219 <connect_to_socket+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <connect_to_socket+0xbf>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e71(%rip),%r12
mov    (%r12),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11de <connect_to_socket+0x7e>
mov    %eax,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%r14)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%r14)
mov    %r15,%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%r14)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11d3 <connect_to_socket+0x73>

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

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 = [25 x i8] c"socket allocation failed\00"
define i32 @connect_to_socket(i8* %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %22
10: ; preds = %3
%11 = tail call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 8)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %2)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call i32 @inet_addr(i8* %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%19 = tail call i32 @connect(i32 %6, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, -1
%21 = select i1 %20, i32 -1, i32 %6
br label %22
22: ; preds = %10, %8
%23 = phi i32 [ -1, %8 ], [ %21, %10 ]
ret i32 %23
}