#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 {
  int tv_usec;
  scalar_t__ tv_sec;
};
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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int recv_to(int fd, char *buffer, int len, int flags, int time_out) {
  fd_set readset, tempset;
  int result, recv_result, iof = -1;
  struct timeval tv;
  FD_ZERO(&readset);
  FD_SET(fd, &readset);
  tv.tv_sec = 0;
  tv.tv_usec = time_out * 1000;
  result = select(fd + 1, &readset, NULL, NULL, &tv);
  do {
    if (0)
      fprintf(stderr, "select return result: %d\n", result);
  } while (0);
  if (result < 0) {
    return -1;
  } else {
    if (result > 0 && FD_ISSET(fd, &readset)) {
      if ((iof = fcntl(fd, F_GETFL, 0)) != -1) {
        fcntl(fd, F_SETFL, iof | O_NONBLOCK);
      } else {
        printf("failed to set non-blocking mode\n");
      }
      recv_result = recv(fd, buffer, len, flags);
      do {
        if (0)
          fprintf(stderr, "number of bytes received: %d\n", recv_result);
      } while (0);
      if (recv_result == 0)
        printf("recv result 0, TCP connection was closed\n");
      if (iof != -1)
        fcntl(fd, F_SETFL, iof);
      return recv_result;
    }
  }
  do {
    if (0)
      fprintf(stderr, "result should be 0: %d\n", result);
  } while (0);
  return -2;
}


