#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 EOF;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int readline(char const *buffer, char *out, size_t max_len) {
  size_t i;
  for (i = 0; i < max_len; i++) {
    if (buffer[i] == '\n') {
      memcpy(out, buffer, i);
      out[i] = '\0';
      return i;
    }
  }
  return EOF;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    118e <readline+0x7e>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1178 <readline+0x68>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    119a <readline+0x8a>
jmp    117d <readline+0x6d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    112c <readline+0x1c>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     113f <readline+0x2f>
mov    %rsi,%r14
mov    %rdi,%rsi
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0xa,(%rsi,%rbx,1)
je     114a <readline+0x3a>
add    $0x1,%rbx
cmp    %rbx,%rdx
jne    1130 <readline+0x20>
mov    0x2ea2(%rip),%rax
mov    (%rax),%ebx
jmp    115a <readline+0x4a>
mov    %r14,%rdi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@EOF = global i32 0
define i32 @readline(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %14
%6 = phi i64 [ %15, %14 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 10
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = tail call i32 @memcpy(i8* %1, i8* %0, i64 %6)
%12 = getelementptr inbounds i8, i8* %1, i64 %6
store i8 0, i8* %12
%13 = trunc i64 %6 to i32
br label %19
14: ; preds = %5
%15 = add nuw i64 %6, 1
%16 = icmp eq i64 %15, %2
br i1 %16, label %17, label %5
17: ; preds = %14, %3
%18 = load i32, i32* @EOF
br label %19
19: ; preds = %17, %10
%20 = phi i32 [ %13, %10 ], [ %18, %17 ]
ret i32 %20
}