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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int establish_connection(void *server_addr) {
  int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock < 0) {
    return -1;
  }
  int cresult =
      connect(sock, (struct sockaddr *)server_addr, sizeof(struct sockaddr));
  if (cresult < 0) {
    return -2;
  }
  return sock;
}


x86 assembly (clang, -O0):
<establish_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
call   1040 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1165 <establish_connection+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <establish_connection+0x75>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    118f <establish_connection+0x6f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1195 <establish_connection+0x75>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establish_connection>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ebb(%rip),%rax
mov    (%rax),%edi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    0x2ea1(%rip),%rax
mov    (%rax),%edx
call   1040 <socket@plt>
test   %eax,%eax
js     1167 <establish_connection+0x47>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <connect@plt>
test   %eax,%eax
mov    $0xfffffffe,%eax
cmovns %ebp,%eax
jmp    116c <establish_connection+0x4c>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @establish_connection(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %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* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %23
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i8*, i8** %3
%16 = bitcast i8* %15 to %struct.sockaddr*
%17 = call i32 @connect(i32 %14, %struct.sockaddr* %16, i32 4)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -2, i32* %2
br label %23
21: ; preds = %13
%22 = load i32, i32* %4
store i32 %22, i32* %2
br label %23
23: ; preds = %21, %20, %12
%24 = load i32, i32* %2
ret i32 %24
}

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