#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 size) {
  int i = 0;
  char ch;
  for (i = 0; i < size; ++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,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a4 <read_line+0x94>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
mov    $0x1,%eax
cmp    -0x20(%rbp),%eax
jne    1185 <read_line+0x75>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1180 <read_line+0x70>
jmp    11a4 <read_line+0x94>
jmp    1191 <read_line+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <read_line+0x9d>
jmp    1196 <read_line+0x86>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1130 <read_line+0x20>
mov    -0x18(%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 i32
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %8
br label %11
11: ; preds = %33, %3
%12 = load i32, i32* %8
%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* %9, 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* %9
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 %21, i8* %25
%26 = load i8, i8* %9
%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* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %11
36: ; preds = %29, %11
%37 = load i32, i32* %8
%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
}