#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 /*<<< orphan*/ c;

/* Variables and functions */
int read(int, char *, int);
int readLine(int fd, char *buffer, int bufferLen) {
  int lineLength = 0;
  while (lineLength < bufferLen) {
    char c;
    int readResult = read(fd, &c, sizeof(c));
    if (readResult <= 0) {
      lineLength = readResult;
      break;
    }
    if (c == '\n') {
      break;
    }
    buffer[lineLength] = c;
    lineLength++;
  }
  return lineLength;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118c <readLine+0x7c>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     115e <readLine+0x4e>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    118c <readLine+0x7c>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1170 <readLine+0x60>
jmp    118c <readLine+0x7c>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <readLine+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    115e <readLine+0x4e>
mov    %rsi,%r15
mov    %edi,%ebp
mov    %edx,%r12d
xor    %ebx,%ebx
lea    0xf(%rsp),%r14
nop
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1164 <readLine+0x54>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     1162 <readLine+0x52>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <readLine+0x20>
mov    %r12d,%eax
jmp    1164 <readLine+0x54>
xor    %eax,%eax
jmp    1164 <readLine+0x54>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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