#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 strlen(char const *);
char *strstr(char *, char *);
char *read_next_line(const char *buffer, int *bytes_read) {
  char *new_line = strstr((char *)buffer, "\r\n");
  if (new_line == NULL) {
    *bytes_read = strlen(buffer);
    return NULL;
  }
  *bytes_read = new_line + 2 - buffer;
  return new_line + 2;
}


x86 assembly (clang, -O0):
<read_next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116d <read_next_line+0x4d>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1190 <read_next_line+0x70>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_next_line>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xecf(%rip),%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1145 <read_next_line+0x25>
add    $0x2,%rax
mov    %eax,%ecx
sub    %ebx,%ecx
jmp    1151 <read_next_line+0x31>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
mov    %ecx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
define i8* @read_next_line(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%7 = load i8*, i8** %4
%8 = call i8* @strstr(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = load i32*, i32** %5
store i32 %13, i32* %14
store i8* null, i8** %3
br label %26
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 2
%18 = load i8*, i8** %4
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = load i32*, i32** %5
store i32 %22, i32* %23
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8* %25, i8** %3
br label %26
26: ; preds = %15, %11
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
define i8* @read_next_line(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
br label %13
7: ; preds = %2
%8 = getelementptr inbounds i8, i8* %3, i64 2
%9 = ptrtoint i8* %8 to i64
%10 = ptrtoint i8* %0 to i64
%11 = sub i64 %9, %10
%12 = trunc i64 %11 to i32
br label %13
13: ; preds = %7, %5
%14 = phi i32 [ %12, %7 ], [ %6, %5 ]
%15 = phi i8* [ %8, %7 ], [ null, %5 ]
store i32 %14, i32* %1
ret i8* %15
}