#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 */
char EOF;
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/);
char getchar();
int /*<<< orphan*/ stdin;
int strlen(char *);
int read_line(char *buffer, size_t size) {
  char c;
  int extra = 0;
  if (fgets(buffer, size, stdin) == NULL) {
    return 4;
  }
  if (buffer[strlen(buffer) - 1] != '\n') {
    while (((c = getchar()) != '\n') && (c != EOF)) {
      extra = 1;
    }
    return (extra == 1) ? 5 : 0;
  }
  buffer[strlen(buffer) - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1173 <read_line+0x43>
movl   $0x4,-0x4(%rbp)
jmp    122f <read_line+0xff>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1207 <read_line+0xd7>
jmp    11a2 <read_line+0x72>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x29(%rbp)
je     11d3 <read_line+0xa3>
movsbl -0x19(%rbp),%eax
mov    0x2e18(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11e3 <read_line+0xb3>
jmp    11ef <read_line+0xbf>
movl   $0x1,-0x20(%rbp)
jmp    11a2 <read_line+0x72>
mov    -0x20(%rbp),%edx
xor    %eax,%eax
mov    $0x5,%ecx
cmp    $0x1,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    122f <read_line+0xff>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
test   %rax,%rax
je     116e <read_line+0x3e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1175 <read_line+0x45>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %ebx,%ebx
jmp    11b0 <read_line+0x80>
mov    $0x4,%ebx
jmp    11b0 <read_line+0x80>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     11b0 <read_line+0x80>
mov    0x2e57(%rip),%r14
cmp    (%r14),%al
je     11b0 <read_line+0x80>
mov    $0x5,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     11b0 <read_line+0x80>
cmp    (%r14),%al
jne    11a0 <read_line+0x70>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stdin = global i32 0
@EOF = global i8 0
define i32 @read_line(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %7
%8 = load i8*, i8** %4
%9 = load i64, i64* %5
%10 = load i32, i32* @stdin
%11 = call i32* @fgets(i8* %8, i64 %9, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 4, i32* %3
br label %50
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %15, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %43
24: ; preds = %14
br label %25
25: ; preds = %37, %24
%26 = call signext i8 (...) @getchar()
store i8 %26, i8* %6
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 10
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i8, i8* %6
%31 = sext i8 %30 to i32
%32 = load i8, i8* @EOF
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %31, %33
br label %35
35: ; preds = %29, %25
%36 = phi i1 [ false, %25 ], [ %34, %29 ]
br i1 %36, label %37, label %38
37: ; preds = %35
store i32 1, i32* %7
br label %25
38: ; preds = %35
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 1
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 5, i32 0
store i32 %42, i32* %3
br label %50
43: ; preds = %14
%44 = load i8*, i8** %4
%45 = load i8*, i8** %4
%46 = call i32 @strlen(i8* %45)
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
store i8 0, i8* %49
store i32 0, i32* %3
br label %50
50: ; preds = %43, %38, %13
%51 = load i32, i32* %3
ret i32 %51
}

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