#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 */

/* Variables and functions */
int bot_fileutils_read_timeout(int, char *, int, int);
int bot_fileutils_read_fully_timeout(int fd, void *bufin, int len,
                                     int msTimeout) {
  char *buf = (char *)bufin;
  int readsofar = 0;
  int thisread = 0;
  while (readsofar < len) {
    thisread = bot_fileutils_read_timeout(fd, &buf[readsofar], len - readsofar,
                                          msTimeout);
    if (thisread == 0)
      return readsofar;
    if (thisread < 0)
      return thisread;
    readsofar += thisread;
  }
  return len;
}


x86 assembly (clang, -O0):
<bot_fileutils_read_fully_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119e <bot_fileutils_read_fully_timeout+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <bot_fileutils_read_timeout@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    117b <bot_fileutils_read_fully_timeout+0x6b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <bot_fileutils_read_fully_timeout+0x94>
cmpl   $0x0,-0x28(%rbp)
jge    1190 <bot_fileutils_read_fully_timeout+0x80>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <bot_fileutils_read_fully_timeout+0x94>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    113b <bot_fileutils_read_fully_timeout+0x2b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bot_fileutils_read_fully_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
test   %edx,%edx
jle    115a <bot_fileutils_read_fully_timeout+0x4a>
mov    %ecx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r12d,%edi
mov    %r14d,%ecx
call   1030 <bot_fileutils_read_timeout@plt>
test   %eax,%eax
je     1154 <bot_fileutils_read_fully_timeout+0x44>
js     1158 <bot_fileutils_read_fully_timeout+0x48>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1130 <bot_fileutils_read_fully_timeout+0x20>
jmp    115a <bot_fileutils_read_fully_timeout+0x4a>
mov    %ebx,%ebp
jmp    115a <bot_fileutils_read_fully_timeout+0x4a>
mov    %eax,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @bot_fileutils_read_fully_timeout(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %18
6: ; preds = %4, %15
%7 = phi i32 [ %16, %15 ], [ 0, %4 ]
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub nsw i32 %2, %7
%11 = tail call i32 @bot_fileutils_read_timeout(i32 %0, i8* %9, i32 %10, i32 %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %6
%14 = icmp slt i32 %11, 0
br i1 %14, label %18, label %15
15: ; preds = %13
%16 = add nsw i32 %11, %7
%17 = icmp slt i32 %16, %2
br i1 %17, label %6, label %18
18: ; preds = %6, %13, %15, %4
%19 = phi i32 [ %2, %4 ], [ %2, %15 ], [ %11, %13 ], [ %7, %6 ]
ret i32 %19
}