#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int weather_nextline(int fd) {
  ssize_t n;
  char ch;
  while ((n = read(fd, &ch, 1))) {
    if (n < 0) {
      perror("read failed");
      return -1;
    } else if (n == 0) {
      break;
    } else {
      if (ch == '\n') {
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<weather_nextline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     119e <weather_nextline+0x7e>
cmpq   $0x0,-0x10(%rbp)
jge    116d <weather_nextline+0x4d>
lea    0xea4(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a5 <weather_nextline+0x85>
cmpq   $0x0,-0x10(%rbp)
jne    117d <weather_nextline+0x5d>
jmp    119e <weather_nextline+0x7e>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    118f <weather_nextline+0x6f>
jmp    119e <weather_nextline+0x7e>
jmp    1194 <weather_nextline+0x74>
jmp    1199 <weather_nextline+0x79>
jmp    112b <weather_nextline+0xb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<weather_nextline>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x7(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1152 <weather_nextline+0x32>
js     115a <weather_nextline+0x3a>
cmpb   $0xa,0x7(%rsp)
jne    1130 <weather_nextline+0x10>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe9f(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1152 <weather_nextline+0x32>

LLVM IR (-O0):
@.str = [12 x i8] c"read failed\00"
define i32 @weather_nextline(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8
store i32 %0, i32* %3
br label %6
6: ; preds = %26, %1
%7 = load i32, i32* %3
%8 = call i64 @read(i32 %7, i8* %5, i32 1)
store i64 %8, i64* %4
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %6
%11 = load i64, i64* %4
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
15: ; preds = %10
%16 = load i64, i64* %4
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
br label %27
19: ; preds = %15
%20 = load i8, i8* %5
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %24
23: ; preds = %19
br label %27
24: ; preds = %19
br label %25
25: ; preds = %24
br label %26
26: ; preds = %25
br label %6
27: ; preds = %23, %18, %6
store i32 0, i32* %2
br label %28
28: ; preds = %27, %13
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [12 x i8] c"read failed\00"
define i32 @weather_nextline(i32 %0) {
%2 = alloca i8
br label %3
3: ; preds = %10, %1
%4 = call i64 @read(i32 %0, i8* %2, i32 1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %3
%7 = icmp slt i64 %4, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %13
10: ; preds = %6
%11 = load i8, i8* %2
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %3
13: ; preds = %3, %10, %8
%14 = phi i32 [ -1, %8 ], [ 0, %10 ], [ 0, %3 ]
ret i32 %14
}