#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_in6 {
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ MSG_PEEK;
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int wait_for_sender(int sfd) {
  char buf[520];
  int nread;
  struct sockaddr_in6 peer_addr;
  peer_addr.sin6_family = AF_INET6;
  socklen_t peer_addr_len = sizeof(struct sockaddr_in6);
  nread = recvfrom(sfd, buf, 520, MSG_PEEK, (struct sockaddr *)&peer_addr,
                   &peer_addr_len);
  if (nread >= 0) {
    if (connect(sfd, (const struct sockaddr *)&peer_addr, peer_addr_len) == 0) {
      return (0);
    }
    return (-1);
  } else {
    perror("recvfrom()");
    return (-1);
  }
}


x86 assembly (clang, -O0):
<wait_for_sender>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x218(%rbp)
movl   $0x4,-0x21c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
lea    -0x218(%rbp),%r8
mov    $0x208,%edx
lea    -0x21c(%rbp),%r9
call   1030 <recvfrom@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jl     11cc <wait_for_sender+0x9c>
mov    -0x8(%rbp),%edi
lea    -0x218(%rbp),%rsi
mov    -0x21c(%rbp),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jne    11c0 <wait_for_sender+0x90>
movl   $0x0,-0x4(%rbp)
jmp    11df <wait_for_sender+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <wait_for_sender+0xaf>
lea    0xe2d(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_sender>:
push   %rbx
sub    $0x220,%rsp
mov    %edi,%ebx
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movl   $0x4,0xc(%rsp)
mov    0x2e92(%rip),%rax
mov    (%rax),%ecx
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
mov    $0x208,%edx
call   1030 <recvfrom@plt>
test   %eax,%eax
js     1193 <wait_for_sender+0x63>
mov    0xc(%rsp),%edx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <connect@plt>
neg    %rax
sbb    %eax,%eax
add    $0x220,%rsp
pop    %rbx
ret
lea    0xe66(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    118a <wait_for_sender+0x5a>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@MSG_PEEK = global i32 0
@.str = [11 x i8] c"recvfrom()\00"
define i32 @wait_for_sender(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [520 x i8]
%5 = alloca i32
%6 = alloca %struct.sockaddr_in6
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @AF_INET6
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
store i32 %8, i32* %9
store i32 4, i32* %7
%10 = load i32, i32* %3
%11 = getelementptr inbounds [520 x i8], [520 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* @MSG_PEEK
%13 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%14 = call i32 @recvfrom(i32 %10, i8* %11, i32 520, i32 %12, %struct.sockaddr* %13, i32* %7)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %1
%18 = load i32, i32* %3
%19 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%20 = load i32, i32* %7
%21 = call i64 @connect(i32 %18, %struct.sockaddr* %19, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 0, i32* %2
br label %27
24: ; preds = %17
store i32 -1, i32* %2
br label %27
25: ; preds = %1
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
27: ; preds = %25, %24, %23
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@MSG_PEEK = global i32 0
@.str = [11 x i8] c"recvfrom()\00"
define i32 @wait_for_sender(i32 %0) {
%2 = alloca [520 x i8]
%3 = alloca %struct.sockaddr_in6
%4 = alloca i32
%5 = getelementptr inbounds [520 x i8], [520 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in6* %3 to i8*
%7 = load i32, i32* @AF_INET6
%8 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 %7, i32* %8
%9 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%10 = load i32, i32* @MSG_PEEK
%11 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%12 = call i32 @recvfrom(i32 %0, i8* %5, i32 520, i32 %10, %struct.sockaddr* %11, i32* %4)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i64 @connect(i32 %0, %struct.sockaddr* %11, i32 %15)
%17 = icmp ne i64 %16, 0
%18 = sext i1 %17 to i32
br label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %14, %19
%22 = phi i32 [ -1, %19 ], [ %18, %14 ]
ret i32 %22
}