#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_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_RCVTIMEO;
scalar_t__ errno;
int /*<<< orphan*/ logcat(char *, ...);
int recv(int, unsigned char *, int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int socket_recv(int sockfd, unsigned char *buf, int len, int timeout, int udp) {
  unsigned char *p_buf = buf;
  int recvBytes = 0;
  int total_len = len;
  struct timeval tv;
  int ret;
  int nRecvBuf = 64 * 1024;
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
                   sizeof(struct timeval));
  if (ret < 0) {
    logcat("setsockopt: %s\n", strerror(errno));
    return -1;
  }
  setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (const char *)&nRecvBuf,
             sizeof(int));
  if (udp)
    return recvBytes = recv(sockfd, p_buf, total_len, 0);
  while (1) {
    recvBytes = recv(sockfd, p_buf, total_len, 0);
    if (recvBytes < 0 && errno != EINTR) {
      if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
      } else {
        logcat("Receive: error occur!\n");
        return -2;
      }
      break;
    } else if (recvBytes == 0) {
      logcat("Receive: disconnected.\n");
      return -2;
    } else if (recvBytes > 0) {
      if (recvBytes == total_len) {
        total_len -= recvBytes;
        break;
      }
      total_len -= recvBytes;
      p_buf += recvBytes;
      if (timeout == 0)
        break;
      else
        continue;
    } else {
      continue;
    }
  }
  return len - total_len;
}


