#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 connect(int, struct sockaddr const *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, scalar_t__, int);
int connect_w(int fd, const struct sockaddr *addr, socklen_t len) {
  int ret;
repeat_connect:
  ret = connect(fd, addr, len);
  if (ret < 0) {
    if (errno == EINTR) {
      goto repeat_connect;
    }
    printf("connect error errno=%d fd=%d\n", errno, fd);
  }
  return ret;
}


x86 assembly (clang, -O0):
<connect_w>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%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)
jge    1186 <connect_w+0x66>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    116d <connect_w+0x4d>
jmp    1132 <connect_w+0x12>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rsi
mov    -0x4(%rbp),%edx
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_w>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2ea5(%rip),%r12
mov    0x2e96(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <connect@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    117d <connect_w+0x5d>
mov    (%r12),%rsi
cmp    0x0(%r13),%rsi
je     1150 <connect_w+0x30>
lea    0xe8b(%rip),%rdi
mov    %r14d,%edx
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [30 x i8] c"connect error errno=%d fd=%d\0A\00"
define i32 @connect_w(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
br label %8
8: ; preds = %19, %3
%9 = load i32, i32* %4
%10 = load %struct.sockaddr*, %struct.sockaddr** %5
%11 = load i32, i32* %6
%12 = call i32 @connect(i32 %9, %struct.sockaddr* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %8
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %8
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i32, i32* %4
%23 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %21, i32 %22)
br label %24
24: ; preds = %20, %8
%25 = load i32, i32* %7
ret i32 %25
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [30 x i8] c"connect error errno=%d fd=%d\0A\00"
define i32 @connect_w(i32 %0, %struct.sockaddr* %1, i32 %2) {
br label %4
4: ; preds = %7, %3
%5 = tail call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
br i1 %10, label %4, label %11
11: ; preds = %7
%12 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %8, i32 %0)
br label %13
13: ; preds = %4, %11
ret i32 %5
}