#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int fdgets(int fd, char *buffer, int len) {
  int outsize = 0;
  char *ptr = buffer;
  while (1) {
    int res = read(fd, ptr, 1);
    if (res <= 0) {
      if (res == -1 && errno != EAGAIN) {
        perror("FDGETS (READ)");
        return (-1);
      }
      *ptr = 0;
      return (outsize);
    }
    if (*ptr == '\n') {
      if (outsize + 1 < len)
        *(ptr + 1) = 0;
      return (outsize);
    }
    ptr++;
    if (outsize++ == len)
      return (len);
  }
}


x86 assembly (clang, -O0):
<fdgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11ad <fdgets+0x8d>
cmpl   $0xffffffff,-0x24(%rbp)
jne    119b <fdgets+0x7b>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
je     119b <fdgets+0x7b>
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <fdgets+0xef>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <fdgets+0xef>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11df <fdgets+0xbf>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jge    11d4 <fdgets+0xb4>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <fdgets+0xef>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
cmp    -0x14(%rbp),%eax
jne    120a <fdgets+0xea>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <fdgets+0xef>
jmp    1141 <fdgets+0x21>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdgets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r12d
mov    %edx,0x4(%rsp)
mov    %edx,%r13d
lea    0x1(%r13),%r15
xor    %ebx,%ebx
xchg   %ax,%ax
lea    (%r14,%rbx,1),%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    116c <fdgets+0x4c>
cmpb   $0xa,0x0(%rbp)
je     118e <fdgets+0x6e>
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1140 <fdgets+0x20>
mov    %r13d,%ebx
jmp    119d <fdgets+0x7d>
cmp    $0xffffffff,%eax
jne    1187 <fdgets+0x67>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    11ae <fdgets+0x8e>
movb   $0x0,(%r14,%rbx,1)
jmp    119d <fdgets+0x7d>
lea    0x1(%rbx),%eax
cmp    0x4(%rsp),%eax
jge    119d <fdgets+0x7d>
movb   $0x0,0x1(%r14,%rbx,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4b(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    119d <fdgets+0x7d>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"FDGETS (READ)\00"
define i32 @fdgets(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %6
store i8* %11, i8** %9
br label %12
12: ; preds = %3, %54
%13 = load i32, i32* %5
%14 = load i8*, i8** %9
%15 = call i32 @read(i32 %13, i8* %14, i32 1)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %12
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EAGAIN
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %55
27: ; preds = %21, %18
%28 = load i8*, i8** %9
store i8 0, i8* %28
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %55
30: ; preds = %12
%31 = load i8*, i8** %9
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 10
br i1 %34, label %35, label %45
35: ; preds = %30
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
%38 = load i32, i32* %7
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i8*, i8** %9
%42 = getelementptr inbounds i8, i8* %41, i64 1
store i8 0, i8* %42
br label %43
43: ; preds = %40, %35
%44 = load i32, i32* %8
store i32 %44, i32* %4
br label %55
45: ; preds = %30
%46 = load i8*, i8** %9
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %9
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
store i32 %49, i32* %8
%50 = load i32, i32* %7
%51 = icmp eq i32 %48, %50
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = load i32, i32* %7
store i32 %53, i32* %4
br label %55
54: ; preds = %45
br label %12
55: ; preds = %52, %43, %27, %25
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"FDGETS (READ)\00"
define i32 @fdgets(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %26, %3
%5 = phi i32 [ 0, %3 ], [ %28, %26 ]
%6 = phi i8* [ %1, %3 ], [ %27, %26 ]
%7 = tail call i32 @read(i32 %0, i8* %6, i32 1)
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %18
9: ; preds = %4
%10 = icmp eq i32 %7, -1
br i1 %10, label %11, label %17
11: ; preds = %9
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %30
17: ; preds = %11, %9
store i8 0, i8* %6
br label %30
18: ; preds = %4
%19 = load i8, i8* %6
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = add nuw nsw i32 %5, 1
%23 = icmp slt i32 %22, %2
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = getelementptr inbounds i8, i8* %6, i64 1
store i8 0, i8* %25
br label %30
26: ; preds = %18
%27 = getelementptr inbounds i8, i8* %6, i64 1
%28 = add nuw nsw i32 %5, 1
%29 = icmp eq i32 %5, %2
br i1 %29, label %30, label %4
30: ; preds = %26, %21, %24, %17, %15
%31 = phi i32 [ %5, %21 ], [ %5, %24 ], [ %5, %17 ], [ -1, %15 ], [ %2, %26 ]
ret i32 %31
}