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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int SOCK_CLOEXEC;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int create_connection(int port) {
  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(0x7f000001);
  sin.sin_port = htons(port);
  int new_socket =
      socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  if (new_socket < 0) {
    return -1;
  }
  int res = connect(new_socket, (struct sockaddr *)(&sin),
                    sizeof(struct sockaddr_in));
  if (res < 0 && errno != EINPROGRESS) {
    perror("Error opening connection to back-end");
    close(new_socket);
    return -1;
  }
  return new_socket;
}


x86 assembly (clang, -O0):
<create_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x7f000001,%edi
call   1040 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f3 <create_connection+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <create_connection+0xe1>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    124b <create_connection+0xdb>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
je     124b <create_connection+0xdb>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <create_connection+0xe1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_connection>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e50(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x7f000001,%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2e09(%rip),%rax
mov    0x2e0a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x0(%rbp),%edi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     120d <create_connection+0x9d>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %eax,%eax
jns    120b <create_connection+0x9b>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
jne    1216 <create_connection+0xa6>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    120d <create_connection+0x9d>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@SOCK_CLOEXEC = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [37 x i8] c"Error opening connection to back-end\00"
define i32 @create_connection(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = call i32 @htonl(i32 2130706433)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %7, i32* %8
%9 = call i32 @htons(i32 %0)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @SOCK_NONBLOCK
%14 = or i32 %13, %12
%15 = load i32, i32* @SOCK_CLOEXEC
%16 = or i32 %14, %15
%17 = call i32 @socket(i32 %11, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %30, label %19
19: ; preds = %1
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i32 @connect(i32 %17, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINPROGRESS
%26 = icmp eq i64 %24, %25
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @close(i32 %17)
br label %30
30: ; preds = %27, %23, %19, %1
%31 = phi i32 [ -1, %1 ], [ -1, %27 ], [ %17, %23 ], [ %17, %19 ]
ret i32 %31
}