#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 time_t;

/* Variables and functions */
int read(int, char *, int);
int time(int *);
int test_recv(char *dt, int mx_ln, int wt_sec, int fd) {
  time_t tloc, elapsed;
  int nbytes, ret;
  time(&tloc);
  nbytes = 0;
  do {
    ret = read(fd, (char *)(dt + nbytes), mx_ln - nbytes);
    if (ret > 0) {
      nbytes += ret;
    }
  } while (nbytes < mx_ln && ((elapsed = (time(NULL) - tloc)) < wt_sec));
  *(dt + nbytes) = '\0';
  return nbytes;
}


x86 assembly (clang, -O0):
<test_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <time@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x20(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1174 <test_recv+0x54>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1179 <test_recv+0x59>
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x25(%rbp)
jge    11a2 <test_recv+0x82>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
sub    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmp    -0x10(%rbp),%eax
setl   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1145 <test_recv+0x25>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r12
lea    0x4(%rsp),%rdi
call   1040 <time@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%r13d
lea    (%r12,%r13,1),%rsi
mov    %ebx,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
mov    $0x0,%eax
cmovs  %eax,%ebp
add    %r13,%rbp
cmp    %ebx,%ebp
jge    1186 <test_recv+0x66>
xor    %edi,%edi
call   1040 <time@plt>
sub    0x4(%rsp),%eax
cmp    %r14d,%eax
jl     1150 <test_recv+0x30>
movb   $0x0,(%r12,%rbp,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @test_recv(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = call i32 @time(i32* %9)
store i32 0, i32* %11
br label %14
14: ; preds = %41, %4
%15 = load i32, i32* %8
%16 = load i8*, i8** %5
%17 = load i32, i32* %11
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %6
%21 = load i32, i32* %11
%22 = sub nsw i32 %20, %21
%23 = call i32 @read(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %12
%24 = load i32, i32* %12
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = load i32, i32* %12
%28 = load i32, i32* %11
%29 = add nsw i32 %28, %27
store i32 %29, i32* %11
br label %30
30: ; preds = %26, %14
br label %31
31: ; preds = %30
%32 = load i32, i32* %11
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = call i32 @time(i32* null)
%37 = load i32, i32* %9
%38 = sub nsw i32 %36, %37
store i32 %38, i32* %10
%39 = load i32, i32* %7
%40 = icmp slt i32 %38, %39
br label %41
41: ; preds = %35, %31
%42 = phi i1 [ false, %31 ], [ %40, %35 ]
br i1 %42, label %14, label %43
43: ; preds = %41
%44 = load i8*, i8** %5
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i32, i32* %11
ret i32 %48
}

LLVM IR (-O2):
define i32 @test_recv(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i32 @time(i32* %5)
br label %8
8: ; preds = %18, %4
%9 = phi i32 [ 0, %4 ], [ %16, %18 ]
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = sub nsw i32 %1, %9
%13 = call i32 @read(i32 %3, i8* %11, i32 %12)
%14 = icmp sgt i32 %13, 0
%15 = select i1 %14, i32 %13, i32 0
%16 = add nuw nsw i32 %15, %9
%17 = icmp slt i32 %16, %1
br i1 %17, label %18, label %23
18: ; preds = %8
%19 = call i32 @time(i32* null)
%20 = load i32, i32* %5
%21 = sub nsw i32 %19, %20
%22 = icmp slt i32 %21, %2
br i1 %22, label %8, label %23
23: ; preds = %8, %18
%24 = zext i32 %16 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
ret i32 %16
}