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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fnet_set_nonblocking(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int fnet_conn(const char *ip, in_port_t port, int isblock) {
  int sockfd;
  struct sockaddr_in server_addr;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    return -1;
  }
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = inet_addr(ip);
  if (connect(sockfd, (struct sockaddr *)(&server_addr),
              sizeof(struct sockaddr)) == -1) {
    return -1;
  }
  if (!isblock)
    fnet_set_nonblocking(sockfd);
  return sockfd;
}


x86 assembly (clang, -O0):
<fnet_conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11a3 <fnet_conn+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <fnet_conn+0xb4>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    11fc <fnet_conn+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <fnet_conn+0xb4>
cmpl   $0x0,-0x18(%rbp)
jne    120e <fnet_conn+0xae>
mov    -0x1c(%rbp),%edi
call   1030 <fnet_set_nonblocking@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnet_conn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%r12d
mov    %rdi,%r15
mov    0x2e6b(%rip),%r14
mov    (%r14),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r13d
cmp    $0xffffffff,%eax
je     11f9 <fnet_conn+0x99>
mov    %eax,%ebp
mov    %ebx,0x4(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %r12d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11f9 <fnet_conn+0x99>
cmpl   $0x0,0x4(%rsp)
jne    11f6 <fnet_conn+0x96>
mov    %ebp,%edi
call   1030 <fnet_set_nonblocking@plt>
mov    %ebp,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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