#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int ext_yahoo_read(void *, char *, int);
int yahoo_tcp_readline(char *ptr, int maxlen, void *fd) {
  int n, rc;
  char c;
  for (n = 1; n < maxlen; n++) {
    do {
      rc = ext_yahoo_read(fd, &c, 1);
    } while (rc == -1 && (errno == EINTR || errno == EAGAIN));
    if (rc == 1) {
      if (c == '\r')
        continue;
      *ptr = c;
      if (c == '\n')
        break;
      ptr++;
    } else if (rc == 0) {
      if (n == 1)
        return 0;
      else
        break;
    } else {
      return -1;
    }
  }
  *ptr = 0;
  return n;
}


x86 assembly (clang, -O0):
<yahoo_tcp_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1235 <yahoo_tcp_readline+0x125>
jmp    113b <yahoo_tcp_readline+0x2b>
mov    -0x20(%rbp),%rdi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <ext_yahoo_read@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x28(%rbp)
mov    %al,-0x2a(%rbp)
jne    119e <yahoo_tcp_readline+0x8e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e66(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x2b(%rbp)
je     1198 <yahoo_tcp_readline+0x88>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2b(%rbp)
mov    -0x2b(%rbp),%al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    113b <yahoo_tcp_readline+0x2b>
cmpl   $0x1,-0x28(%rbp)
jne    11f1 <yahoo_tcp_readline+0xe1>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    11c5 <yahoo_tcp_readline+0xb5>
jmp    1227 <yahoo_tcp_readline+0x117>
mov    -0x29(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    11e0 <yahoo_tcp_readline+0xd0>
jmp    1235 <yahoo_tcp_readline+0x125>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1222 <yahoo_tcp_readline+0x112>
cmpl   $0x0,-0x28(%rbp)
jne    1216 <yahoo_tcp_readline+0x106>
cmpl   $0x1,-0x24(%rbp)
jne    1211 <yahoo_tcp_readline+0x101>
movl   $0x0,-0x4(%rbp)
jmp    1242 <yahoo_tcp_readline+0x132>
jmp    1235 <yahoo_tcp_readline+0x125>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <yahoo_tcp_readline+0x132>
jmp    1227 <yahoo_tcp_readline+0x117>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    112a <yahoo_tcp_readline+0x1a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<yahoo_tcp_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x1,%r12d
cmp    $0x2,%esi
mov    %rdi,0x10(%rsp)
jl     11d5 <yahoo_tcp_readline+0xc5>
mov    %rdx,%rbx
mov    %esi,%r15d
mov    $0x1,%r12d
lea    0xf(%rsp),%rbp
mov    0x2e96(%rip),%r13
mov    0x2e87(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <ext_yahoo_read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
jne    1193 <yahoo_tcp_readline+0x83>
mov    0x0(%r13),%rcx
cmp    (%r14),%rcx
je     1160 <yahoo_tcp_readline+0x50>
mov    0x2e3c(%rip),%rdx
cmp    (%rdx),%rcx
je     1160 <yahoo_tcp_readline+0x50>
jmp    11e0 <yahoo_tcp_readline+0xd0>
test   %ecx,%ecx
je     11cd <yahoo_tcp_readline+0xbd>
cmp    $0x1,%ecx
jne    11e0 <yahoo_tcp_readline+0xd0>
movzbl 0xf(%rsp),%eax
cmp    $0xd,%al
je     11bf <yahoo_tcp_readline+0xaf>
mov    0x10(%rsp),%rcx
mov    %al,(%rcx)
cmpb   $0xa,0xf(%rsp)
je     11d5 <yahoo_tcp_readline+0xc5>
mov    %rcx,%rax
add    $0x1,%rax
mov    %rax,0x10(%rsp)
add    $0x1,%r12d
cmp    %r15d,%r12d
jne    1160 <yahoo_tcp_readline+0x50>
mov    %r15d,%r12d
jmp    11d5 <yahoo_tcp_readline+0xc5>
xor    %eax,%eax
cmp    $0x1,%r12d
je     11e0 <yahoo_tcp_readline+0xd0>
mov    0x10(%rsp),%rax
movb   $0x0,(%rax)
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @yahoo_tcp_readline(i8* nocapture writeonly %0, i32 %1, i8* %2) {
%4 = alloca i8
%5 = icmp sgt i32 %1, 1
br i1 %5, label %6, label %32
6: ; preds = %3, %28
%7 = phi i32 [ %30, %28 ], [ 1, %3 ]
%8 = phi i8* [ %29, %28 ], [ %0, %3 ]
br label %9
9: ; preds = %6, %11
%10 = call i32 @ext_yahoo_read(i8* %2, i8* %4, i32 1)
switch i32 %10, label %35 [
i32 -1, label %11
i32 1, label %18
i32 0, label %26
]
11: ; preds = %9
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %9, label %35
18: ; preds = %9
%19 = load i8, i8* %4
%20 = icmp eq i8 %19, 13
br i1 %20, label %28, label %21
21: ; preds = %18
store i8 %19, i8* %8
%22 = load i8, i8* %4
%23 = icmp eq i8 %22, 10
br i1 %23, label %32, label %24
24: ; preds = %21
%25 = getelementptr inbounds i8, i8* %8, i64 1
br label %28
26: ; preds = %9
%27 = icmp eq i32 %7, 1
br i1 %27, label %35, label %32
28: ; preds = %18, %24
%29 = phi i8* [ %8, %18 ], [ %25, %24 ]
%30 = add nuw nsw i32 %7, 1
%31 = icmp eq i32 %30, %1
br i1 %31, label %32, label %6
32: ; preds = %28, %21, %3, %26
%33 = phi i8* [ %8, %26 ], [ %0, %3 ], [ %29, %28 ], [ %8, %21 ]
%34 = phi i32 [ %7, %26 ], [ 1, %3 ], [ %1, %28 ], [ %7, %21 ]
store i8 0, i8* %33
br label %35
35: ; preds = %11, %9, %26, %32
%36 = phi i32 [ %34, %32 ], [ 0, %26 ], [ -1, %9 ], [ -1, %11 ]
ret i32 %36
}