#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 /*<<< orphan*/ epicfail(char *);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
void extract_line(char *line, char *buf, int *buffer_pos) {
  char *crlf_pos = strstr(buf, "\r\n");
  int crlf_len = 2;
  if (crlf_pos == NULL) {
    crlf_pos = strstr(buf, "\n");
    crlf_len = 1;
  }
  if (crlf_pos == NULL)
    epicfail("No newline in buffer");
  strncpy(line, buf, crlf_pos - buf);
  int new_len = *buffer_pos - (crlf_pos - buf) - crlf_len;
  memmove(buf, crlf_pos + crlf_len, new_len);
  *buffer_pos = new_len;
}


x86 assembly (clang, -O0):
<extract_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
movl   $0x2,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1195 <extract_line+0x55>
mov    -0x10(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <extract_line+0x6c>
lea    0xe5c(%rip),%rdi
call   1040 <epicfail@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
movslq -0x24(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
call   1050 <memmove@plt>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbp
lea    0xe95(%rip),%rsi
mov    %r12,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     1183 <extract_line+0x33>
mov    %rax,%r13
mov    $0x2,%r15d
jmp    11af <extract_line+0x5f>
mov    %r12,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     11a0 <extract_line+0x50>
mov    %rax,%r13
jmp    11af <extract_line+0x5f>
lea    0xe5c(%rip),%rdi
call   1050 <epicfail@plt>
xor    %r13d,%r13d
mov    %r13d,%ebx
sub    %r12d,%ebx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    (%r14),%ebp
sub    %r15d,%ebp
sub    %ebx,%ebp
add    %r15,%r13
mov    %r12,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1060 <memmove@plt>
mov    %ebp,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [21 x i8] c"No newline in buffer\00"
define void @extract_line(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%10 = load i8*, i8** %5
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %7
store i32 2, i32* %8
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i8* @strstr(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %7
store i32 1, i32* %8
br label %17
17: ; preds = %14, %3
%18 = load i8*, i8** %7
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @epicfail(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = load i8*, i8** %7
%26 = load i8*, i8** %5
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = call i32 @strncpy(i8* %23, i8* %24, i32 %30)
%32 = load i32*, i32** %6
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = load i8*, i8** %7
%36 = load i8*, i8** %5
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = sub nsw i64 %34, %39
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = sub nsw i64 %40, %42
%44 = trunc i64 %43 to i32
store i32 %44, i32* %9
%45 = load i8*, i8** %5
%46 = load i8*, i8** %7
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i32, i32* %9
%51 = call i32 @memmove(i8* %45, i8* %49, i32 %50)
%52 = load i32, i32* %9
%53 = load i32*, i32** %6
store i32 %52, i32* %53
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
@.str.2 = [21 x i8] c"No newline in buffer\00"
define void @extract_line(i8* %0, i8* %1, i32* nocapture %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = tail call i8* @strchr(i8* dereferenceable(1) %1, i32 10)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @epicfail(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %3, %9, %6
%12 = phi i8* [ null, %9 ], [ %7, %6 ], [ %4, %3 ]
%13 = phi i64 [ 1, %9 ], [ 1, %6 ], [ 2, %3 ]
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %1 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
%18 = tail call i32 @strncpy(i8* %0, i8* %1, i32 %17)
%19 = load i32, i32* %2
%20 = add i64 %16, %13
%21 = trunc i64 %20 to i32
%22 = sub i32 %19, %21
%23 = getelementptr inbounds i8, i8* %12, i64 %13
%24 = tail call i32 @memmove(i8* %1, i8* %23, i32 %22)
store i32 %22, i32* %2
ret void
}