#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_line_timeout_ex(int fd, void *buf_in, int maxlen,
                                       int msTimeout, int *timed_out) {
  int len = 0;
  int thislen;
  char *buf = (char *)buf_in;
  *timed_out = 0;
  while (len < maxlen) {
    thislen = bot_fileutils_read_timeout(fd, &buf[len], 1, msTimeout);
    if (thislen < 0)
      return thislen;
    if (thislen == 0) {
      *timed_out = 1;
      return 0;
    }
    if (buf[len] == '\n' || buf[len] == '\r') {
      buf[len] = 0;
      return len;
    }
    len++;
  }
  buf[len] = 0;
  return len;
}


x86 assembly (clang, -O0):
<bot_fileutils_read_line_timeout_ex>:
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    %r8,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f0 <bot_fileutils_read_line_timeout_ex+0xe0>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%ecx
mov    $0x1,%edx
call   1030 <bot_fileutils_read_timeout@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1181 <bot_fileutils_read_line_timeout_ex+0x71>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1202 <bot_fileutils_read_line_timeout_ex+0xf2>
cmpl   $0x0,-0x28(%rbp)
jne    11a1 <bot_fileutils_read_line_timeout_ex+0x91>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1202 <bot_fileutils_read_line_timeout_ex+0xf2>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11cb <bot_fileutils_read_line_timeout_ex+0xbb>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11e2 <bot_fileutils_read_line_timeout_ex+0xd2>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1202 <bot_fileutils_read_line_timeout_ex+0xf2>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1142 <bot_fileutils_read_line_timeout_ex+0x32>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bot_fileutils_read_line_timeout_ex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
movl   $0x0,(%r8)
test   %edx,%edx
jle    1179 <bot_fileutils_read_line_timeout_ex+0x69>
mov    %ecx,%r12d
mov    %edi,%ebx
mov    %r8,0x10(%rsp)
mov    %edx,0xc(%rsp)
mov    %edx,%r14d
xor    %ebp,%ebp
nop
mov    %r13,%r15
add    %rbp,%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %r12d,%ecx
call   1030 <bot_fileutils_read_timeout@plt>
test   %eax,%eax
js     118f <bot_fileutils_read_line_timeout_ex+0x7f>
je     1193 <bot_fileutils_read_line_timeout_ex+0x83>
movzbl (%r15),%eax
cmp    $0xd,%al
je     1187 <bot_fileutils_read_line_timeout_ex+0x77>
cmp    $0xa,%al
je     1187 <bot_fileutils_read_line_timeout_ex+0x77>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1140 <bot_fileutils_read_line_timeout_ex+0x30>
mov    0xc(%rsp),%ecx
jmp    117b <bot_fileutils_read_line_timeout_ex+0x6b>
xor    %ecx,%ecx
mov    %ecx,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %ecx,%ebp
jmp    11a0 <bot_fileutils_read_line_timeout_ex+0x90>
movb   $0x0,0x0(%r13,%rbp,1)
jmp    11a0 <bot_fileutils_read_line_timeout_ex+0x90>
mov    %eax,%ebp
jmp    11a0 <bot_fileutils_read_line_timeout_ex+0x90>
mov    0x10(%rsp),%rax
movl   $0x1,(%rax)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bot_fileutils_read_line_timeout_ex(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 i32
%13 = alloca i32
%14 = alloca i8*
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
store i32 0, i32* %12
%15 = load i8*, i8** %8
store i8* %15, i8** %14
%16 = load i32*, i32** %11
store i32 0, i32* %16
br label %17
17: ; preds = %60, %5
%18 = load i32, i32* %12
%19 = load i32, i32* %9
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %63
21: ; preds = %17
%22 = load i32, i32* %7
%23 = load i8*, i8** %14
%24 = load i32, i32* %12
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i32, i32* %10
%28 = call i32 @bot_fileutils_read_timeout(i32 %22, i8* %26, i32 1, i32 %27)
store i32 %28, i32* %13
%29 = load i32, i32* %13
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = load i32, i32* %13
store i32 %32, i32* %6
br label %69
33: ; preds = %21
%34 = load i32, i32* %13
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load i32*, i32** %11
store i32 1, i32* %37
store i32 0, i32* %6
br label %69
38: ; preds = %33
%39 = load i8*, i8** %14
%40 = load i32, i32* %12
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 10
br i1 %45, label %54, label %46
46: ; preds = %38
%47 = load i8*, i8** %14
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 13
br i1 %53, label %54, label %60
54: ; preds = %46, %38
%55 = load i8*, i8** %14
%56 = load i32, i32* %12
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
%59 = load i32, i32* %12
store i32 %59, i32* %6
br label %69
60: ; preds = %46
%61 = load i32, i32* %12
%62 = add nsw i32 %61, 1
store i32 %62, i32* %12
br label %17
63: ; preds = %17
%64 = load i8*, i8** %14
%65 = load i32, i32* %12
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 0, i8* %67
%68 = load i32, i32* %12
store i32 %68, i32* %6
br label %69
69: ; preds = %63, %54, %36, %31
%70 = load i32, i32* %6
ret i32 %70
}

LLVM IR (-O2):
define i32 @bot_fileutils_read_line_timeout_ex(i32 %0, i8* %1, i32 %2, i32 %3, i32* nocapture writeonly %4) {
store i32 0, i32* %4
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %24
7: ; preds = %5
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %21
%10 = phi i64 [ 0, %7 ], [ %22, %21 ]
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = tail call i32 @bot_fileutils_read_timeout(i32 %0, i8* %11, i32 1, i32 %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %9
%15 = icmp eq i32 %12, 0
br i1 %15, label %16, label %17
16: ; preds = %14
store i32 1, i32* %4
br label %28
17: ; preds = %14
%18 = load i8, i8* %11
switch i8 %18, label %21 [
i8 10, label %19
i8 13, label %19
]
19: ; preds = %17, %17
%20 = trunc i64 %10 to i32
store i8 0, i8* %11
br label %28
21: ; preds = %17
%22 = add nuw nsw i64 %10, 1
%23 = icmp eq i64 %22, %8
br i1 %23, label %24, label %9
24: ; preds = %21, %5
%25 = phi i32 [ 0, %5 ], [ %2, %21 ]
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %9, %24, %19, %16
%29 = phi i32 [ 0, %16 ], [ %20, %19 ], [ %25, %24 ], [ %12, %9 ]
ret i32 %29
}