#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 */

/* Type definitions */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ init_sockaddr(struct sockaddr_in *, char const *,
                                 unsigned short);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to(const char *ipaddr, unsigned short port) {
  struct sockaddr_in name;
  int sock = -1;
  int flag = 1;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    return -1;
  }
  if (!init_sockaddr(&name, ipaddr, port)) {
    printf("Could not initialise socket address\n");
    close(sock);
    return -1;
  }
  if (connect(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
    perror("connect");
    close(sock);
    return -1;
  }
  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
  return sock;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cf <connect_to+0x5f>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <connect_to+0xfe>
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
movzwl -0x12(%rbp),%edx
call   1060 <init_sockaddr@plt>
cmp    $0x0,%eax
jne    1209 <connect_to+0x99>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <connect_to+0xfe>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1244 <connect_to+0xd4>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <connect_to+0xfe>
mov    -0x1c(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1217 <connect_to+0xa7>
mov    %eax,%ebx
movzwl %bp,%edx
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1060 <init_sockaddr@plt>
test   %eax,%eax
je     11f4 <connect_to+0x84>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1225 <connect_to+0xb5>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
jmp    120c <connect_to+0x9c>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde2(%rip),%rdi
call   1070 <perror@plt>
jmp    1207 <connect_to+0x97>
lea    0xddb(%rip),%rdi
call   1070 <perror@plt>
jmp    1200 <connect_to+0x90>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [37 x i8] c"Could not initialise socket address\0A\00"
@.str.2 = [8 x i8] c"connect\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @connect_to(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
store i32 -1, i32* %7
store i32 1, i32* %8
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i16, i16* %5
%19 = call i32 @init_sockaddr(%struct.sockaddr_in* %6, i8* %17, i16 zeroext %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %3
br label %40
25: ; preds = %16
%26 = load i32, i32* %7
%27 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%28 = call i64 @connect(i32 %26, %struct.sockaddr* %27, i32 4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %3
br label %40
34: ; preds = %25
%35 = load i32, i32* %7
%36 = load i32, i32* @IPPROTO_TCP
%37 = load i32, i32* @TCP_NODELAY
%38 = call i32 @setsockopt(i32 %35, i32 %36, i32 %37, i32* %8, i32 4)
%39 = load i32, i32* %7
store i32 %39, i32* %3
br label %40
40: ; preds = %34, %30, %21, %14
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@str = [36 x i8] c"Could not initialise socket address\00"
define i32 @connect_to(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %30
13: ; preds = %2
%14 = call i32 @init_sockaddr(%struct.sockaddr_in* %3, i8* %0, i16 zeroext %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%18 = call i32 @close(i32 %9)
br label %30
19: ; preds = %13
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i64 @connect(i32 %9, %struct.sockaddr* %20, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @close(i32 %9)
br label %30
26: ; preds = %19
%27 = load i32, i32* @IPPROTO_TCP
%28 = load i32, i32* @TCP_NODELAY
%29 = call i32 @setsockopt(i32 %9, i32 %27, i32 %28, i32* %4, i32 4)
br label %30
30: ; preds = %26, %23, %16, %11
%31 = phi i32 [ -1, %11 ], [ -1, %23 ], [ %9, %26 ], [ -1, %16 ]
ret i32 %31
}