#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 */
struct iobuf {
  int dummy;
};

/* Variables and functions */
char *iobuf_data(struct iobuf *);
int /*<<< orphan*/ iobuf_drop(struct iobuf *, size_t);
size_t iobuf_len(struct iobuf *);
char *iobuf_getline(struct iobuf *iobuf, size_t *rlen) {
  char *buf;
  size_t len, i;
  buf = iobuf_data(iobuf);
  len = iobuf_len(iobuf);
  for (i = 0; i + 1 <= len; i++)
    if (buf[i] == '\n') {
      iobuf_drop(iobuf, i + 1);
      len = (i && buf[i - 1] == '\r') ? i - 1 : i;
      buf[len] = '\0';
      if (rlen)
        *rlen = len;
      return (buf);
    }
  return (NULL);
}


x86 assembly (clang, -O0):
<iobuf_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <iobuf_data@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <iobuf_len@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
cmp    -0x28(%rbp),%rax
ja     1224 <iobuf_getline+0xf4>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    120e <iobuf_getline+0xde>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x1,%rsi
call   1030 <iobuf_drop@plt>
cmpq   $0x0,-0x30(%rbp)
je     11cf <iobuf_getline+0x9f>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11cf <iobuf_getline+0x9f>
mov    -0x30(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11d7 <iobuf_getline+0xa7>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     1201 <iobuf_getline+0xd1>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122c <iobuf_getline+0xfc>
jmp    1213 <iobuf_getline+0xe3>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1162 <iobuf_getline+0x32>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iobuf_getline>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <iobuf_data@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1050 <iobuf_len@plt>
xor    %ecx,%ecx
lea    0x1(%rcx),%rbx
cmp    %rax,%rbx
ja     1182 <iobuf_getline+0x52>
cmpb   $0xa,(%r15,%rcx,1)
mov    %rbx,%rcx
jne    1150 <iobuf_getline+0x20>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <iobuf_drop@plt>
cmp    $0x1,%rbx
je     1187 <iobuf_getline+0x57>
cmpb   $0xd,-0x2(%r15,%rbx,1)
jne    1187 <iobuf_getline+0x57>
add    $0xfffffffffffffffe,%rbx
jmp    118b <iobuf_getline+0x5b>
xor    %r15d,%r15d
jmp    1198 <iobuf_getline+0x68>
add    $0xffffffffffffffff,%rbx
movb   $0x0,(%r15,%rbx,1)
test   %r14,%r14
je     1198 <iobuf_getline+0x68>
mov    %rbx,(%r14)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.iobuf = type { i32 }
define i8* @iobuf_getline(%struct.iobuf* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca %struct.iobuf*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store %struct.iobuf* %0, %struct.iobuf** %4
store i64* %1, i64** %5
%9 = load %struct.iobuf*, %struct.iobuf** %4
%10 = call i8* @iobuf_data(%struct.iobuf* %9)
store i8* %10, i8** %6
%11 = load %struct.iobuf*, %struct.iobuf** %4
%12 = call i64 @iobuf_len(%struct.iobuf* %11)
store i64 %12, i64* %7
store i64 0, i64* %8
br label %13
13: ; preds = %58, %2
%14 = load i64, i64* %8
%15 = add i64 %14, 1
%16 = load i64, i64* %7
%17 = icmp ule i64 %15, %16
br i1 %17, label %18, label %61
18: ; preds = %13
%19 = load i8*, i8** %6
%20 = load i64, i64* %8
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %57
25: ; preds = %18
%26 = load %struct.iobuf*, %struct.iobuf** %4
%27 = load i64, i64* %8
%28 = add i64 %27, 1
%29 = call i32 @iobuf_drop(%struct.iobuf* %26, i64 %28)
%30 = load i64, i64* %8
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %25
%33 = load i8*, i8** %6
%34 = load i64, i64* %8
%35 = sub i64 %34, 1
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 13
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i64, i64* %8
%42 = sub i64 %41, 1
br label %45
43: ; preds = %32, %25
%44 = load i64, i64* %8
br label %45
45: ; preds = %43, %40
%46 = phi i64 [ %42, %40 ], [ %44, %43 ]
store i64 %46, i64* %7
%47 = load i8*, i8** %6
%48 = load i64, i64* %7
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = load i64*, i64** %5
%51 = icmp ne i64* %50, null
br i1 %51, label %52, label %55
52: ; preds = %45
%53 = load i64, i64* %7
%54 = load i64*, i64** %5
store i64 %53, i64* %54
br label %55
55: ; preds = %52, %45
%56 = load i8*, i8** %6
store i8* %56, i8** %3
br label %62
57: ; preds = %18
br label %58
58: ; preds = %57
%59 = load i64, i64* %8
%60 = add i64 %59, 1
store i64 %60, i64* %8
br label %13
61: ; preds = %13
store i8* null, i8** %3
br label %62
62: ; preds = %61, %55
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
%struct.iobuf = type { i32 }
define i8* @iobuf_getline(%struct.iobuf* %0, i64* writeonly %1) {
%3 = tail call i8* @iobuf_data(%struct.iobuf* %0)
%4 = tail call i64 @iobuf_len(%struct.iobuf* %0)
br label %5
5: ; preds = %9, %2
%6 = phi i64 [ 0, %2 ], [ %7, %9 ]
%7 = add i64 %6, 1
%8 = icmp ugt i64 %7, %4
br i1 %8, label %27, label %9
9: ; preds = %5
%10 = getelementptr inbounds i8, i8* %3, i64 %6
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %5
13: ; preds = %9
%14 = tail call i32 @iobuf_drop(%struct.iobuf* %0, i64 %7)
%15 = icmp eq i64 %6, 0
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = add i64 %6, -1
%18 = getelementptr inbounds i8, i8* %3, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 13
br i1 %20, label %22, label %21
21: ; preds = %16, %13
br label %22
22: ; preds = %16, %21
%23 = phi i64 [ %6, %21 ], [ %17, %16 ]
%24 = getelementptr inbounds i8, i8* %3, i64 %23
store i8 0, i8* %24
%25 = icmp eq i64* %1, null
br i1 %25, label %27, label %26
26: ; preds = %22
store i64 %23, i64* %1
br label %27
27: ; preds = %5, %22, %26
%28 = phi i8* [ %3, %26 ], [ %3, %22 ], [ null, %5 ]
ret i8* %28
}