#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 */
long recv(int, char *, int, int /*<<< orphan*/);
long readline(int sock, char *buf, long maxlen) {
  long res, bufpos = 0;
  char bytebuff;
  for (;;) {
    res = recv(sock, &bytebuff, 1, 0);
    if (res < 1)
      break;
    if (bytebuff == '\r')
      continue;
    if (bytebuff == '\n')
      break;
    buf[bufpos++] = bytebuff;
    if (bufpos >= maxlen - 1)
      break;
  }
  buf[bufpos] = 0;
  return (bufpos);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jge    1152 <readline+0x42>
jmp    11ae <readline+0x9e>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    1164 <readline+0x54>
jmp    112b <readline+0x1b>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    1176 <readline+0x66>
jmp    11ae <readline+0x9e>
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jl     11a9 <readline+0x99>
jmp    11ae <readline+0x9e>
jmp    112b <readline+0x1b>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
add    $0xffffffffffffffff,%rdx
cmp    $0x2,%rdx
mov    $0x1,%r12d
cmovge %rdx,%r12
xor    %r15d,%r15d
lea    0xf(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    1173 <readline+0x63>
movzbl 0xf(%rsp),%eax
cmp    $0xd,%al
je     1140 <readline+0x30>
cmp    $0xa,%al
je     1173 <readline+0x63>
mov    %al,(%r14,%r15,1)
add    $0x1,%r15
cmp    %r12,%r15
jne    1140 <readline+0x30>
mov    %r12,%r15
movb   $0x0,(%r14,%r15,1)
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @readline(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
%5 = add i64 %2, -1
%6 = call i64 @llvm.smax.i64(i64 %5, i64 1)
br label %7
7: ; preds = %14, %3
%8 = phi i64 [ %15, %14 ], [ 0, %3 ]
br label %9
9: ; preds = %7, %12
%10 = call i64 @recv(i32 %0, i8* %4, i32 1, i32 0)
%11 = icmp slt i64 %10, 1
br i1 %11, label %18, label %12
12: ; preds = %9
%13 = load i8, i8* %4
switch i8 %13, label %14 [
i8 13, label %9
i8 10, label %18
]
14: ; preds = %12
%15 = add nuw nsw i64 %8, 1
%16 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 %13, i8* %16
%17 = icmp eq i64 %15, %6
br i1 %17, label %18, label %7
18: ; preds = %14, %12, %9
%19 = phi i64 [ %8, %9 ], [ %8, %12 ], [ %6, %14 ]
%20 = getelementptr inbounds i8, i8* %1, i64 %19
store i8 0, i8* %20
ret i64 %19
}