#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 */
typedef int /*<<< orphan*/ uint8_t;
struct timeval {
  unsigned int tv_sec;
  int /*<<< orphan*/ member_0;
};
struct sockaddr_storage {
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ str_address;
typedef int ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ recv_address;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
char *formatAddress(struct sockaddr *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
             struct sockaddr *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(int /*<<< orphan*/);
ssize_t recv_peer_offer(int s, unsigned int secs, uint8_t msg[1500]) {
  struct timeval timeout = {0};
  fd_set readfds;
  ssize_t received;
  struct sockaddr_storage recv_address = {0};
  socklen_t recv_address_len = sizeof(recv_address);
  char str_address[46 + 6] = {0};
  FD_ZERO(&readfds);
  FD_SET(s, &readfds);
  printf("Waiting for Peer Offer signal\n");
  timeout.tv_sec = secs;
  if (select(s + 1, &readfds, NULL, NULL, &timeout) == -1) {
    printf("Failed to wait for peer discovery signal: %s\n", strerror(errno));
    return -1;
  }
  if (!FD_ISSET(s, &readfds))
    return 0;
  received = recvfrom(s, msg, 1500, 0, (struct sockaddr *)&recv_address,
                      &recv_address_len);
  if (received == -1) {
    printf("Failed to receive from UDP socket: %s\n", strerror(errno));
    return -1;
  }
  printf("Received possible Peer Offer signal (%u bytes) from %s\n",
         (unsigned int)received,
         formatAddress((struct sockaddr *)&recv_address, str_address,
                       sizeof(str_address)));
  return received;
}


x86 assembly (clang, -O0):
<recv_peer_offer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
movl   $0x4,-0x34(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x34,%edx
call   1060 <memset@plt>
lea    -0x24(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10b0 <FD_SET@plt>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
cmp    $0xffffffff,%eax
jne    1253 <recv_peer_offer+0xc3>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <recv_peer_offer+0x169>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1274 <recv_peer_offer+0xe4>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <recv_peer_offer+0x169>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x5dc,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12c7 <recv_peer_offer+0x137>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <recv_peer_offer+0x169>
mov    -0x28(%rbp),%eax
mov    %eax,-0x74(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0x34,%edx
call   1030 <formatAddress@plt>
mov    -0x74(%rbp),%esi
mov    %rax,%rdx
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_peer_offer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
movq   $0x0,0x18(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x4,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movl   $0x0,0x50(%rsp)
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
lea    0xe9a(%rip),%rdi
call   1040 <puts@plt>
mov    %r15d,0x18(%rsp)
lea    0x1(%rbx),%edi
xor    %r15d,%r15d
lea    0x18(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
cmp    $0xffffffff,%eax
je     127a <recv_peer_offer+0xea>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
call   1050 <FD_ISSET@plt>
test   %eax,%eax
je     12b6 <recv_peer_offer+0x126>
lea    0x8(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1291 <recv_peer_offer+0x101>
mov    %eax,%r15d
lea    0x8(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    $0x34,%edx
call   1030 <formatAddress@plt>
lea    0xdea(%rip),%rdi
mov    %r15d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12b6 <recv_peer_offer+0x126>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd71(%rip),%rdi
jmp    12a6 <recv_peer_offer+0x116>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd88(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [31 x i8] c"Waiting for Peer Offer signal\0A\00"
@.str.1 = [46 x i8] c"Failed to wait for peer discovery signal: %s\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Failed to receive from UDP socket: %s\0A\00"
@.str.3 = [56 x i8] c"Received possible Peer Offer signal (%u bytes) from %s\0A\00"
define i32 @recv_peer_offer(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_storage
%12 = alloca i32
%13 = alloca [52 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%14 = bitcast %struct.timeval* %8 to i8*
%15 = bitcast %struct.sockaddr_storage* %11 to i8*
store i32 4, i32* %12
%16 = bitcast [52 x i8]* %13 to i8*
%17 = call i32 @FD_ZERO(i32* %9)
%18 = load i32, i32* %5
%19 = call i32 @FD_SET(i32 %18, i32* %9)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %6
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* %9, i32* null, i32* null, %struct.timeval* %8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %3
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %4
br label %54
31: ; preds = %3
%32 = load i32, i32* %5
%33 = call i32 @FD_ISSET(i32 %32, i32* %9)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %31
store i32 0, i32* %4
br label %54
36: ; preds = %31
%37 = load i32, i32* %5
%38 = load i32*, i32** %7
%39 = bitcast %struct.sockaddr_storage* %11 to %struct.sockaddr*
%40 = call i32 @recvfrom(i32 %37, i32* %38, i32 1500, i32 0, %struct.sockaddr* %39, i32* %12)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %45)
store i32 -1, i32* %4
br label %54
47: ; preds = %36
%48 = load i32, i32* %10
%49 = bitcast %struct.sockaddr_storage* %11 to %struct.sockaddr*
%50 = getelementptr inbounds [52 x i8], [52 x i8]* %13, i64 0, i64 0
%51 = call i8* @formatAddress(%struct.sockaddr* %49, i8* %50, i32 52)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %48, i8* %51)
%53 = load i32, i32* %10
store i32 %53, i32* %4
br label %54
54: ; preds = %47, %43, %35, %27
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [46 x i8] c"Failed to wait for peer discovery signal: %s\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Failed to receive from UDP socket: %s\0A\00"
@.str.3 = [56 x i8] c"Received possible Peer Offer signal (%u bytes) from %s\0A\00"
@str = [30 x i8] c"Waiting for Peer Offer signal\00"
define i32 @recv_peer_offer(i32 %0, i32 %1, i32* %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
%9 = alloca [52 x i8]
%10 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%11 = bitcast i32* %6 to i8*
%12 = bitcast %struct.sockaddr_storage* %7 to i8*
%13 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i64 0, i32 0
store i32 0, i32* %13
%14 = bitcast i32* %8 to i8*
store i32 4, i32* %8
%15 = getelementptr inbounds [52 x i8], [52 x i8]* %9, i64 0, i64 0
%16 = call i32 @FD_ZERO(i32* %6)
%17 = call i32 @FD_SET(i32 %0, i32* %6)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%19 = bitcast i64* %4 to i32*
store i32 %1, i32* %19
%20 = add nsw i32 %0, 1
%21 = call i32 @select(i32 %20, i32* %6, i32* null, i32* null, %struct.timeval* %5)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %3
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %41
27: ; preds = %3
%28 = call i32 @FD_ISSET(i32 %0, i32* %6)
%29 = icmp eq i32 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %27
%31 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%32 = call i32 @recvfrom(i32 %0, i32* %2, i32 1500, i32 0, %struct.sockaddr* %31, i32* %8)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %41
38: ; preds = %30
%39 = call i8* @formatAddress(%struct.sockaddr* %31, i8* %15, i32 52)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %32, i8* %39)
br label %41
41: ; preds = %27, %38, %34, %23
%42 = phi i32 [ -1, %23 ], [ -1, %34 ], [ %32, %38 ], [ 0, %27 ]
ret i32 %42
}