#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 recv(int, char *, int, int /*<<< orphan*/);
int read_line(int fd, char *buf, int maxsize) {
  char ch;
  int i;
  for (i = 0; i < maxsize; ++i) {
    int n = recv(fd, &ch, 1, 0);
    if (1 == n) {
      buf[i] = ch;
      if (ch == '\n')
        break;
    } else {
      return -1;
    }
  }
  return i + 1;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119d <read_line+0x8d>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
mov    $0x1,%eax
cmp    -0x20(%rbp),%eax
jne    117e <read_line+0x6e>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1179 <read_line+0x69>
jmp    119d <read_line+0x8d>
jmp    118a <read_line+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <read_line+0x96>
jmp    118f <read_line+0x7f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <read_line+0x19>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    1161 <read_line+0x51>
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%r12d
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x1,%eax
jne    1168 <read_line+0x58>
movzbl 0xf(%rsp),%eax
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1163 <read_line+0x53>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <read_line+0x20>
mov    %r12d,%ebx
jmp    1163 <read_line+0x53>
xor    %ebx,%ebx
add    $0x1,%ebx
jmp    116d <read_line+0x5d>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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