#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 timeval {
  long tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int recvfrom(int, void *, size_t, int, struct sockaddr *, int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
ssize_t timer_recvfrom(int sock, void *buff, size_t nbytes, int flags,
                       struct sockaddr *from, socklen_t *addrlen, long sec,
                       int num_try) {
  int i, result;
  fd_set target, detection;
  struct timeval timeout;
  ssize_t buff_len;
  FD_ZERO(&target);
  FD_SET(sock, &target);
  for (i = 0; i < num_try; i++) {
    detection = target;
    timeout.tv_sec = sec;
    timeout.tv_usec = 0;
    result = select(sock + 1, &detection, 0, 0, &timeout);
    if (result == -1) {
      puts("select() error");
      break;
    } else if (result == 0) {
      puts("Time-out");
    } else {
      if (FD_ISSET(sock, &detection)) {
        return recvfrom(sock, buff, nbytes, flags, from, addrlen);
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<timer_recvfrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
lea    -0x3c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1080 <FD_SET@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    0x18(%rbp),%eax
jge    1269 <timer_recvfrom+0x109>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x40(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x50(%rbp),%r8
mov    %ecx,%edx
call   1060 <select@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11f8 <timer_recvfrom+0x98>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
jmp    1269 <timer_recvfrom+0x109>
cmpl   $0x0,-0x38(%rbp)
jne    1213 <timer_recvfrom+0xb3>
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
jmp    1251 <timer_recvfrom+0xf1>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     124c <timer_recvfrom+0xec>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x4(%rbp)
jmp    1270 <timer_recvfrom+0x110>
jmp    1251 <timer_recvfrom+0xf1>
jmp    1256 <timer_recvfrom+0xf6>
jmp    125b <timer_recvfrom+0xfb>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a1 <timer_recvfrom+0x41>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer_recvfrom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9,%r15
mov    %r8,%r12
mov    %ecx,%r13d
mov    %rdx,0x30(%rsp)
mov    %rsi,0x28(%rsp)
mov    %edi,%r14d
mov    0x88(%rsp),%ebp
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
mov    $0xffffffff,%eax
test   %ebp,%ebp
jle    1265 <timer_recvfrom+0x105>
mov    %r13d,0xc(%rsp)
mov    %r12,0x18(%rsp)
mov    %r15,0x20(%rsp)
mov    0x80(%rsp),%rbx
lea    0x1(%r14),%r15d
lea    0x10(%rsp),%r12
lea    0x38(%rsp),%r13
jmp    11f1 <timer_recvfrom+0x91>
nopw   0x0(%rax,%rax,1)
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
add    $0xffffffff,%ebp
je     1260 <timer_recvfrom+0x100>
mov    0x14(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,0x38(%rsp)
movq   $0x0,0x40(%rsp)
mov    %r15d,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
test   %eax,%eax
je     11e0 <timer_recvfrom+0x80>
cmp    $0xffffffff,%eax
je     1254 <timer_recvfrom+0xf4>
mov    %r14d,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11ec <timer_recvfrom+0x8c>
mov    %r14d,%edi
mov    0x28(%rsp),%rsi
mov    0x30(%rsp),%rdx
mov    0xc(%rsp),%ecx
mov    0x18(%rsp),%r8
mov    0x20(%rsp),%r9
call   1070 <recvfrom@plt>
jmp    1265 <timer_recvfrom+0x105>
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i64, i64 }
@.str = [15 x i8] c"select() error\00"
@.str.1 = [9 x i8] c"Time-out\00"
define i32 @timer_recvfrom(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5, i64 %6, i32 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca %struct.sockaddr*
%15 = alloca i32*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca %struct.timeval
%23 = alloca i32
store i32 %0, i32* %10
store i8* %1, i8** %11
store i64 %2, i64* %12
store i32 %3, i32* %13
store %struct.sockaddr* %4, %struct.sockaddr** %14
store i32* %5, i32** %15
store i64 %6, i64* %16
store i32 %7, i32* %17
%24 = call i32 @FD_ZERO(i32* %20)
%25 = load i32, i32* %10
%26 = call i32 @FD_SET(i32 %25, i32* %20)
store i32 0, i32* %18
br label %27
27: ; preds = %63, %8
%28 = load i32, i32* %18
%29 = load i32, i32* %17
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %66
31: ; preds = %27
%32 = load i32, i32* %20
store i32 %32, i32* %21
%33 = load i64, i64* %16
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 0
store i64 %33, i64* %34
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
store i64 0, i64* %35
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
%38 = call i32 @select(i32 %37, i32* %21, i32 0, i32 0, %struct.timeval* %22)
store i32 %38, i32* %19
%39 = load i32, i32* %19
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %66
43: ; preds = %31
%44 = load i32, i32* %19
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %61
48: ; preds = %43
%49 = load i32, i32* %10
%50 = call i64 @FD_ISSET(i32 %49, i32* %21)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %48
%53 = load i32, i32* %10
%54 = load i8*, i8** %11
%55 = load i64, i64* %12
%56 = load i32, i32* %13
%57 = load %struct.sockaddr*, %struct.sockaddr** %14
%58 = load i32*, i32** %15
%59 = call i32 @recvfrom(i32 %53, i8* %54, i64 %55, i32 %56, %struct.sockaddr* %57, i32* %58)
store i32 %59, i32* %9
br label %67
60: ; preds = %48
br label %61
61: ; preds = %60, %46
br label %62
62: ; preds = %61
br label %63
63: ; preds = %62
%64 = load i32, i32* %18
%65 = add nsw i32 %64, 1
store i32 %65, i32* %18
br label %27
66: ; preds = %41, %27
store i32 -1, i32* %9
br label %67
67: ; preds = %66, %52
%68 = load i32, i32* %9
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i64, i64 }
@.str = [15 x i8] c"select() error\00"
@.str.1 = [9 x i8] c"Time-out\00"
define i32 @timer_recvfrom(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5, i64 %6, i32 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = bitcast i32* %9 to i8*
%13 = bitcast i32* %10 to i8*
%14 = bitcast %struct.timeval* %11 to i8*
%15 = call i32 @FD_ZERO(i32* %9)
%16 = call i32 @FD_SET(i32 %0, i32* %9)
%17 = icmp sgt i32 %7, 0
br i1 %17, label %18, label %38
18: ; preds = %8
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i64 0, i32 0
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i64 0, i32 1
%21 = add nsw i32 %0, 1
br label %22
22: ; preds = %18, %35
%23 = phi i32 [ 0, %18 ], [ %36, %35 ]
%24 = load i32, i32* %9
store i32 %24, i32* %10
store i64 %6, i64* %19
store i64 0, i64* %20
%25 = call i32 @select(i32 %21, i32* %10, i32 0, i32 0, %struct.timeval* %11)
switch i32 %25, label %30 [
i32 -1, label %26
i32 0, label %28
]
26: ; preds = %22
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %38
28: ; preds = %22
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %35
30: ; preds = %22
%31 = call i64 @FD_ISSET(i32 %0, i32* %10)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @recvfrom(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5)
br label %38
35: ; preds = %30, %28
%36 = add nuw nsw i32 %23, 1
%37 = icmp eq i32 %36, %7
br i1 %37, label %38, label %22
38: ; preds = %35, %8, %26, %33
%39 = phi i32 [ %34, %33 ], [ -1, %26 ], [ -1, %8 ], [ -1, %35 ]
ret i32 %39
}