#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 sock_peek(long, char *, unsigned long, unsigned long);
int sock_read(long, char *, int, unsigned long);
char *strstr(char *, char *);
int sock_readLine(long int sock, char buf[], unsigned long buflen,
                  unsigned long timeout) {
  int bytesToRead;
  int received = 0;
  int length = 0;
  char *eolnAt = 0;
  do {
    length = sock_peek(sock, buf + received, buflen - received, timeout);
    if (length > 0) {
      eolnAt = strstr(buf, "\n");
      if (eolnAt)
        bytesToRead = (eolnAt - buf) - received + 1;
      else
        bytesToRead = length;
      length = sock_read(sock, buf + received, bytesToRead + 1, timeout);
    }
    if (length > 0)
      received = received + length;
  } while ((length > 0) && !eolnAt);
  return length;
}


x86 assembly (clang, -O0):
<sock_readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x28(%rbp),%rax
sub    %rax,%rdx
mov    -0x20(%rbp),%rcx
call   1030 <sock_peek@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11f2 <sock_readLine+0xc2>
mov    -0x10(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11cb <sock_readLine+0x9b>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x28(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
jmp    11d1 <sock_readLine+0xa1>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
add    $0x1,%edx
mov    -0x20(%rbp),%rcx
call   1040 <sock_read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    1205 <sock_readLine+0xd5>
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    120a <sock_readLine+0xda>
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x39(%rbp)
jle    1226 <sock_readLine+0xf6>
cmpq   $0x0,-0x38(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    115e <sock_readLine+0x2e>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_readLine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,0x10(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r13
xor    %eax,%eax
mov    %rsi,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%r15d
lea    (%rbx,%r15,1),%r12
mov    0x10(%rsp),%rdx
sub    %r15,%rdx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rcx
call   1040 <sock_peek@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d6 <sock_readLine+0xa6>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %r14,%rcx
mov    %r13,%rdi
mov    %rax,%r14
mov    %r14d,%edx
sub    %ebx,%edx
sub    %r15d,%edx
add    $0x1,%edx
test   %r14,%r14
cmove  %ebp,%edx
add    $0x1,%edx
mov    %r12,%rsi
mov    %rcx,%rbx
call   1050 <sock_read@plt>
mov    %eax,%ebp
mov    $0x0,%eax
test   %ebp,%ebp
cmovns %ebp,%eax
jle    11d6 <sock_readLine+0xa6>
add    %r15d,%eax
test   %r14,%r14
mov    %rbx,%r14
mov    0x8(%rsp),%rbx
je     1160 <sock_readLine+0x30>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
define i32 @sock_readLine(i64 %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i64 %0, i64* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
store i32 0, i32* %10
store i32 0, i32* %11
store i8* null, i8** %12
br label %13
13: ; preds = %70, %4
%14 = load i64, i64* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %10
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i64, i64* %7
%20 = load i32, i32* %10
%21 = sext i32 %20 to i64
%22 = sub i64 %19, %21
%23 = load i64, i64* %8
%24 = call i32 @sock_peek(i64 %14, i8* %18, i64 %22, i64 %23)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %55
27: ; preds = %13
%28 = load i8*, i8** %6
%29 = call i8* @strstr(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %29, i8** %12
%30 = load i8*, i8** %12
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %43
32: ; preds = %27
%33 = load i8*, i8** %12
%34 = load i8*, i8** %6
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = sub nsw i64 %37, %39
%41 = add nsw i64 %40, 1
%42 = trunc i64 %41 to i32
store i32 %42, i32* %9
br label %45
43: ; preds = %27
%44 = load i32, i32* %11
store i32 %44, i32* %9
br label %45
45: ; preds = %43, %32
%46 = load i64, i64* %5
%47 = load i8*, i8** %6
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
%53 = load i64, i64* %8
%54 = call i32 @sock_read(i64 %46, i8* %50, i32 %52, i64 %53)
store i32 %54, i32* %11
br label %55
55: ; preds = %45, %13
%56 = load i32, i32* %11
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = load i32, i32* %10
%60 = load i32, i32* %11
%61 = add nsw i32 %59, %60
store i32 %61, i32* %10
br label %62
62: ; preds = %58, %55
br label %63
63: ; preds = %62
%64 = load i32, i32* %11
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i8*, i8** %12
%68 = icmp ne i8* %67, null
%69 = xor i1 %68, true
br label %70
70: ; preds = %66, %63
%71 = phi i1 [ false, %63 ], [ %69, %66 ]
br i1 %71, label %13, label %72
72: ; preds = %70
%73 = load i32, i32* %11
ret i32 %73
}

LLVM IR (-O2):
define i32 @sock_readLine(i64 %0, i8* %1, i64 %2, i64 %3) {
%5 = ptrtoint i8* %1 to i64
br label %6
6: ; preds = %13, %4
%7 = phi i32 [ 0, %4 ], [ %26, %13 ]
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub i64 %2, %8
%11 = tail call i32 @sock_peek(i64 %0, i8* %9, i64 %10, i64 %3)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %6
%14 = tail call i8* @strchr(i8* dereferenceable(1) %1, i32 10)
%15 = icmp eq i8* %14, null
%16 = ptrtoint i8* %14 to i64
%17 = add i64 %8, %5
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = add i32 %19, 1
%21 = select i1 %15, i32 %11, i32 %20
%22 = add nsw i32 %21, 1
%23 = tail call i32 @sock_read(i64 %0, i8* %9, i32 %22, i64 %3)
%24 = icmp sgt i32 %23, 0
%25 = select i1 %24, i32 %23, i32 0
%26 = add nuw nsw i32 %25, %7
%27 = select i1 %24, i1 %15, i1 false
br i1 %27, label %6, label %28
28: ; preds = %6, %13
%29 = phi i32 [ %23, %13 ], [ %11, %6 ]
ret i32 %29
}