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


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
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jge    11c6 <read_line+0xa6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jne    119b <read_line+0x7b>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1196 <read_line+0x76>
jmp    11c6 <read_line+0xa6>
jmp    11b3 <read_line+0x93>
lea    0xe5e(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <read_line+0xc0>
jmp    11b8 <read_line+0x98>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1140 <read_line+0x20>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
xor    %ebp,%ebp
cmp    $0x3,%edx
jl     117c <read_line+0x5c>
mov    %edx,%r14d
mov    %edi,%r12d
add    $0xfffffffe,%r14d
mov    %r14d,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%r15,%rbp,1),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x1,%eax
jne    1197 <read_line+0x77>
cmpb   $0xa,(%rbx)
je     117c <read_line+0x5c>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1150 <read_line+0x30>
mov    %r14d,%ebp
mov    %ebp,%eax
movb   $0x0,0x1(%r15,%rax,1)
add    $0x1,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe62(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1188 <read_line+0x68>

LLVM IR (-O0):
@.str = [10 x i8] c"readLine!\00"
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 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 %10
10: ; preds = %37, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 2
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %40
15: ; preds = %10
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @recv(i32 %16, i8* %20, i32 1, i32 0)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp eq i32 1, %22
br i1 %23, label %24, label %34
24: ; preds = %15
%25 = load i8*, i8** %6
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %33
32: ; preds = %24
br label %40
33: ; preds = %24
br label %36
34: ; preds = %15
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
36: ; preds = %33
br label %37
37: ; preds = %36
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %10
40: ; preds = %32, %10
%41 = load i8*, i8** %6
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
store i8 0, i8* %45
%46 = load i32, i32* %8
store i32 %46, i32* %4
br label %47
47: ; preds = %40, %34
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [10 x i8] c"readLine!\00"
define i32 @read_line(i32 %0, i8* %1, i32 %2) {
%4 = add i32 %2, -2
%5 = icmp sgt i32 %2, 2
br i1 %5, label %6, label %23
6: ; preds = %3
%7 = zext i32 %4 to i64
br label %8
8: ; preds = %6, %18
%9 = phi i64 [ 0, %6 ], [ %19, %18 ]
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = tail call i32 @recv(i32 %0, i8* %10, i32 1, i32 0)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i8, i8* %10
%15 = icmp eq i8 %14, 10
br i1 %15, label %21, label %18
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %28
18: ; preds = %13
%19 = add nuw nsw i64 %9, 1
%20 = icmp eq i64 %19, %7
br i1 %20, label %23, label %8
21: ; preds = %13
%22 = trunc i64 %9 to i32
br label %23
23: ; preds = %18, %21, %3
%24 = phi i32 [ 0, %3 ], [ %22, %21 ], [ %4, %18 ]
%25 = add nuw nsw i32 %24, 1
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %16, %23
%29 = phi i32 [ %25, %23 ], [ -1, %16 ]
ret i32 %29
}