#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*/ socklen_t;

/* Variables and functions */
scalar_t__ EINTR;
int check_conn_is_ok(int);
int connect(int, struct sockaddr const *, int /*<<< orphan*/);
scalar_t__ errno;
int connect_safe(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
  int ret = 0;
  ret = connect(sockfd, addr, addrlen);
  if (ret) {
    if (errno == EINTR) {
      ret = check_conn_is_ok(sockfd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<connect_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     117f <connect_safe+0x5f>
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
jne    117a <connect_safe+0x5a>
mov    -0x4(%rbp),%edi
call   1030 <check_conn_is_ok@plt>
mov    %eax,-0x18(%rbp)
jmp    117f <connect_safe+0x5f>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_safe>:
push   %rbx
mov    %edi,%ebx
call   1040 <connect@plt>
test   %eax,%eax
je     114a <connect_safe+0x2a>
mov    0x2ead(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e9b(%rip),%rdx
cmp    (%rdx),%rcx
jne    114c <connect_safe+0x2c>
mov    %ebx,%edi
pop    %rbx
jmp    1030 <check_conn_is_ok@plt>
xor    %eax,%eax
pop    %rbx
ret

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

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