#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*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getchar();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int read_line(char *buf, int buf_size, int *line) {
  int ch;
  buf[buf_size - 1] = buf[buf_size - 2] = '\1';
  if (fgets(buf, buf_size, stdin) == NULL)
    return 0;
  ++*line;
  if (buf[buf_size - 1] == '\0' && buf[buf_size - 2] != '\n') {
    do {
      ch = getchar();
    } while (ch != EOF && ch != '\n');
    fprintf(stderr, "warning: truncated line %d\n", *line);
  }
  return 1;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
call   1030 <fgets@plt>
cmp    $0x0,%rax
jne    1190 <read_line+0x60>
movl   $0x0,-0x4(%rbp)
jmp    122c <read_line+0xfc>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1225 <read_line+0xf5>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1225 <read_line+0xf5>
jmp    11d4 <read_line+0xa4>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
mov    0x2df6(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x25(%rbp)
je     11ff <read_line+0xcf>
cmpl   $0xa,-0x24(%rbp)
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11d4 <read_line+0xa4>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xde0(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
movslq %esi,%rbp
movw   $0x101,-0x2(%rbp,%rdi,1)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%esi
call   1030 <fgets@plt>
test   %rax,%rax
je     11aa <read_line+0x7a>
addl   $0x1,(%r14)
mov    $0x1,%r15d
cmpb   $0x0,-0x1(%rbx,%rbp,1)
jne    11ad <read_line+0x7d>
cmpb   $0xa,-0x2(%rbx,%rbp,1)
je     11ad <read_line+0x7d>
mov    0x2e65(%rip),%rbx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
je     1190 <read_line+0x60>
cmp    (%rbx),%eax
jne    1180 <read_line+0x50>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%edx
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11ad <read_line+0x7d>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@EOF = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"warning: truncated line %d\0A\00"
define i32 @read_line(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = sub nsw i32 %10, 2
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
store i8 1, i8* %13
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = sub nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
store i8 1, i8* %18
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32, i32* @stdin
%22 = call i32* @fgets(i8* %19, i32 %20, i32 %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %3
store i32 0, i32* %4
br label %64
25: ; preds = %3
%26 = load i32*, i32** %7
%27 = load i32, i32* %26
%28 = add nsw i32 %27, 1
store i32 %28, i32* %26
%29 = load i8*, i8** %5
%30 = load i32, i32* %6
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %63
37: ; preds = %25
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = sub nsw i32 %39, 2
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 10
br i1 %45, label %46, label %63
46: ; preds = %37
br label %47
47: ; preds = %56, %46
%48 = call i32 (...) @getchar()
store i32 %48, i32* %8
br label %49
49: ; preds = %47
%50 = load i32, i32* %8
%51 = load i32, i32* @EOF
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 10
br label %56
56: ; preds = %53, %49
%57 = phi i1 [ false, %49 ], [ %55, %53 ]
br i1 %57, label %47, label %58
58: ; preds = %56
%59 = load i32, i32* @stderr
%60 = load i32*, i32** %7
%61 = load i32, i32* %60
%62 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %58, %37, %25
store i32 1, i32* %4
br label %64
64: ; preds = %63, %24
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"warning: truncated line %d\0A\00"
define i32 @read_line(i8* %0, i32 %1, i32* nocapture %2) {
%4 = add nsw i32 %1, -2
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 1, i8* %6
%7 = add nsw i32 %1, -1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 1, i8* %9
%10 = load i32, i32* @stdin
%11 = tail call i32* @fgets(i8* %0, i32 %1, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %31, label %13
13: ; preds = %3
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
store i32 %15, i32* %2
%16 = load i8, i8* %9
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = load i8, i8* %6
%20 = icmp eq i8 %19, 10
br i1 %20, label %31, label %21
21: ; preds = %18, %21
%22 = tail call i32 (...) @getchar()
%23 = load i32, i32* @EOF
%24 = icmp ne i32 %22, %23
%25 = icmp ne i32 %22, 10
%26 = and i1 %25, %24
br i1 %26, label %21, label %27
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i32, i32* %2
%30 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %13, %18, %27, %3
%32 = phi i32 [ 0, %3 ], [ 1, %27 ], [ 1, %18 ], [ 1, %13 ]
ret i32 %32
}