#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 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;
    }
    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    0xea9(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%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    119d <recv_fix_len+0x6d>
lea    0xe75(%rip),%rdi
call   1050 <perror@plt>
jmp    11ff <recv_fix_len+0xcf>
jmp    11a2 <recv_fix_len+0x72>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f9 <recv_fix_len+0xc9>
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    11eb <recv_fix_len+0xbb>
lea    0xe3b(%rip),%rdi
call   1050 <perror@plt>
jmp    11ff <recv_fix_len+0xcf>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11a2 <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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
movq   $0x6,0x8(%rsp)
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
test   %eax,%eax
js     11af <recv_fix_len+0x7f>
xor    %ebp,%ebp
test   %ebx,%ebx
jle    11a0 <recv_fix_len+0x70>
lea    0xe94(%rip),%r12
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     11ba <recv_fix_len+0x8a>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1180 <recv_fix_len+0x50>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xe48(%rip),%r12
jmp    11bf <recv_fix_len+0x8f>
mov    $0xffffffff,%ebp
mov    %r12,%rdi
call   1050 <perror@plt>
jmp    11a0 <recv_fix_len+0x70>

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"
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 %44
19: ; preds = %3
br label %20
20: ; preds = %38, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %6
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %42
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 %44
38: ; preds = %24
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = add nsw i32 %40, %39
store i32 %41, i32* %8
br label %20
42: ; preds = %20
%43 = load i32, i32* %8
store i32 %43, i32* %7
br label %44
44: ; preds = %42, %36, %17
%45 = load i32, i32* %7
ret i32 %45
}

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"
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 %23, label %11
11: ; preds = %3
%12 = icmp sgt i32 %2, 0
br i1 %12, label %15, label %27
13: ; preds = %15
%14 = icmp slt i32 %22, %2
br i1 %14, label %15, label %27
15: ; preds = %11, %13
%16 = phi i32 [ %22, %13 ], [ 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)
%21 = icmp eq i32 %20, -1
%22 = add nsw i32 %20, %16
br i1 %21, label %23, label %13
23: ; preds = %15, %3
%24 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %15 ]
%25 = phi i32 [ %9, %3 ], [ -1, %15 ]
%26 = call i32 @perror(i8* %24)
br label %27
27: ; preds = %13, %23, %11
%28 = phi i32 [ 0, %11 ], [ %25, %23 ], [ %22, %13 ]
ret i32 %28
}