#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 timeval {
  int tv_sec;
  int /*<<< orphan*/ member_0;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int connect_socket(int sockfd, char *ip, int port, int timeout_recv) {
  int result = 0;
  if ((sockfd != -1) && ip) {
    struct sockaddr_in serv_addr = {0};
    struct timeval tv = {0};
    tv.tv_sec = 10;
    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
               sizeof(struct timeval));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(ip);
    serv_addr.sin_port = htons(port);
    result =
        connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) >= 0;
  }
  return result;
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     120f <connect_socket+0xbf>
cmpq   $0x0,-0x10(%rbp)
je     120f <connect_socket+0xbf>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0xa,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
setge  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
cmp    $0xffffffff,%edi
je     11cc <connect_socket+0x8c>
mov    %rsi,%rbp
test   %rsi,%rsi
je     11cc <connect_socket+0x8c>
mov    %edx,%r14d
mov    %edi,%ebx
movl   $0x0,0x14(%rsp)
movq   $0xa,0x18(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rbp,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1060 <connect@plt>
shr    $0x3f,%rax
xor    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@AF_INET = global i32 0
define i32 @connect_socket(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %40
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %40
17: ; preds = %14
%18 = bitcast %struct.sockaddr_in* %10 to i8*
%19 = bitcast %struct.timeval* %11 to i8*
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 10, i32* %20
%21 = load i32, i32* %5
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_RCVTIMEO
%24 = bitcast %struct.timeval* %11 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 8)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i8*, i8** %6
%29 = call i32 @inet_addr(i8* %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* %7
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %5
%36 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%37 = call i64 @connect(i32 %35, %struct.sockaddr* %36, i32 16)
%38 = icmp sge i64 %37, 0
%39 = zext i1 %38 to i32
store i32 %39, i32* %9
br label %40
40: ; preds = %17, %14, %4
%41 = load i32, i32* %9
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@AF_INET = global i32 0
define i32 @connect_socket(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i64
%7 = icmp ne i32 %0, -1
%8 = icmp ne i8* %1, null
%9 = and i1 %7, %8
br i1 %9, label %10, label %28
10: ; preds = %4
%11 = bitcast %struct.sockaddr_in* %5 to i8*
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 3
store i32 0, i32* %12
%13 = bitcast i64* %6 to i8*
store i64 10, i64* %6
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_RCVTIMEO
%16 = call i32 @setsockopt(i32 %0, i32 %14, i32 %15, i8* %13, i32 8)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @inet_addr(i8* %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %2)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i64 @connect(i32 %0, %struct.sockaddr* %23, i32 16)
%25 = lshr i64 %24, 63
%26 = trunc i64 %25 to i32
%27 = xor i32 %26, 1
br label %28
28: ; preds = %10, %4
%29 = phi i32 [ %27, %10 ], [ 0, %4 ]
ret i32 %29
}