#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*/ tv;
struct timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int recv_fix_len(int sockfd, char *buf, int len) {
  int ret = 0;
  int recv_len = 0;
  struct timeval tv = {6, 0};
  ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  if (ret < 0) {
    perror("Fail to setsockopt.");
    goto exit;
  }
  while (recv_len < len) {
    ret = recv(sockfd, buf + recv_len, len - recv_len, 0);
    if (-1 == ret) {
      perror("Fail to recv");
      goto exit;
    } else if (0 == ret) {
      printf("recv timeout!\n");
      goto exit;
    }
    recv_len += ret;
  }
  ret = recv_len;
exit:
  return ret;
}


x86 assembly (clang, -O0):
<recv_fix_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0xe99(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ad <recv_fix_len+0x6d>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
jmp    1230 <recv_fix_len+0xf0>
jmp    11b2 <recv_fix_len+0x72>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    122a <recv_fix_len+0xea>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11fb <recv_fix_len+0xbb>
lea    0xe2b(%rip),%rdi
call   1060 <perror@plt>
jmp    1230 <recv_fix_len+0xf0>
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jne    1217 <recv_fix_len+0xd7>
lea    0xe1c(%rip),%rdi
call   1050 <printf@plt>
jmp    1230 <recv_fix_len+0xf0>
jmp    121c <recv_fix_len+0xdc>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b2 <recv_fix_len+0x72>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fix_len>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
movq   $0x6,(%rsp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x8,%r8d
call   1050 <setsockopt@plt>
test   %eax,%eax
js     11d1 <recv_fix_len+0x91>
xor    %ebp,%ebp
test   %ebx,%ebx
jle    11c4 <recv_fix_len+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     11b6 <recv_fix_len+0x76>
cmp    $0xffffffff,%eax
je     11e1 <recv_fix_len+0xa1>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1190 <recv_fix_len+0x50>
jmp    11c4 <recv_fix_len+0x84>
lea    0xe64(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
jmp    11c4 <recv_fix_len+0x84>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11c4 <recv_fix_len+0x84>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.recv_fix_len.tv = %struct.timeval { i32 6, i32 0 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [20 x i8] c"Fail to setsockopt.\00"
@.str.1 = [13 x i8] c"Fail to recv\00"
@.str.2 = [15 x i8] c"recv timeout!\0A\00"
define i32 @recv_fix_len(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = bitcast %struct.timeval* %9 to i8*
%11 = load i32, i32* %4
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_RCVTIMEO
%14 = call i32 @setsockopt(i32 %11, i32 %12, i32 %13, %struct.timeval* %9, i32 8)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %50
19: ; preds = %3
br label %20
20: ; preds = %44, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %6
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %48
24: ; preds = %20
%25 = load i32, i32* %4
%26 = load i8*, i8** %5
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %6
%31 = load i32, i32* %8
%32 = sub nsw i32 %30, %31
%33 = call i32 @recv(i32 %25, i8* %29, i32 %32, i32 0)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 -1, %34
br i1 %35, label %36, label %38
36: ; preds = %24
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %50
38: ; preds = %24
%39 = load i32, i32* %7
%40 = icmp eq i32 0, %39
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %50
43: ; preds = %38
br label %44
44: ; preds = %43
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = add nsw i32 %46, %45
store i32 %47, i32* %8
br label %20
48: ; preds = %20
%49 = load i32, i32* %8
store i32 %49, i32* %7
br label %50
50: ; preds = %48, %41, %36, %17
%51 = load i32, i32* %7
ret i32 %51
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [20 x i8] c"Fail to setsockopt.\00"
@.str.1 = [13 x i8] c"Fail to recv\00"
@str = [14 x i8] c"recv timeout!\00"
define i32 @recv_fix_len(i32 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = bitcast i64* %4 to i8*
store i64 6, i64* %4
%7 = load i32, i32* @SOL_SOCKET
%8 = load i32, i32* @SO_RCVTIMEO
%9 = call i32 @setsockopt(i32 %0, i32 %7, i32 %8, %struct.timeval* %5, i32 8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = icmp sgt i32 %2, 0
br i1 %12, label %15, label %28
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %28
15: ; preds = %11, %25
%16 = phi i32 [ %26, %25 ], [ 0, %11 ]
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = sub nsw i32 %2, %16
%20 = call i32 @recv(i32 %0, i8* %18, i32 %19, i32 0)
switch i32 %20, label %25 [
i32 -1, label %21
i32 0, label %23
]
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %28
23: ; preds = %15
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %28
25: ; preds = %15
%26 = add nsw i32 %20, %16
%27 = icmp slt i32 %26, %2
br i1 %27, label %15, label %28
28: ; preds = %25, %11, %23, %21, %13
%29 = phi i32 [ %9, %13 ], [ -1, %21 ], [ 0, %23 ], [ 0, %11 ], [ %26, %25 ]
ret i32 %29
}