x86 assembly (clang, -O0):
<recv_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0xffffffff,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <FD_SET@plt>
movq   $0x0,-0x38(%rbp)
imul   $0x3e8,-0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x28(%rbp)
jmp    11d9 <recv_to+0x69>
cmpl   $0x0,-0x28(%rbp)
jge    11ef <recv_to+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <recv_to+0x15b>
cmpl   $0x0,-0x28(%rbp)
jle    12b5 <recv_to+0x145>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12b5 <recv_to+0x145>
mov    -0x8(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
je     1250 <recv_to+0xe0>
mov    -0x8(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
mov    0x2da4(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
jmp    125c <recv_to+0xec>
lea    0xda9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
jmp    1276 <recv_to+0x106>
cmpl   $0x0,-0x2c(%rbp)
jne    128c <recv_to+0x11c>
lea    0xd9a(%rip),%rdi
call   1060 <printf@plt>
cmpl   $0xffffffff,-0x30(%rbp)
je     12aa <recv_to+0x13a>
mov    -0x8(%rbp),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
call   1040 <fcntl@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12cb <recv_to+0x15b>
jmp    12ba <recv_to+0x14a>
jmp    12bf <recv_to+0x14f>
jmp    12c4 <recv_to+0x154>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_to>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %ecx,%r13d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%r14d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
imul   $0x3e8,%ebp,%eax
mov    %eax,0x8(%rsp)
lea    0x1(%r14),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %eax,%eax
js     122f <recv_to+0xbf>
mov    $0xfffffffe,%ebp
je     127e <recv_to+0x10e>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     127e <recv_to+0x10e>
mov    %r13d,%ebx
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1236 <recv_to+0xc6>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
or     %r13d,%edx
mov    %r14d,%edi
call   1050 <fcntl@plt>
jmp    1242 <recv_to+0xd2>
mov    $0xffffffff,%ebp
jmp    127e <recv_to+0x10e>
lea    0xdc3(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %ebx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1264 <recv_to+0xf4>
lea    0xdc1(%rip),%rdi
call   1040 <puts@plt>
cmp    $0xffffffff,%r13d
je     127e <recv_to+0x10e>
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %r13d,%edx
call   1050 <fcntl@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [33 x i8] c"failed to set non-blocking mode\0A\00"
@.str.1 = [42 x i8] c"recv result 0, TCP connection was closed\0A\00"
@stderr = global i32 0
define i32 @recv_to(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca %struct.timeval
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 -1, i32* %16
%18 = call i32 @FD_ZERO(i32* %12)
%19 = load i32, i32* %7
%20 = call i32 @FD_SET(i32 %19, i32* %12)
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 1
store i64 0, i64* %21
%22 = load i32, i32* %11
%23 = mul nsw i32 %22, 1000
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
%27 = call i32 @select(i32 %26, i32* %12, i32* null, i32* null, %struct.timeval* %17)
store i32 %27, i32* %14
br label %28
28: ; preds = %5
br label %29
29: ; preds = %28
%30 = load i32, i32* %14
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %6
br label %80
33: ; preds = %29
%34 = load i32, i32* %14
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %76
36: ; preds = %33
%37 = load i32, i32* %7
%38 = call i64 @FD_ISSET(i32 %37, i32* %12)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %76
40: ; preds = %36
%41 = load i32, i32* %7
%42 = load i32, i32* @F_GETFL
%43 = call i32 @fcntl(i32 %41, i32 %42, i32 0)
store i32 %43, i32* %16
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %52
45: ; preds = %40
%46 = load i32, i32* %7
%47 = load i32, i32* @F_SETFL
%48 = load i32, i32* %16
%49 = load i32, i32* @O_NONBLOCK
%50 = or i32 %48, %49
%51 = call i32 @fcntl(i32 %46, i32 %47, i32 %50)
br label %54
52: ; preds = %40
%53 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %54
54: ; preds = %52, %45
%55 = load i32, i32* %7
%56 = load i8*, i8** %8
%57 = load i32, i32* %9
%58 = load i32, i32* %10
%59 = call i32 @recv(i32 %55, i8* %56, i32 %57, i32 %58)
store i32 %59, i32* %15
br label %60
60: ; preds = %54
br label %61
61: ; preds = %60
%62 = load i32, i32* %15
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
%67 = load i32, i32* %16
%68 = icmp ne i32 %67, -1
br i1 %68, label %69, label %74
69: ; preds = %66
%70 = load i32, i32* %7
%71 = load i32, i32* @F_SETFL
%72 = load i32, i32* %16
%73 = call i32 @fcntl(i32 %70, i32 %71, i32 %72)
br label %74
74: ; preds = %69, %66
%75 = load i32, i32* %15
store i32 %75, i32* %6
br label %80
76: ; preds = %36, %33
br label %77
77: ; preds = %76
br label %78
78: ; preds = %77
br label %79
79: ; preds = %78
store i32 -2, i32* %6
br label %80
80: ; preds = %79, %74, %32
%81 = load i32, i32* %6
ret i32 %81
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@str = [32 x i8] c"failed to set non-blocking mode\00"
@str.2 = [41 x i8] c"recv result 0, TCP connection was closed\00"
define i32 @recv_to(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast i32* %6 to i8*
%9 = bitcast %struct.timeval* %7 to i8*
%10 = call i32 @FD_ZERO(i32* %6)
%11 = call i32 @FD_SET(i32 %0, i32* %6)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %12
%13 = mul nsw i32 %4, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %13, i32* %14
%15 = add nsw i32 %0, 1
%16 = call i32 @select(i32 %15, i32* %6, i32* null, i32* null, %struct.timeval* %7)
%17 = icmp slt i32 %16, 0
br i1 %17, label %43, label %18
18: ; preds = %5
%19 = icmp eq i32 %16, 0
br i1 %19, label %43, label %20
20: ; preds = %18
%21 = call i64 @FD_ISSET(i32 %0, i32* %6)
%22 = icmp eq i64 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %20
%24 = load i32, i32* @F_GETFL
%25 = call i32 @fcntl(i32 %0, i32 %24, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %29, %25
%31 = call i32 @fcntl(i32 %0, i32 %28, i32 %30)
br label %34
32: ; preds = %23
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = call i32 @recv(i32 %0, i8* %1, i32 %2, i32 %3)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
br i1 %26, label %43, label %40
40: ; preds = %39
%41 = load i32, i32* @F_SETFL
%42 = call i32 @fcntl(i32 %0, i32 %41, i32 %25)
br label %43
43: ; preds = %18, %20, %39, %40, %5
%44 = phi i32 [ -1, %5 ], [ %35, %40 ], [ %35, %39 ], [ -2, %20 ], [ -2, %18 ]
ret i32 %44
}