#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;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
scalar_t__ sock_init(struct sockaddr *, char const *, int);
int sock_connect(int sock, const char *hostname, int port) {
  struct sockaddr saddr;
  if (sock_init(&saddr, hostname, port) != 0)
    return -1;
connect_sock_restart:
  if (connect(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    if (errno == EINTR)
      goto connect_sock_restart;
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1030 <sock_init@plt>
cmp    $0x0,%rax
je     1158 <sock_connect+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <sock_connect+0x89>
jmp    115d <sock_connect+0x3d>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
jge    11a3 <sock_connect+0x83>
mov    0x2e61(%rip),%rax
mov    (%rax),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jne    1197 <sock_connect+0x77>
jmp    115d <sock_connect+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <sock_connect+0x89>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
call   1030 <sock_init@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    1183 <sock_connect+0x63>
lea    0x8(%rsp),%r15
mov    0x2e91(%rip),%r12
mov    0x2e82(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <connect@plt>
test   %rax,%rax
jns    1180 <sock_connect+0x60>
mov    (%r12),%rax
cmp    0x0(%rbp),%rax
je     1160 <sock_connect+0x40>
jmp    1183 <sock_connect+0x63>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_connect(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %6
%10 = load i32, i32* %7
%11 = call i64 @sock_init(%struct.sockaddr* %8, i8* %9, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %27
14: ; preds = %3
br label %15
15: ; preds = %23, %14
%16 = load i32, i32* %5
%17 = call i64 @connect(i32 %16, %struct.sockaddr* %8, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %15
24: ; preds = %19
store i32 -1, i32* %4
br label %27
25: ; preds = %15
%26 = load i32, i32* %5
store i32 %26, i32* %4
br label %27
27: ; preds = %25, %24, %13
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_connect(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr
%5 = bitcast %struct.sockaddr* %4 to i8*
%6 = call i64 @sock_init(%struct.sockaddr* %4, i8* %1, i32 %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %3, %11
%9 = call i64 @connect(i32 %0, %struct.sockaddr* %4, i32 4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %8, label %15
15: ; preds = %8, %11, %3
%16 = phi i32 [ -1, %3 ], [ %0, %8 ], [ -1, %11 ]
ret i32 %16
}