#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(char);
int strlen(char *);
int read_line(FILE *stream, char *buf) {
  if (!fgets(buf, 1024, stream)) {
    if (feof(stream)) {
      putchar('\n');
      return 0;
    } else {
      perror("shell: Error");
      return -1;
    }
  }
  return strlen(buf);
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    11bc <read_line+0x6c>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     11a4 <read_line+0x54>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c8 <read_line+0x78>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <read_line+0x78>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     117d <read_line+0x2d>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <strlen@plt>
mov    %r14,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     119e <read_line+0x4e>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    1196 <read_line+0x46>

LLVM IR (-O0):
@.str = [13 x i8] c"shell: Error\00"
define i32 @read_line(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %5
%7 = load i32*, i32** %4
%8 = call i32 @fgets(i8* %6, i32 1024, i32* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = load i32*, i32** %4
%12 = call i64 @feof(i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @putchar(i8 signext 10)
store i32 0, i32* %3
br label %21
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %21
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %3
br label %21
21: ; preds = %18, %16, %14
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@.str = [13 x i8] c"shell: Error\00"
define i32 @read_line(i32* nocapture %0, i8* %1) {
%3 = tail call i32 @fgets(i8* %1, i32 1024, i32* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = tail call i64 @feof(i32* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @putchar(i8 signext 10)
br label %14
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %14
12: ; preds = %2
%13 = tail call i32 @strlen(i8* %1)
br label %14
14: ; preds = %12, %10, %8
%15 = phi i32 [ %13, %12 ], [ 0, %8 ], [ -1, %10 ]
ret i32 %15
}