#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 stat {
  scalar_t__ st_rdev;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ isspace(char);
scalar_t__ makedev(int, int);
int /*<<< orphan*/ perror(char *);
size_t strlen(char *);
int read_line(char **dst, const char *path) {
  FILE *f;
  size_t n = 0;
  struct stat stat;
  *dst = NULL;
  f = fopen(path, "r");
  if (!f) {
    perror("fopen");
    return -1;
  }
  if (fstat(fileno(f), &stat) < 0) {
    perror("fstat");
    fclose(f);
    return -1;
  }
  if (S_ISCHR(stat.st_mode) && stat.st_rdev == makedev(1, 3)) {
    fclose(f);
    return 1;
  }
  if (getline(dst, &n, f) < 0) {
    perror("getline");
    fclose(f);
    return -1;
  }
  fclose(f);
  n = strlen(*dst);
  while (--n) {
    if (isspace((*dst)[n]))
      (*dst)[n] = '\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)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fa <read_line+0x5a>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <read_line+0x18b>
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x38(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
jge    1239 <read_line+0x99>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <read_line+0x18b>
mov    -0x30(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1287 <read_line+0xe7>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x1,%edi
mov    $0x3,%esi
call   10a0 <makedev@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jne    1287 <read_line+0xe7>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    132b <read_line+0x18b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rsi
call   10b0 <getline@plt>
cmp    $0x0,%rax
jge    12c3 <read_line+0x123>
lea    0xd65(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <read_line+0x18b>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1324 <read_line+0x184>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     131f <read_line+0x17f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12dc <read_line+0x13c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
mov    %rdi,%rbx
movq   $0x0,(%rsp)
movq   $0x0,(%rdi)
lea    0xe3a(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     129d <read_line+0xfd>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
js     12ab <read_line+0x10b>
mov    0x10(%rsp),%edi
call   1070 <S_ISCHR@plt>
test   %rax,%rax
je     1231 <read_line+0x91>
mov    0x8(%rsp),%rbp
mov    $0x1,%r15d
mov    $0x1,%edi
mov    $0x3,%esi
call   10a0 <makedev@plt>
cmp    %rax,%rbp
jne    1231 <read_line+0x91>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    12ce <read_line+0x12e>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   10b0 <getline@plt>
test   %rax,%rax
js     12b4 <read_line+0x114>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    (%rbx),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
xor    %r15d,%r15d
add    $0xffffffffffffffff,%rax
mov    %rax,(%rsp)
je     12ce <read_line+0x12e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0x0(%rbp,%rax,1),%edi
call   1050 <isspace@plt>
test   %rax,%rax
je     128a <read_line+0xea>
mov    (%rbx),%rax
mov    (%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    (%rsp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,(%rsp)
je     12ce <read_line+0x12e>
mov    (%rbx),%rbp
jmp    1270 <read_line+0xd0>
lea    0xd5e(%rip),%rdi
call   1090 <perror@plt>
jmp    12c8 <read_line+0x128>
lea    0xd56(%rip),%rdi
jmp    12bb <read_line+0x11b>
lea    0xd53(%rip),%rdi
call   1090 <perror@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [8 x i8] c"getline\00"
define i32 @read_line(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca %struct.stat
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i64 0, i64* %7
%9 = load i8**, i8*** %4
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = call i32 @fileno(i32* %17)
%19 = call i64 @fstat(i32 %18, %struct.stat* %8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
store i32 -1, i32* %3
br label %72
25: ; preds = %16
%26 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i64 @S_ISCHR(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %25
%31 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%32 = load i64, i64* %31
%33 = call i64 @makedev(i32 1, i32 3)
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
store i32 1, i32* %3
br label %72
38: ; preds = %30, %25
%39 = load i8**, i8*** %4
%40 = load i32*, i32** %6
%41 = call i64 @getline(i8** %39, i64* %7, i32* %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
store i32 -1, i32* %3
br label %72
47: ; preds = %38
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
%50 = load i8**, i8*** %4
%51 = load i8*, i8** %50
%52 = call i64 @strlen(i8* %51)
store i64 %52, i64* %7
br label %53
53: ; preds = %70, %47
%54 = load i64, i64* %7
%55 = add i64 %54, -1
store i64 %55, i64* %7
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %53
%58 = load i8**, i8*** %4
%59 = load i8*, i8** %58
%60 = load i64, i64* %7
%61 = getelementptr inbounds i8, i8* %59, i64 %60
%62 = load i8, i8* %61
%63 = call i64 @isspace(i8 signext %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %70
65: ; preds = %57
%66 = load i8**, i8*** %4
%67 = load i8*, i8** %66
%68 = load i64, i64* %7
%69 = getelementptr inbounds i8, i8* %67, i64 %68
store i8 0, i8* %69
br label %70
70: ; preds = %65, %57
br label %53
71: ; preds = %53
store i32 0, i32* %3
br label %72
72: ; preds = %71, %43, %35, %21, %14
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [8 x i8] c"getline\00"
define i32 @read_line(i8** %0, i8* nocapture %1) {
%3 = alloca i64
%4 = alloca %struct.stat
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = bitcast %struct.stat* %4 to i8*
store i8* null, i8** %0
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %59
11: ; preds = %2
%12 = tail call i32 @fileno(i32* %7)
%13 = call i64 @fstat(i32 %12, %struct.stat* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @fclose(i32* %7)
br label %59
18: ; preds = %11
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = tail call i64 @S_ISCHR(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%25 = load i64, i64* %24
%26 = tail call i64 @makedev(i32 1, i32 3)
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = tail call i32 @fclose(i32* %7)
br label %59
30: ; preds = %23, %18
%31 = call i64 @getline(i8** %0, i64* %3, i32* %7)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @fclose(i32* %7)
br label %59
36: ; preds = %30
%37 = call i32 @fclose(i32* %7)
%38 = load i8*, i8** %0
%39 = call i64 @strlen(i8* dereferenceable(1) %38)
%40 = add i64 %39, -1
store i64 %40, i64* %3
%41 = icmp eq i64 %40, 0
br i1 %41, label %59, label %42
42: ; preds = %36, %57
%43 = phi i8* [ %58, %57 ], [ %38, %36 ]
%44 = phi i64 [ %55, %57 ], [ %40, %36 ]
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = load i8, i8* %45
%47 = call i64 @isspace(i8 signext %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %42
%50 = load i8*, i8** %0
%51 = load i64, i64* %3
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %49, %42
%54 = load i64, i64* %3
%55 = add i64 %54, -1
store i64 %55, i64* %3
%56 = icmp eq i64 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %53
%58 = load i8*, i8** %0
br label %42
59: ; preds = %53, %36, %33, %28, %15, %9
%60 = phi i32 [ -1, %15 ], [ 1, %28 ], [ -1, %33 ], [ -1, %9 ], [ 0, %36 ], [ 0, %53 ]
ret i32 %60
}