#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__ read(int, char *, int);
int fdgets(char *s, int count, int f) {
  int i;
  char ch;
  for (i = count - 1; i > 0; i--) {
    if (read(f, &ch, sizeof(char)) <= 0) {
      if (i > 1)
        *s++ = '\n';
      *s = '\0';
      return 0;
    }
    *s++ = (char)ch;
    if (ch == '\n')
      break;
  }
  *s = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<fdgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11b8 <fdgets+0xa8>
mov    -0x18(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jg     117f <fdgets+0x6f>
cmpl   $0x1,-0x1c(%rbp)
jle    116c <fdgets+0x5c>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0xa,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11c6 <fdgets+0xb6>
mov    -0x1d(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    11a5 <fdgets+0x95>
jmp    11b8 <fdgets+0xa8>
jmp    11aa <fdgets+0x9a>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    112b <fdgets+0x1b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xf(%rsp),%r12
mov    $0x1,%r14d
nop
cmp    $0x2,%ebp
jl     116d <fdgets+0x5d>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    115e <fdgets+0x4e>
movzbl 0xf(%rsp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    $0xa,%al
jne    1130 <fdgets+0x20>
jmp    116d <fdgets+0x5d>
xor    %r14d,%r14d
cmp    $0x3,%ebp
jb     116d <fdgets+0x5d>
movb   $0xa,(%rbx)
add    $0x1,%rbx
movb   $0x0,(%rbx)
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fdgets(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %8
br label %12
12: ; preds = %36, %3
%13 = load i32, i32* %8
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %39
15: ; preds = %12
%16 = load i32, i32* %7
%17 = call i64 @read(i32 %16, i8* %9, i32 1)
%18 = icmp sle i64 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i32, i32* %8
%21 = icmp sgt i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
store i8 10, i8* %23
br label %25
25: ; preds = %22, %19
%26 = load i8*, i8** %5
store i8 0, i8* %26
store i32 0, i32* %4
br label %41
27: ; preds = %15
%28 = load i8, i8* %9
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %5
store i8 %28, i8* %29
%31 = load i8, i8* %9
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %35
34: ; preds = %27
br label %39
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = load i32, i32* %8
%38 = add nsw i32 %37, -1
store i32 %38, i32* %8
br label %12
39: ; preds = %34, %12
%40 = load i8*, i8** %5
store i8 0, i8* %40
store i32 1, i32* %4
br label %41
41: ; preds = %39, %25
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @fdgets(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = alloca i8
br label %5
5: ; preds = %17, %3
%6 = phi i8* [ %0, %3 ], [ %19, %17 ]
%7 = phi i32 [ %1, %3 ], [ %8, %17 ]
%8 = add nsw i32 %7, -1
%9 = icmp sgt i32 %7, 1
br i1 %9, label %10, label %21
10: ; preds = %5
%11 = call i64 @read(i32 %2, i8* %4, i32 1)
%12 = icmp slt i64 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = icmp ugt i32 %7, 2
br i1 %14, label %15, label %21
15: ; preds = %13
%16 = getelementptr inbounds i8, i8* %6, i64 1
store i8 10, i8* %6
br label %21
17: ; preds = %10
%18 = load i8, i8* %4
%19 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %18, i8* %6
%20 = icmp eq i8 %18, 10
br i1 %20, label %21, label %5
21: ; preds = %5, %17, %13, %15
%22 = phi i8* [ %16, %15 ], [ %6, %13 ], [ %19, %17 ], [ %6, %5 ]
%23 = phi i32 [ 0, %15 ], [ 0, %13 ], [ 1, %17 ], [ 1, %5 ]
store i8 0, i8* %22
ret i32 %23
}