#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;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
scalar_t__ errno;
int read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct timeval *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int time(int /*<<< orphan*/ *);
int sockreadline(int sock, char *buf, int n, time_t *timeoutStartTime) {
  int numRead, totRead = 0, gotatleastonebyte = 0;
  char ch;
  struct timeval tv;
  if (timeoutStartTime != NULL) {
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv,
               sizeof(struct timeval));
  }
  for (;;) {
    numRead = read(sock, &ch, 1);
    if ((timeoutStartTime != NULL) && (time(NULL) - *timeoutStartTime >= 10)) {
      syslog(LOG_INFO, "Request takes too long to come. Connection aborted.");
      return (-1);
    }
    if (numRead == -1) {
      if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
        continue;
      } else {
        return (-1);
      }
    } else if (numRead == 0) {
      if (gotatleastonebyte == 0)
        totRead = -1;
      break;
    } else {
      gotatleastonebyte = 1;
      if (ch == '\r')
        continue;
      if (ch == '\n')
        break;
      if (totRead < n - 1) {
        totRead++;
        *buf++ = ch;
      }
    }
  }
  if (timeoutStartTime != NULL) {
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv,
               sizeof(struct timeval));
  }
  *buf = 0;
  return (totRead);
}


x86 assembly (clang, -O0):
<sockreadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a2 <sockreadline+0x62>
movl   $0x1,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
jmp    11a7 <sockreadline+0x67>
mov    -0x8(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ff <sockreadline+0xbf>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    -0x20(%rbp),%rcx
sub    (%rcx),%eax
cmp    $0xa,%eax
jl     11ff <sockreadline+0xbf>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
call   1060 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <sockreadline+0x1fb>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1268 <sockreadline+0x128>
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
je     1257 <sockreadline+0x117>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%rax
je     1257 <sockreadline+0x117>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <sockreadline+0x11c>
jmp    11a7 <sockreadline+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <sockreadline+0x1fb>
cmpl   $0x0,-0x24(%rbp)
jne    1288 <sockreadline+0x148>
cmpl   $0x0,-0x2c(%rbp)
jne    1283 <sockreadline+0x143>
movl   $0xffffffff,-0x28(%rbp)
jmp    12f0 <sockreadline+0x1b0>
movl   $0x1,-0x2c(%rbp)
movsbl -0x2d(%rbp),%eax
cmp    $0xd,%eax
jne    12a1 <sockreadline+0x161>
jmp    11a7 <sockreadline+0x67>
movsbl -0x2d(%rbp),%eax
cmp    $0xa,%eax
jne    12b3 <sockreadline+0x173>
jmp    12f0 <sockreadline+0x1b0>
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12e1 <sockreadline+0x1a1>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2d(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    12e6 <sockreadline+0x1a6>
jmp    12eb <sockreadline+0x1ab>
jmp    11a7 <sockreadline+0x67>
cmpq   $0x0,-0x20(%rbp)
je     132e <sockreadline+0x1ee>
movl   $0x0,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
mov    0x2cbb(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockreadline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%rbx
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
test   %rcx,%rcx
je     1193 <sockreadline+0x53>
movl   $0x1,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
lea    0x28(%rsp),%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %r14,0x18(%rsp)
add    $0xffffffff,%r15d
mov    %r15d,0x20(%rsp)
xor    %eax,%eax
mov    %rax,0x10(%rsp)
lea    0xf(%rsp),%r13
mov    0x2e1c(%rip),%r12
mov    0x2e05(%rip),%r15
xor    %edx,%edx
mov    %edx,0x24(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%r14d
test   %rbx,%rbx
je     11f9 <sockreadline+0xb9>
xor    %edi,%edi
call   1050 <time@plt>
sub    (%rbx),%eax
cmp    $0xa,%eax
jge    12db <sockreadline+0x19b>
cmp    $0xffffffff,%r14d
jne    1230 <sockreadline+0xf0>
mov    (%r12),%rax
cmp    (%r15),%rax
je     11d0 <sockreadline+0x90>
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <sockreadline+0x90>
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <sockreadline+0x90>
jmp    12f0 <sockreadline+0x1b0>
cs nopw 0x0(%rax,%rax,1)
nop
test   %r14d,%r14d
je     1288 <sockreadline+0x148>
mov    $0x1,%edx
mov    0xf(%rsp),%al
cmp    $0xd,%al
je     11bd <sockreadline+0x7d>
cmp    $0xa,%al
je     1308 <sockreadline+0x1c8>
mov    $0x1,%edx
mov    0x20(%rsp),%ecx
cmp    %ecx,0x10(%rsp)
jge    11bd <sockreadline+0x7d>
mov    0x10(%rsp),%rcx
add    $0x1,%ecx
mov    %rcx,0x10(%rsp)
mov    0x18(%rsp),%rcx
mov    %al,(%rcx)
add    $0x1,%rcx
mov    %rcx,0x18(%rsp)
mov    $0x1,%edx
jmp    11bd <sockreadline+0x7d>
cmpl   $0x1,0x24(%rsp)
sbb    %eax,%eax
or     0x10(%rsp),%eax
mov    %eax,%r15d
mov    0x18(%rsp),%r14
test   %rbx,%rbx
je     12d5 <sockreadline+0x195>
movl   $0x0,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
lea    0x28(%rsp),%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
movb   $0x0,(%r14)
jmp    12f6 <sockreadline+0x1b6>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd15(%rip),%rsi
call   1060 <syslog@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x18(%rsp),%r14
mov    0x10(%rsp),%r15
test   %rbx,%rbx
jne    12a0 <sockreadline+0x160>
jmp    12d5 <sockreadline+0x195>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@LOG_INFO = global i32 0
@.str = [52 x i8] c"Request takes too long to come. Connection aborted.\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @sockreadline(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 i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8
%14 = alloca %struct.timeval
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 0, i32* %11
store i32 0, i32* %12
%15 = load i32*, i32** %9
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %24
17: ; preds = %4
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i64 0, i64* %19
%20 = load i32, i32* %6
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVTIMEO
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, %struct.timeval* %14, i32 16)
br label %24
24: ; preds = %17, %4
br label %25
25: ; preds = %87, %68, %54, %24
%26 = load i32, i32* %6
%27 = call i32 @read(i32 %26, i8* %13, i32 1)
store i32 %27, i32* %10
%28 = load i32*, i32** %9
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %39
30: ; preds = %25
%31 = call i32 @time(i32* null)
%32 = load i32*, i32** %9
%33 = load i32, i32* %32
%34 = sub nsw i32 %31, %33
%35 = icmp sge i32 %34, 10
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i32, i32* @LOG_INFO
%38 = call i32 @syslog(i32 %37, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %101
39: ; preds = %30, %25
%40 = load i32, i32* %10
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %56
42: ; preds = %39
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br i1 %45, label %54, label %46
46: ; preds = %42
%47 = load i64, i64* @errno
%48 = load i64, i64* @EAGAIN
%49 = icmp eq i64 %47, %48
br i1 %49, label %54, label %50
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = load i64, i64* @EWOULDBLOCK
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50, %46, %42
br label %25
55: ; preds = %50
store i32 -1, i32* %5
br label %101
56: ; preds = %39
%57 = load i32, i32* %10
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32, i32* %12
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 -1, i32* %11
br label %63
63: ; preds = %62, %59
br label %88
64: ; preds = %56
store i32 1, i32* %12
%65 = load i8, i8* %13
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %66, 13
br i1 %67, label %68, label %69
68: ; preds = %64
br label %25
69: ; preds = %64
%70 = load i8, i8* %13
%71 = sext i8 %70 to i32
%72 = icmp eq i32 %71, 10
br i1 %72, label %73, label %74
73: ; preds = %69
br label %88
74: ; preds = %69
%75 = load i32, i32* %11
%76 = load i32, i32* %8
%77 = sub nsw i32 %76, 1
%78 = icmp slt i32 %75, %77
br i1 %78, label %79, label %85
79: ; preds = %74
%80 = load i32, i32* %11
%81 = add nsw i32 %80, 1
store i32 %81, i32* %11
%82 = load i8, i8* %13
%83 = load i8*, i8** %7
%84 = getelementptr inbounds i8, i8* %83, i32 1
store i8* %84, i8** %7
store i8 %82, i8* %83
br label %85
85: ; preds = %79, %74
br label %86
86: ; preds = %85
br label %87
87: ; preds = %86
br label %25
88: ; preds = %73, %63
%89 = load i32*, i32** %9
%90 = icmp ne i32* %89, null
br i1 %90, label %91, label %98
91: ; preds = %88
%92 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store i32 0, i32* %92
%93 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i64 0, i64* %93
%94 = load i32, i32* %6
%95 = load i32, i32* @SOL_SOCKET
%96 = load i32, i32* @SO_RCVTIMEO
%97 = call i32 @setsockopt(i32 %94, i32 %95, i32 %96, %struct.timeval* %14, i32 16)
br label %98
98: ; preds = %91, %88
%99 = load i8*, i8** %7
store i8 0, i8* %99
%100 = load i32, i32* %11
store i32 %100, i32* %5
br label %101
101: ; preds = %98, %55, %36
%102 = load i32, i32* %5
ret i32 %102
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@LOG_INFO = global i32 0
@.str = [52 x i8] c"Request takes too long to come. Connection aborted.\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @sockreadline(i32 %0, i8* nocapture writeonly %1, i32 %2, i32* %3) {
%5 = alloca i8
%6 = alloca %struct.timeval
%7 = bitcast %struct.timeval* %6 to i8*
%8 = icmp eq i32* %3, null
br i1 %8, label %15, label %9
9: ; preds = %4
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 1, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_RCVTIMEO
%14 = call i32 @setsockopt(i32 %0, i32 %12, i32 %13, %struct.timeval* %6, i32 16)
br label %15
15: ; preds = %9, %4
%16 = add nsw i32 %2, -1
br label %17
17: ; preds = %53, %15
%18 = phi i8* [ %55, %53 ], [ %1, %15 ]
%19 = phi i32 [ %54, %53 ], [ 0, %15 ]
%20 = phi i32 [ 1, %53 ], [ 0, %15 ]
%21 = icmp slt i32 %19, %16
br label %22
22: ; preds = %17, %52
%23 = phi i32 [ 1, %52 ], [ %20, %17 ]
br label %24
24: ; preds = %22, %50
%25 = phi i32 [ %23, %22 ], [ 1, %50 ]
br label %26
26: ; preds = %24, %37
%27 = call i32 @read(i32 %0, i8* %5, i32 1)
br i1 %8, label %36, label %28
28: ; preds = %26
%29 = call i32 @time(i32* null)
%30 = load i32, i32* %3
%31 = sub nsw i32 %29, %30
%32 = icmp sgt i32 %31, 9
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @LOG_INFO
%35 = call i32 @syslog(i32 %34, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
br label %65
36: ; preds = %28, %26
switch i32 %27, label %50 [
i32 -1, label %37
i32 0, label %47
]
37: ; preds = %36
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
%41 = load i64, i64* @EAGAIN
%42 = icmp eq i64 %38, %41
%43 = select i1 %40, i1 true, i1 %42
%44 = load i64, i64* @EWOULDBLOCK
%45 = icmp eq i64 %38, %44
%46 = select i1 %43, i1 true, i1 %45
br i1 %46, label %26, label %65
47: ; preds = %36
%48 = icmp eq i32 %25, 0
%49 = select i1 %48, i32 -1, i32 %19
br label %56
50: ; preds = %36
%51 = load i8, i8* %5
switch i8 %51, label %52 [
i8 13, label %24
i8 10, label %56
]
52: ; preds = %50
br i1 %21, label %53, label %22
53: ; preds = %52
%54 = add nuw nsw i32 %19, 1
%55 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %51, i8* %18
br label %17
56: ; preds = %50, %47
%57 = phi i32 [ %49, %47 ], [ %19, %50 ]
br i1 %8, label %64, label %58
58: ; preds = %56
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 0, i32* %59
%60 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %60
%61 = load i32, i32* @SOL_SOCKET
%62 = load i32, i32* @SO_RCVTIMEO
%63 = call i32 @setsockopt(i32 %0, i32 %61, i32 %62, %struct.timeval* %6, i32 16)
br label %64
64: ; preds = %58, %56
store i8 0, i8* %18
br label %65
65: ; preds = %37, %64, %33
%66 = phi i32 [ -1, %33 ], [ %57, %64 ], [ -1, %37 ]
ret i32 %66
}