#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__ {
  void *s_addr;
};
struct sockaddr_in {
  void *sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
unsigned long INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ connect(int, struct sockaddr *, int);
void *htonl(unsigned long);
void *htons(int);
unsigned long ntohl(unsigned long);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_connection(unsigned long ipaddr, int port) {
  int sock;
  unsigned long a;
  struct sockaddr_in addr;
  a = ntohl(ipaddr);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(0);
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    return -1;
  }
  addr.sin_addr.s_addr = htonl(a);
  addr.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("connect");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<make_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <ntohl@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%rdi
call   1040 <htonl@plt>
mov    %rax,-0x30(%rbp)
xor    %edi,%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    1205 <make_connection+0x95>
lea    0xe0c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <make_connection+0xe7>
mov    -0x20(%rbp),%rdi
call   1040 <htonl@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1251 <make_connection+0xe1>
lea    0xdc5(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <make_connection+0xe7>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
call   1080 <ntohl@plt>
mov    %rax,%r15
mov    0x2e5c(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%rdi
call   1040 <htonl@plt>
mov    %rax,0x8(%rsp)
xor    %edi,%edi
call   1030 <htons@plt>
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     1215 <make_connection+0xa5>
mov    %r15,%rdi
call   1040 <htonl@plt>
mov    %rax,0x8(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     121e <make_connection+0xae>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde4(%rip),%rdi
jmp    1225 <make_connection+0xb5>
lea    0xde0(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1208 <make_connection+0x98>

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i64 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [8 x i8] c"connect\00"
define i32 @make_connection(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.sockaddr_in
store i64 %0, i64* %4
store i32 %1, i32* %5
%9 = load i64, i64* %4
%10 = call i64 @ntohl(i64 %9)
store i64 %10, i64* %7
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i64, i64* @INADDR_ANY
%17 = call i8* @htonl(i64 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i8* %17, i8** %19
%20 = call i8* @htons(i32 0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i8* %20, i8** %21
%22 = load i32, i32* %6
%23 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 24)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %2
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
28: ; preds = %2
%29 = load i64, i64* %7
%30 = call i8* @htonl(i64 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i8* %30, i8** %32
%33 = load i32, i32* %5
%34 = call i8* @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i8* %34, i8** %35
%36 = load i32, i32* %6
%37 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%38 = call i64 @connect(i32 %36, %struct.sockaddr* %37, i32 24)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %28
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
42: ; preds = %28
%43 = load i32, i32* %6
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %40, %26
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i64 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [8 x i8] c"connect\00"
define i32 @make_connection(i64 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i64 @ntohl(i64 %0)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i64, i64* @INADDR_ANY
%12 = tail call i8* @htonl(i64 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i8* %12, i8** %13
%14 = tail call i8* @htons(i32 0)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i8* %14, i8** %15
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i64 @bind(i32 %8, %struct.sockaddr* %16, i32 24)
%18 = icmp slt i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %2
%20 = call i8* @htonl(i64 %5)
store i8* %20, i8** %13
%21 = call i8* @htons(i32 %1)
store i8* %21, i8** %15
%22 = call i64 @connect(i32 %8, %struct.sockaddr* %16, i32 24)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19, %2
%25 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %19 ]
%26 = call i32 @perror(i8* %25)
br label %27
27: ; preds = %24, %19
%28 = phi i32 [ %8, %19 ], [ -1, %24 ]
ret i32 %28
}