x86 assembly (clang, -O0):
<socket_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x10000,-0x48(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11df <socket_recv+0x9f>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <socket_recv+0x206>
mov    -0x8(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1229 <socket_recv+0xe9>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
mov    %eax,-0x4(%rbp)
jmp    1346 <socket_recv+0x206>
jmp    122e <socket_recv+0xee>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12be <socket_recv+0x17e>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
je     12be <socket_recv+0x17e>
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
je     129a <socket_recv+0x15a>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d17(%rip),%rcx
cmp    (%rcx),%rax
jne    129f <socket_recv+0x15f>
jmp    12b9 <socket_recv+0x179>
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1346 <socket_recv+0x206>
jmp    133d <socket_recv+0x1fd>
cmpl   $0x0,-0x2c(%rbp)
jne    12e2 <socket_recv+0x1a2>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1050 <logcat@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1346 <socket_recv+0x206>
cmpl   $0x0,-0x2c(%rbp)
jle    1338 <socket_recv+0x1f8>
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    1308 <socket_recv+0x1c8>
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
jmp    133d <socket_recv+0x1fd>
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1333 <socket_recv+0x1f3>
jmp    133d <socket_recv+0x1fd>
jmp    122e <socket_recv+0xee>
jmp    122e <socket_recv+0xee>
mov    -0x14(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r15d
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%ebp
movl   $0x10000,0x4(%rsp)
mov    %ecx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e71(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
test   %eax,%eax
js     11d3 <socket_recv+0x93>
mov    0x0(%r13),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %r15d,%r15d
je     11fd <socket_recv+0xbd>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r14d
jmp    128c <socket_recv+0x14c>
mov    0x2dee(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
lea    0xe17(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <logcat@plt>
mov    $0xffffffff,%r14d
jmp    128c <socket_recv+0x14c>
mov    %ebx,(%rsp)
mov    0x2dc1(%rip),%r13
mov    0x2daa(%rip),%r15
mov    %r14d,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    123d <socket_recv+0xfd>
mov    0x0(%r13),%rax
cmp    (%r15),%rax
je     1220 <socket_recv+0xe0>
jmp    1250 <socket_recv+0x110>
je     127e <socket_recv+0x13e>
sub    %eax,%ebx
je     1287 <socket_recv+0x147>
mov    %eax,%eax
add    %rax,%r12
cmpl   $0x0,(%rsp)
jne    1220 <socket_recv+0xe0>
jmp    1289 <socket_recv+0x149>
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%rax
je     1289 <socket_recv+0x149>
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%rax
je     1289 <socket_recv+0x149>
lea    0xda1(%rip),%rdi
xor    %eax,%eax
call   1050 <logcat@plt>
mov    $0xfffffffe,%r14d
jmp    128c <socket_recv+0x14c>
lea    0xda2(%rip),%rdi
jmp    126f <socket_recv+0x12f>
xor    %ebx,%ebx
sub    %ebx,%r14d
mov    %r14d,%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 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i64 0
@SO_RCVBUF = global i32 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.1 = [23 x i8] c"Receive: error occur!\0A\00"
@.str.2 = [24 x i8] c"Receive: disconnected.\0A\00"
define i32 @socket_recv(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 i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.timeval
%16 = alloca i32
%17 = alloca i32
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
%18 = load i8*, i8** %8
store i8* %18, i8** %12
store i32 0, i32* %13
%19 = load i32, i32* %9
store i32 %19, i32* %14
store i32 65536, i32* %17
%20 = load i32, i32* %10
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %22
%23 = load i32, i32* %7
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_RCVTIMEO
%26 = bitcast %struct.timeval* %15 to i8*
%27 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i8* %26, i32 16)
store i32 %27, i32* %16
%28 = load i32, i32* %16
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %5
%31 = load i64, i64* @errno
%32 = call i32 @strerror(i64 %31)
%33 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %32)
store i32 -1, i32* %6
br label %104
34: ; preds = %5
%35 = load i32, i32* %7
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_RCVBUF
%38 = bitcast i32* %17 to i8*
%39 = call i32 @setsockopt(i32 %35, i32 %36, i32 %37, i8* %38, i32 4)
%40 = load i32, i32* %11
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %34
%43 = load i32, i32* %7
%44 = load i8*, i8** %12
%45 = load i32, i32* %14
%46 = call i32 @recv(i32 %43, i8* %44, i32 %45, i32 0)
store i32 %46, i32* %13
store i32 %46, i32* %6
br label %104
47: ; preds = %34
br label %48
48: ; preds = %47, %98, %99
%49 = load i32, i32* %7
%50 = load i8*, i8** %12
%51 = load i32, i32* %14
%52 = call i32 @recv(i32 %49, i8* %50, i32 %51, i32 0)
store i32 %52, i32* %13
%53 = load i32, i32* %13
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %71
55: ; preds = %48
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINTR
%58 = icmp ne i64 %56, %57
br i1 %58, label %59, label %71
59: ; preds = %55
%60 = load i64, i64* @errno
%61 = load i64, i64* @EWOULDBLOCK
%62 = icmp eq i64 %60, %61
br i1 %62, label %67, label %63
63: ; preds = %59
%64 = load i64, i64* @errno
%65 = load i64, i64* @EAGAIN
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %63, %59
br label %70
68: ; preds = %63
%69 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %6
br label %104
70: ; preds = %67
br label %100
71: ; preds = %55, %48
%72 = load i32, i32* %13
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %6
br label %104
76: ; preds = %71
%77 = load i32, i32* %13
%78 = icmp sgt i32 %77, 0
br i1 %78, label %79, label %99
79: ; preds = %76
%80 = load i32, i32* %13
%81 = load i32, i32* %14
%82 = icmp eq i32 %80, %81
br i1 %82, label %83, label %87
83: ; preds = %79
%84 = load i32, i32* %13
%85 = load i32, i32* %14
%86 = sub nsw i32 %85, %84
store i32 %86, i32* %14
br label %100
87: ; preds = %79
%88 = load i32, i32* %13
%89 = load i32, i32* %14
%90 = sub nsw i32 %89, %88
store i32 %90, i32* %14
%91 = load i32, i32* %13
%92 = load i8*, i8** %12
%93 = sext i32 %91 to i64
%94 = getelementptr inbounds i8, i8* %92, i64 %93
store i8* %94, i8** %12
%95 = load i32, i32* %10
%96 = icmp eq i32 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %87
br label %100
98: ; preds = %87
br label %48
99: ; preds = %76
br label %48
100: ; preds = %97, %83, %70
%101 = load i32, i32* %9
%102 = load i32, i32* %14
%103 = sub nsw i32 %101, %102
store i32 %103, i32* %6
br label %104
104: ; preds = %100, %74, %68, %42, %30
%105 = load i32, i32* %6
ret i32 %105
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i64 0
@SO_RCVBUF = global i32 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.1 = [23 x i8] c"Receive: error occur!\0A\00"
@.str.2 = [24 x i8] c"Receive: disconnected.\0A\00"
define i32 @socket_recv(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = bitcast %struct.timeval* %6 to i8*
%9 = bitcast i32* %7 to i8*
store i32 65536, i32* %7
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %3, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_RCVTIMEO
%14 = call i32 @setsockopt(i32 %0, i32 %12, i32 %13, i8* %8, i32 16)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %5
%17 = load i64, i64* @errno
%18 = call i32 @strerror(i64 %17)
%19 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %60
20: ; preds = %5
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVBUF
%23 = call i32 @setsockopt(i32 %0, i32 %21, i32 %22, i8* %9, i32 4)
%24 = icmp eq i32 %4, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = icmp eq i32 %3, 0
br label %54
27: ; preds = %20
%28 = call i32 @recv(i32 %0, i8* %1, i32 %2, i32 0)
br label %60
29: ; preds = %54, %32
%30 = call i32 @recv(i32 %0, i8* %55, i32 %56, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %29, label %36
36: ; preds = %32
%37 = load i64, i64* @EWOULDBLOCK
%38 = icmp eq i64 %33, %37
%39 = load i64, i64* @EAGAIN
%40 = icmp eq i64 %33, %39
%41 = select i1 %38, i1 true, i1 %40
br i1 %41, label %57, label %42
42: ; preds = %36
%43 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %60
44: ; preds = %29
%45 = icmp eq i32 %30, 0
br i1 %45, label %46, label %48
46: ; preds = %44
%47 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %60
48: ; preds = %44
%49 = icmp eq i32 %56, %30
br i1 %49, label %57, label %50
50: ; preds = %48
%51 = sub nsw i32 %56, %30
%52 = zext i32 %30 to i64
%53 = getelementptr inbounds i8, i8* %55, i64 %52
br i1 %26, label %57, label %54
54: ; preds = %25, %50
%55 = phi i8* [ %1, %25 ], [ %53, %50 ]
%56 = phi i32 [ %2, %25 ], [ %51, %50 ]
br label %29
57: ; preds = %50, %48, %36
%58 = phi i32 [ %56, %36 ], [ %51, %50 ], [ 0, %48 ]
%59 = sub nsw i32 %2, %58
br label %60
60: ; preds = %57, %46, %42, %27, %16
%61 = phi i32 [ -1, %16 ], [ %28, %27 ], [ %59, %57 ], [ -2, %42 ], [ -2, %46 ]
ret i32 %61
}