#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 in6_addr {
  int dummy;
};
struct sockaddr_in6 {
  int sin6_port;
  struct in6_addr sin6_addr;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
scalar_t__ EALREADY;
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ memset(struct in6_addr *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int s, r;
  struct in6_addr foo;
  struct sockaddr_in6 bar;
  memset(&foo, 0, sizeof(struct sockaddr_in6));
  bar.sin6_addr = foo;
  bar.sin6_port = 1026;
  s = socket(AF_INET6, SOCK_STREAM, 0);
  if (s < 0)
    return 111;
  do
    r = connect(s, (struct sockaddr *)&bar, sizeof bar);
  while ((r == -1) && (errno == EINTR));
  if ((r == -1) && (errno == EALREADY))
    errno = EINPROGRESS;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x402,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118e <main+0x5e>
movl   $0x6f,-0x4(%rbp)
jmp    121a <main+0xea>
jmp    1193 <main+0x63>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1040 <connect@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0x19(%rbp)
jne    11d0 <main+0xa0>
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1193 <main+0x63>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1213 <main+0xe3>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <main+0xe3>
mov    0x2de2(%rip),%rax
mov    (%rax),%rcx
mov    0x2db0(%rip),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x14(%rsp)
movl   $0x402,0x10(%rsp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
test   %eax,%eax
js     11d1 <main+0xa1>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    0x2e39(%rip),%r15
mov    0x2e2a(%rip),%r12
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1040 <connect@plt>
mov    %eax,%ecx
mov    (%r15),%rdx
cmp    $0xffffffff,%eax
jne    11af <main+0x7f>
cmp    (%r12),%rdx
je     1190 <main+0x60>
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11d6 <main+0xa6>
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rdx
jne    11d6 <main+0xa6>
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%r15)
jmp    11d6 <main+0xa6>
mov    $0x6f,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
%struct.sockaddr_in6 = type { i32, %struct.in6_addr }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EALREADY = global i64 0
@EINPROGRESS = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.in6_addr
%5 = alloca %struct.sockaddr_in6
store i32 0, i32* %1
%6 = call i32 @memset(%struct.in6_addr* %4, i32 0, i32 8)
%7 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
%8 = bitcast %struct.in6_addr* %7 to i8*
%9 = bitcast %struct.in6_addr* %4 to i8*
%10 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
store i32 1026, i32* %10
%11 = load i32, i32* @AF_INET6
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %0
store i32 111, i32* %1
br label %41
17: ; preds = %0
br label %18
18: ; preds = %29, %17
%19 = load i32, i32* %2
%20 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%21 = call i32 @connect(i32 %19, %struct.sockaddr* %20, i32 8)
store i32 %21, i32* %3
br label %22
22: ; preds = %18
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br label %29
29: ; preds = %25, %22
%30 = phi i1 [ false, %22 ], [ %28, %25 ]
br i1 %30, label %18, label %31
31: ; preds = %29
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EALREADY
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i64, i64* @EINPROGRESS
store i64 %39, i64* @errno
br label %40
40: ; preds = %38, %34, %31
store i32 0, i32* %1
br label %41
41: ; preds = %40, %16
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
%struct.sockaddr_in6 = type { i32, %struct.in6_addr }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EALREADY = global i64 0
@EINPROGRESS = global i64 0
define i32 @main() {
%1 = alloca %struct.in6_addr
%2 = alloca %struct.sockaddr_in6
%3 = bitcast %struct.in6_addr* %1 to i8*
%4 = bitcast %struct.sockaddr_in6* %2 to i8*
%5 = call i32 @memset(%struct.in6_addr* %1, i32 0, i32 8)
%6 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %1, i64 0, i32 0
%7 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1, i32 0
%8 = load i32, i32* %6
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 1026, i32* %9
%10 = load i32, i32* @AF_INET6
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %29, label %14
14: ; preds = %0
%15 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
br label %16
16: ; preds = %14, %16
%17 = call i32 @connect(i32 %12, %struct.sockaddr* %15, i32 8)
%18 = icmp eq i32 %17, -1
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %16, label %23
23: ; preds = %16
%24 = load i64, i64* @EALREADY
%25 = icmp eq i64 %19, %24
%26 = select i1 %18, i1 %25, i1 false
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i64, i64* @EINPROGRESS
store i64 %28, i64* @errno
br label %29
29: ; preds = %23, %27, %0
%30 = phi i32 [ 111, %0 ], [ 0, %27 ], [ 0, %23 ]
ret i32 %30
}