#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 {
  scalar_t__ sin_port;
  TYPE_1__ sin_addr;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
struct linger {
  scalar_t__ l_linger;
  scalar_t__ l_onoff;
};

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ SO_LINGER;
int /*<<< orphan*/ SO_OOBINLINE;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ htons(int);
scalar_t__ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int lw_sock_connect(int *sock, char *ip, int port) {
  int result = 0;
  struct sockaddr_in name;
  int enable = 1;
  int disable = 0;
  struct linger li;
  *sock = socket(AF_INET, SOCK_STREAM, 0);
  if (*sock >= 0) {
    name.sin_family = AF_INET;
    name.sin_addr.s_addr = INADDR_ANY;
    name.sin_port = 0;
    if (bind(*sock, (struct sockaddr *)&name, sizeof name) >= 0) {
      name.sin_family = AF_INET;
      if (inet_aton(ip, &name.sin_addr) != 0) {
        name.sin_port = htons(port);
        if (connect((*sock), (struct sockaddr *)&name, sizeof name) >= 0) {
          li.l_onoff = 0;
          li.l_linger = 0;
          setsockopt(*sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&enable,
                     sizeof(int));
          setsockopt(*sock, SOL_SOCKET, SO_OOBINLINE, (char *)&disable,
                     sizeof(int));
          setsockopt(*sock, SOL_SOCKET, SO_LINGER, (char *)&li,
                     sizeof(struct linger));
          fcntl(*sock, F_SETFL, O_NONBLOCK, 0);
          result = 1;
        }
      }
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<lw_sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jl     1314 <lw_sock_connect+0x1a4>
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jl     130f <lw_sock_connect+0x19f>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
call   1060 <inet_aton@plt>
cmp    $0x0,%rax
je     130a <lw_sock_connect+0x19a>
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jl     1305 <lw_sock_connect+0x195>
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
mov    0x2d18(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    0x2d01(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
mov    0x2d02(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2ccc(%rip),%rax
mov    (%rax),%esi
mov    0x2ce3(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1040 <fcntl@plt>
movl   $0x1,-0x18(%rbp)
jmp    130a <lw_sock_connect+0x19a>
jmp    130f <lw_sock_connect+0x19f>
jmp    1314 <lw_sock_connect+0x1a4>
mov    -0x18(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lw_sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
movl   $0x1,0x4(%rsp)
movl   $0x0,(%rsp)
mov    0x2e45(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,(%r15)
test   %eax,%eax
js     12c1 <lw_sock_connect+0x151>
mov    (%rbx),%rcx
mov    %rcx,0x18(%rsp)
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x18,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     12c1 <lw_sock_connect+0x151>
lea    0x10(%rsp),%rsi
mov    (%rbx),%rax
mov    %rax,0x18(%rsp)
mov    %r12,%rdi
call   1060 <inet_aton@plt>
test   %rax,%rax
je     12c1 <lw_sock_connect+0x151>
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %rax,0x8(%rsp)
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
mov    $0x18,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     12c1 <lw_sock_connect+0x151>
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
mov    (%r15),%edi
mov    0x2d9d(%rip),%rbx
mov    (%rbx),%esi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r15),%edi
mov    (%rbx),%esi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r15),%edi
mov    (%rbx),%esi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    (%r15),%edi
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1040 <fcntl@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.linger = type { i64, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_OOBINLINE = global i32 0
@SO_LINGER = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @lw_sock_connect(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.linger
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 1, i32* %9
store i32 0, i32* %10
%12 = load i8*, i8** @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i8* %12, i32 %13, i32 0)
%15 = load i32*, i32** %4
store i32 %14, i32* %15
%16 = load i32*, i32** %4
%17 = load i32, i32* %16
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %76
19: ; preds = %3
%20 = load i8*, i8** @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i8* %20, i8** %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i64 0, i64* %25
%26 = load i32*, i32** %4
%27 = load i32, i32* %26
%28 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 24)
%30 = icmp sge i64 %29, 0
br i1 %30, label %31, label %75
31: ; preds = %19
%32 = load i8*, i8** @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i8* %32, i8** %33
%34 = load i8*, i8** %5
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%36 = call i64 @inet_aton(i8* %34, %struct.TYPE_2__* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %74
38: ; preds = %31
%39 = load i32, i32* %6
%40 = call i64 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i64 %40, i64* %41
%42 = load i32*, i32** %4
%43 = load i32, i32* %42
%44 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%45 = call i64 @connect(i32 %43, %struct.sockaddr* %44, i32 24)
%46 = icmp sge i64 %45, 0
br i1 %46, label %47, label %73
47: ; preds = %38
%48 = getelementptr inbounds %struct.linger, %struct.linger* %11, i32 0, i32 1
store i64 0, i64* %48
%49 = getelementptr inbounds %struct.linger, %struct.linger* %11, i32 0, i32 0
store i64 0, i64* %49
%50 = load i32*, i32** %4
%51 = load i32, i32* %50
%52 = load i32, i32* @SOL_SOCKET
%53 = load i32, i32* @SO_KEEPALIVE
%54 = bitcast i32* %9 to i8*
%55 = call i32 @setsockopt(i32 %51, i32 %52, i32 %53, i8* %54, i32 4)
%56 = load i32*, i32** %4
%57 = load i32, i32* %56
%58 = load i32, i32* @SOL_SOCKET
%59 = load i32, i32* @SO_OOBINLINE
%60 = bitcast i32* %10 to i8*
%61 = call i32 @setsockopt(i32 %57, i32 %58, i32 %59, i8* %60, i32 4)
%62 = load i32*, i32** %4
%63 = load i32, i32* %62
%64 = load i32, i32* @SOL_SOCKET
%65 = load i32, i32* @SO_LINGER
%66 = bitcast %struct.linger* %11 to i8*
%67 = call i32 @setsockopt(i32 %63, i32 %64, i32 %65, i8* %66, i32 16)
%68 = load i32*, i32** %4
%69 = load i32, i32* %68
%70 = load i32, i32* @F_SETFL
%71 = load i32, i32* @O_NONBLOCK
%72 = call i32 @fcntl(i32 %69, i32 %70, i32 %71, i32 0)
store i32 1, i32* %7
br label %73
73: ; preds = %47, %38
br label %74
74: ; preds = %73, %31
br label %75
75: ; preds = %74, %19
br label %76
76: ; preds = %75, %3
%77 = load i32, i32* %7
ret i32 %77
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.linger = type { i64, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_OOBINLINE = global i32 0
@SO_LINGER = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @lw_sock_connect(i32* nocapture %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.linger
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%10 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%11 = bitcast %struct.linger* %7 to i8*
%12 = load i8*, i8** @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i8* %12, i32 %13, i32 0)
store i32 %14, i32* %0
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %53
16: ; preds = %3
%17 = load i8*, i8** @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i8* %17, i8** %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i64 0, i32 0
store i32 %19, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i64 0, i64* %22
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i64 @bind(i32 %14, %struct.sockaddr* %23, i32 24)
%25 = icmp sgt i64 %24, -1
br i1 %25, label %26, label %53
26: ; preds = %16
%27 = load i8*, i8** @AF_INET
store i8* %27, i8** %18
%28 = call i64 @inet_aton(i8* %1, %struct.TYPE_2__* %20)
%29 = icmp eq i64 %28, 0
br i1 %29, label %53, label %30
30: ; preds = %26
%31 = call i64 @htons(i32 %2)
store i64 %31, i64* %22
%32 = load i32, i32* %0
%33 = call i64 @connect(i32 %32, %struct.sockaddr* %23, i32 24)
%34 = icmp sgt i64 %33, -1
br i1 %34, label %35, label %53
35: ; preds = %30
%36 = bitcast %struct.linger* %7 to i8*
%37 = load i32, i32* %0
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_KEEPALIVE
%40 = call i32 @setsockopt(i32 %37, i32 %38, i32 %39, i8* %9, i32 4)
%41 = load i32, i32* %0
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_OOBINLINE
%44 = call i32 @setsockopt(i32 %41, i32 %42, i32 %43, i8* %10, i32 4)
%45 = load i32, i32* %0
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_LINGER
%48 = call i32 @setsockopt(i32 %45, i32 %46, i32 %47, i8* %11, i32 16)
%49 = load i32, i32* %0
%50 = load i32, i32* @F_SETFL
%51 = load i32, i32* @O_NONBLOCK
%52 = call i32 @fcntl(i32 %49, i32 %50, i32 %51, i32 0)
br label %53
53: ; preds = %16, %30, %35, %26, %3
%54 = phi i32 [ 1, %35 ], [ 0, %30 ], [ 0, %26 ], [ 0, %16 ], [ 0, %3 ]
ret i32 %54
}