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

/* Variables and functions */
int /*<<< orphan*/ dv_error(char *);
scalar_t__ errno;
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int strlen(char *);
void dv_getline(char *s, int size, FILE *stream) {
  if (errno) {
    dv_error("");
  }
  if (fgets(s, size, stream) == NULL) {
    if (ferror(stream)) {
      dv_error("Failed to read line from file.");
    }
    if (feof(stream)) {
      dv_error("Failed to read from file because EOF.");
    }
    dv_error("Failed to read line from file.");
  }
  if (strlen(s) == size - sizeof('\0')) {
    dv_error("One line length is too long for buffer size.");
  }
}


x86 assembly (clang, -O0):
<dv_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e76(%rip),%rax
cmpq   $0x0,(%rax)
je     1180 <dv_getline+0x30>
lea    0xec9(%rip),%rdi
call   1070 <dv_error@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11e4 <dv_getline+0x94>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11b9 <dv_getline+0x69>
lea    0xe4c(%rip),%rdi
call   1070 <dv_error@plt>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     11d8 <dv_getline+0x88>
lea    0xe4c(%rip),%rdi
call   1070 <dv_error@plt>
lea    0xe21(%rip),%rdi
call   1070 <dv_error@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cltq
movslq -0xc(%rbp),%rcx
sub    $0x4,%rcx
cmp    %rcx,%rax
jne    120c <dv_getline+0xbc>
lea    0xe3e(%rip),%rdi
call   1070 <dv_error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dv_getline>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e7c(%rip),%rax
cmpq   $0x0,(%rax)
je     1176 <dv_getline+0x26>
lea    0xed3(%rip),%rdi
call   1070 <dv_error@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c7 <dv_getline+0x77>
mov    %rbp,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11a2 <dv_getline+0x52>
lea    0xe63(%rip),%rdi
call   1070 <dv_error@plt>
mov    %rbp,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     11bb <dv_getline+0x6b>
lea    0xe69(%rip),%rdi
call   1070 <dv_error@plt>
lea    0xe3e(%rip),%rdi
call   1070 <dv_error@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movslq %r14d,%rcx
add    $0xfffffffffffffffc,%rcx
cmp    %rax,%rcx
jne    11ed <dv_getline+0x9d>
lea    0xe61(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <dv_error@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [31 x i8] c"Failed to read line from file.\00"
@.str.2 = [38 x i8] c"Failed to read from file because EOF.\00"
@.str.3 = [45 x i8] c"One line length is too long for buffer size.\00"
define void @dv_getline(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%7 = load i64, i64* @errno
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = load i32*, i32** %6
%15 = call i32* @fgets(i8* %12, i32 %13, i32* %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %31
17: ; preds = %11
%18 = load i32*, i32** %6
%19 = call i64 @ferror(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @dv_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i32*, i32** %6
%25 = call i64 @feof(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @dv_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = call i32 @dv_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %11
%32 = load i8*, i8** %4
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = sub i64 %36, 4
%38 = icmp eq i64 %34, %37
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 @dv_error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %31
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [31 x i8] c"Failed to read line from file.\00"
@.str.2 = [38 x i8] c"Failed to read from file because EOF.\00"
@.str.3 = [45 x i8] c"One line length is too long for buffer size.\00"
define void @dv_getline(i8* %0, i32 %1, i32* nocapture %2) {
%4 = load i64, i64* @errno
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = tail call i32* @fgets(i8* %0, i32 %1, i32* %2)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = tail call i64 @ferror(i32* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @dv_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = tail call i64 @feof(i32* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @dv_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = tail call i32 @dv_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %8
%24 = tail call i32 @strlen(i8* %0)
%25 = sext i32 %24 to i64
%26 = sext i32 %1 to i64
%27 = add nsw i64 %26, -4
%28 = icmp eq i64 %27, %25
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = tail call i32 @dv_error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
ret void
}