#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 */
unsigned int read(int, unsigned char *, int);
unsigned int read_line(int skt, unsigned char *buffer, unsigned int read_len) {
  unsigned char c, *ptr;
  unsigned int n, rc;
  ptr = buffer;
  read_len = read_len - 2;
  for (n = 1; n < read_len; n++) {
    if ((rc = read(skt, &c, 1)) == 1) {
      *ptr++ = c;
      if (c == '\n') {
        break;
      }
    } else if (rc == 0) {
      if (n == 1)
        return 0;
      else
        break;
    } else
      return -1;
  }
  *ptr = 0;
  return n;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11d2 <read_line+0xc2>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x1,%eax
jne    118e <read_line+0x7e>
mov    -0x15(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
movzbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1189 <read_line+0x79>
jmp    11d2 <read_line+0xc2>
jmp    11bf <read_line+0xaf>
cmpl   $0x0,-0x28(%rbp)
jne    11b3 <read_line+0xa3>
cmpl   $0x1,-0x24(%rbp)
jne    11ae <read_line+0x9e>
movl   $0x0,-0x4(%rbp)
jmp    11df <read_line+0xcf>
jmp    11d2 <read_line+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <read_line+0xcf>
jmp    11c4 <read_line+0xb4>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113a <read_line+0x2a>
mov    -0x20(%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):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    -0x2(%rdx),%r12d
mov    $0x1,%ebx
cmp    $0x2,%r12d
jb     1184 <read_line+0x74>
mov    %edi,%r15d
mov    $0x3,%r13d
sub    %edx,%r13d
mov    $0x1,%ebx
lea    0x7(%rsp),%r14
mov    %rsi,%rbp
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    119c <read_line+0x8c>
movzbl 0x7(%rsp),%eax
mov    %al,0x0(%rbp)
cmp    $0xa,%al
je     117e <read_line+0x6e>
lea    0x1(%rbp),%rsi
lea    (%rbx,%r13,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    1140 <read_line+0x30>
mov    %r12d,%ebx
add    $0x1,%rbp
jmp    1187 <read_line+0x77>
mov    %rsi,%rbp
movb   $0x0,0x0(%rbp)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %eax,%eax
jne    11a9 <read_line+0x99>
xor    %eax,%eax
cmp    $0x1,%ebx
jne    1187 <read_line+0x77>
jmp    118d <read_line+0x7d>
mov    $0xffffffff,%eax
jmp    118d <read_line+0x7d>

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 i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
store i8* %12, i8** %9
%13 = load i32, i32* %7
%14 = sub i32 %13, 2
store i32 %14, i32* %7
store i32 1, i32* %10
br label %15
15: ; preds = %42, %3
%16 = load i32, i32* %10
%17 = load i32, i32* %7
%18 = icmp ult i32 %16, %17
br i1 %18, label %19, label %45
19: ; preds = %15
%20 = load i32, i32* %5
%21 = call i32 @read(i32 %20, i8* %8, i32 1)
store i32 %21, i32* %11
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i8, i8* %8
%25 = load i8*, i8** %9
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %9
store i8 %24, i8* %25
%27 = load i8, i8* %8
%28 = zext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %23
br label %45
31: ; preds = %23
br label %41
32: ; preds = %19
%33 = load i32, i32* %11
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 0, i32* %4
br label %48
39: ; preds = %35
br label %45
40: ; preds = %32
store i32 -1, i32* %4
br label %48
41: ; preds = %31
br label %42
42: ; preds = %41
%43 = load i32, i32* %10
%44 = add i32 %43, 1
store i32 %44, i32* %10
br label %15
45: ; preds = %39, %30, %15
%46 = load i8*, i8** %9
store i8 0, i8* %46
%47 = load i32, i32* %10
store i32 %47, i32* %4
br label %48
48: ; preds = %45, %40, %38
%49 = load i32, i32* %4
ret i32 %49
}

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