#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 */
typedef int uint32_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ srv;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_perror(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int32_t tcp_connect(uint32_t port) {
  struct sockaddr_in srv;
  int32_t sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sockfd == -1) {
    log_perror("nyx: socket");
    return 0;
  }
  memset(&srv, 0, sizeof(struct sockaddr_in));
  srv.sin_family = AF_INET;
  srv.sin_port = htons(port ? port : 443);
  if (!inet_pton(AF_INET, "127.0.0.1", &srv.sin_addr))
    return 0;
  if (connect(sockfd, (struct sockaddr *)&srv, sizeof(srv)) == 0)
    return sockfd;
  else
    log_perror("nyx: connect");
  return 0;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b0 <tcp_connect+0x50>
lea    0xe61(%rip),%rdi
call   1060 <log_perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1260 <tcp_connect+0x100>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11e1 <tcp_connect+0x81>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11ee <tcp_connect+0x8e>
mov    $0x1bb,%eax
mov    %eax,-0x20(%rbp)
jmp    11ee <tcp_connect+0x8e>
mov    -0x20(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1050 <inet_pton@plt>
cmp    $0x0,%eax
jne    1227 <tcp_connect+0xc7>
movl   $0x0,-0x4(%rbp)
jmp    1260 <tcp_connect+0x100>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    124d <tcp_connect+0xed>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1260 <tcp_connect+0x100>
lea    0xdc2(%rip),%rdi
call   1060 <log_perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e73(%rip),%r12
mov    (%r12),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11ff <tcp_connect+0x9f>
mov    %eax,%r14d
mov    %rsp,%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
test   %ebx,%ebx
mov    $0x1bb,%edi
cmovne %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%edi
lea    0xe3a(%rip),%rsi
mov    %r15,%rdx
call   1050 <inet_pton@plt>
test   %eax,%eax
je     120d <tcp_connect+0xad>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %r14d,%ebp
test   %rax,%rax
je     120d <tcp_connect+0xad>
lea    0xe19(%rip),%rdi
jmp    1206 <tcp_connect+0xa6>
lea    0xdfa(%rip),%rdi
call   1060 <log_perror@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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