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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ connect(int, struct sockaddr *, int);
int gateSocket();
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int gateConnect(unsigned short port) {
  int sd;
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  sd = gateSocket();
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  return connect(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0 ? -1 : sd;
}


x86 assembly (clang, -O0):
<gateConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
lea    0xe8a(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <gateSocket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movzwl -0x2(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    11c8 <gateConnect+0x78>
mov    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ce <gateConnect+0x7e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gateConnect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
lea    0xe8d(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1050 <gateSocket@plt>
mov    %eax,%ebx
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
define i32 @gateConnect(i16 zeroext %0) {
%2 = alloca i16
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store i16 %0, i16* %2
%5 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%6 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%8 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %7, i32 0, i32 0
store i32 %6, i32* %8
%9 = call i32 (...) @gateSocket()
store i32 %9, i32* %3
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i16, i16* %2
%13 = call i32 @htons(i16 zeroext %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %3
%16 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%17 = call i64 @connect(i32 %15, %struct.sockaddr* %16, i32 12)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %1
br label %22
20: ; preds = %1
%21 = load i32, i32* %3
br label %22
22: ; preds = %20, %19
%23 = phi i32 [ -1, %19 ], [ %21, %20 ]
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
define i32 @gateConnect(i16 zeroext %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 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
store i32 %5, i32* %6
%7 = call i32 (...) @gateSocket()
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = call i32 @htons(i16 zeroext %0)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%13 = call i64 @connect(i32 %7, %struct.sockaddr* %12, i32 12)
%14 = icmp slt i64 %13, 0
%15 = select i1 %14, i32 -1, i32 %7
ret i32 %15
}