#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 */
typedef int /*<<< orphan*/ tcpflush;
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*/ smallwindow;
typedef int /*<<< orphan*/ recvbufsz;
typedef int /*<<< orphan*/ mss;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
int /*<<< orphan*/ TCP_MAXSEG;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ TCP_WINDOW_CLAMP;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              unsigned int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_socket(void) {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  unsigned int tcpflush = 1;
  unsigned int recvbufsz = 512;
  unsigned int smallwindow = 512;
  unsigned int mss = 1024;
  setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recvbufsz, sizeof(recvbufsz));
  setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &recvbufsz, sizeof(recvbufsz));
  setsockopt(sock, IPPROTO_IP, TCP_NODELAY, &tcpflush, sizeof(tcpflush));
  setsockopt(sock, IPPROTO_IP, TCP_MAXSEG, &mss, sizeof(mss));
  setsockopt(sock, IPPROTO_IP, TCP_WINDOW_CLAMP, &smallwindow,
             sizeof(smallwindow));
  struct sockaddr_in ipaddr;
  in_addr_t hostip = inet_addr("192.168.0.1");
  ipaddr.sin_family = AF_INET;
  ipaddr.sin_port = htons(80);
  ipaddr.sin_addr.s_addr = hostip;
  if (connect(sock, (struct sockaddr *)&ipaddr, sizeof(struct sockaddr)) < 0) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x200,-0x10(%rbp)
movl   $0x200,-0x14(%rbp)
movl   $0x400,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0xdb5(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1299 <open_socket+0x149>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <open_socket+0x14f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e77(%rip),%r15
mov    (%r15),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
movl   $0x1,0x14(%rsp)
movl   $0x200,0x10(%rsp)
movl   $0x200,0xc(%rsp)
movl   $0x400,0x8(%rsp)
mov    0x2e4b(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2de7(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0xdbe(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x1c(%rsp)
mov    %ebp,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
add    $0x28,%rsp
pop    %rbx
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
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@SO_SNDBUF = global i32 0
@IPPROTO_IP = global i32 0
@TCP_NODELAY = global i32 0
@TCP_MAXSEG = global i32 0
@TCP_WINDOW_CLAMP = global i32 0
@.str = [12 x i8] c"192.168.0.1\00"
define i32 @open_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%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* %2
store i32 1, i32* %3
store i32 512, i32* %4
store i32 512, i32* %5
store i32 1024, i32* %6
%12 = load i32, i32* %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_RCVBUF
%15 = call i32 @setsockopt(i32 %12, i32 %13, i32 %14, i32* %4, i32 4)
%16 = load i32, i32* %2
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_SNDBUF
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %4, i32 4)
%20 = load i32, i32* %2
%21 = load i32, i32* @IPPROTO_IP
%22 = load i32, i32* @TCP_NODELAY
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %3, i32 4)
%24 = load i32, i32* %2
%25 = load i32, i32* @IPPROTO_IP
%26 = load i32, i32* @TCP_MAXSEG
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %6, i32 4)
%28 = load i32, i32* %2
%29 = load i32, i32* @IPPROTO_IP
%30 = load i32, i32* @TCP_WINDOW_CLAMP
%31 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %5, i32 4)
%32 = call i32 @inet_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 %32, i32* %8
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %33, i32* %34
%35 = call i32 @htons(i32 80)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* %8
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %2
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i64 @connect(i32 %40, %struct.sockaddr* %41, i32 4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %0
store i32 -1, i32* %1
br label %47
45: ; preds = %0
%46 = load i32, i32* %2
store i32 %46, i32* %1
br label %47
47: ; preds = %45, %44
%48 = load i32, i32* %1
ret i32 %48
}

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
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@SO_SNDBUF = global i32 0
@IPPROTO_IP = global i32 0
@TCP_NODELAY = global i32 0
@TCP_MAXSEG = global i32 0
@TCP_WINDOW_CLAMP = global i32 0
@.str = [12 x i8] c"192.168.0.1\00"
define i32 @open_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%10 = bitcast i32* %2 to i8*
store i32 512, i32* %2
%11 = bitcast i32* %3 to i8*
store i32 512, i32* %3
%12 = bitcast i32* %4 to i8*
store i32 1024, i32* %4
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_RCVBUF
%15 = call i32 @setsockopt(i32 %8, i32 %13, i32 %14, i32* %2, i32 4)
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_SNDBUF
%18 = call i32 @setsockopt(i32 %8, i32 %16, i32 %17, i32* %2, i32 4)
%19 = load i32, i32* @IPPROTO_IP
%20 = load i32, i32* @TCP_NODELAY
%21 = call i32 @setsockopt(i32 %8, i32 %19, i32 %20, i32* %1, i32 4)
%22 = load i32, i32* @IPPROTO_IP
%23 = load i32, i32* @TCP_MAXSEG
%24 = call i32 @setsockopt(i32 %8, i32 %22, i32 %23, i32* %4, i32 4)
%25 = load i32, i32* @IPPROTO_IP
%26 = load i32, i32* @TCP_WINDOW_CLAMP
%27 = call i32 @setsockopt(i32 %8, i32 %25, i32 %26, i32* %3, i32 4)
%28 = bitcast %struct.sockaddr_in* %5 to i8*
%29 = call i32 @inet_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %30, i32* %31
%32 = call i32 @htons(i32 80)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %29, i32* %34
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i64 @connect(i32 %8, %struct.sockaddr* %35, i32 4)
%37 = icmp slt i64 %36, 0
%38 = select i1 %37, i32 -1, i32 %8
ret i32 %38
}