#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 */
typedef int ssize_t;

/* Variables and functions */
int recv(int, char *, int, int /*<<< orphan*/);
ssize_t readLine(int sockfd, void *buf, size_t len) {
  char *sbuf = (char *)buf;
  int i = 0;
  while (i <= (int)len) {
    char byte;
    if (recv(sockfd, &byte, 1, 0) != 1) {
      return -1;
    }
    sbuf[i++] = byte;
    if (byte == '\n') {
      sbuf[i++] = 0;
      break;
    }
  }
  return i - 2;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jg     11ae <readLine+0x9e>
mov    -0x8(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x1,%eax
je     1169 <readLine+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <readLine+0xa7>
mov    -0x25(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x25(%rbp),%eax
cmp    $0xa,%eax
jne    11a9 <readLine+0x99>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ae <readLine+0x9e>
jmp    1132 <readLine+0x22>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %r14d,%r14d
js     1179 <readLine+0x69>
mov    %rsi,%r12
mov    %edi,%r13d
add    $0x1,%r14d
mov    %r14,%rbp
neg    %rbp
mov    $0x1,%ebx
lea    0x7(%rsp),%r15
nopl   (%rax)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x1,%eax
jne    117e <readLine+0x6e>
movzbl 0x7(%rsp),%eax
mov    %al,-0x1(%r12,%rbx,1)
cmp    $0xa,%al
je     1186 <readLine+0x76>
lea    (%rbx,%rbp,1),%rax
add    $0x1,%rax
add    $0x1,%rbx
cmp    $0x1,%rax
jne    1140 <readLine+0x30>
jmp    1193 <readLine+0x83>
xor    %r14d,%r14d
jmp    1193 <readLine+0x83>
mov    $0xffffffff,%r14d
jmp    1197 <readLine+0x87>
mov    %ebx,%eax
add    $0x1,%ebx
movb   $0x0,(%r12,%rax,1)
mov    %ebx,%r14d
add    $0xfffffffe,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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