#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 */
struct sockaddr {
  int /*<<< orphan*/ sa_family;
};

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int MAXConnect(struct sockaddr *sa) {
  int sockfd;
  if ((sockfd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  if (connect(sockfd, sa, sizeof(*sa)) < 0) {
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<MAXConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    115a <MAXConnect+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1187 <MAXConnect+0x67>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <connect@plt>
cmp    $0x0,%rax
jge    1181 <MAXConnect+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1187 <MAXConnect+0x67>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MAXConnect>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%edi
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
test   %eax,%eax
js     115a <MAXConnect+0x3a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebp,%eax
jmp    115f <MAXConnect+0x3f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
define i32 @MAXConnect(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr*
%4 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %3
%5 = load %struct.sockaddr*, %struct.sockaddr** %3
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i32 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %20
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load %struct.sockaddr*, %struct.sockaddr** %3
%15 = call i64 @connect(i32 %13, %struct.sockaddr* %14, i32 4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %2
br label %20
18: ; preds = %12
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %20
20: ; preds = %18, %17, %11
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
define i32 @MAXConnect(%struct.sockaddr* %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = tail call i64 @connect(i32 %5, %struct.sockaddr* %0, i32 4)
%9 = icmp slt i64 %8, 0
%10 = select i1 %9, i32 -1, i32 %5
br label %11
11: ; preds = %7, %1
%12 = phi i32 [ -1, %1 ], [ %10, %7 ]
ret i32 %12
}