#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 uint16_t;
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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ECONNREFUSED;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int lux_sync(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int lux_network_open(const char *address, uint16_t port) {
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof addr);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr(address);
  addr.sin_port = htons(port);
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
    return -1;
  int rc = connect(sock, (struct sockaddr *)&addr, sizeof addr);
  if (rc < 0)
    return -1;
  rc = lux_sync(sock, 10);
  if (rc != 0) {
    printf("[debug] line "
           "95"
           ": "
           "No response from %s:%d"
           "\n",
           address, port);
    close(sock);
    errno = ECONNREFUSED;
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<lux_network_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f4 <lux_network_open+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <lux_network_open+0xf7>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    121e <lux_network_open+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <lux_network_open+0xf7>
mov    -0x24(%rbp),%edi
mov    $0xa,%esi
call   1030 <lux_sync@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1271 <lux_network_open+0xf1>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xdba(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%ecx
mov    0x2d65(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <lux_network_open+0xf7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lux_network_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e40(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
call   1080 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1237 <lux_network_open+0xb7>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %eax,%eax
js     1237 <lux_network_open+0xb7>
mov    %ebp,%edi
mov    $0xa,%esi
call   1030 <lux_sync@plt>
test   %eax,%eax
je     1235 <lux_network_open+0xb5>
lea    0xdf1(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2d97(%rip),%rcx
mov    %eax,(%rcx)
jmp    1237 <lux_network_open+0xb7>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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_DGRAM = global i32 0
@.str = [41 x i8] c"[debug] line 95: No response from %s:%d\0A\00"
@ECONNREFUSED = global i32 0
@errno = global i32 0
define i32 @lux_network_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i8*, i8** %4
%13 = call i32 @inet_addr(i8* %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %5
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
br label %46
25: ; preds = %2
%26 = load i32, i32* %7
%27 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%28 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 12)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %3
br label %46
32: ; preds = %25
%33 = load i32, i32* %7
%34 = call i32 @lux_sync(i32 %33, i32 10)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %38, i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* @ECONNREFUSED
store i32 %43, i32* @errno
store i32 -1, i32* %3
br label %46
44: ; preds = %32
%45 = load i32, i32* %7
store i32 %45, i32* %3
br label %46
46: ; preds = %44, %37, %31, %24
%47 = load i32, i32* %3
ret i32 %47
}

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_DGRAM = global i32 0
@.str = [41 x i8] c"[debug] line 95: No response from %s:%d\0A\00"
@ECONNREFUSED = global i32 0
@errno = global i32 0
define i32 @lux_network_open(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = call i32 @inet_addr(i8* %0)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %8, i32* %9
%10 = call i32 @htons(i32 %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %2
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @connect(i32 %14, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %16
%21 = call i32 @lux_sync(i32 %14, i32 10)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%25 = call i32 @close(i32 %14)
%26 = load i32, i32* @ECONNREFUSED
store i32 %26, i32* @errno
br label %27
27: ; preds = %23, %16, %20, %2
%28 = phi i32 [ -1, %2 ], [ -1, %23 ], [ -1, %16 ], [ %14, %20 ]
ret i32 %28
}