#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__ EINTR;
int EOF;
scalar_t__ errno;
int fgetc(int /*<<< orphan*/ *);
char *safe_fgets(char *s, int size, FILE *stream) {
  int cnt = 0;
  int ch;
  while (cnt < size) {
    do {
      errno = 0;
      ch = fgetc(stream);
    } while (ch == EOF && errno == EINTR);
    if (ch == EOF) {
      s[cnt++] = '\0';
      break;
    }
    s[cnt++] = ch;
    if (ch == '\n') {
      s[cnt++] = '\0';
      break;
    }
  }
  return s;
}


x86 assembly (clang, -O0):
<safe_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1202 <safe_fgets+0xf2>
jmp    113b <safe_fgets+0x2b>
mov    0x2e96(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
mov    0x2e87(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x21(%rbp)
jne    1186 <safe_fgets+0x76>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    113b <safe_fgets+0x2b>
mov    -0x20(%rbp),%eax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%eax
jne    11be <safe_fgets+0xae>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1202 <safe_fgets+0xf2>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0xa,-0x20(%rbp)
jne    11fd <safe_fgets+0xed>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1202 <safe_fgets+0xf2>
jmp    112a <safe_fgets+0x1a>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fgets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
xor    %r12d,%r12d
test   %esi,%esi
cmovle %r12d,%r15d
mov    %rdx,%r13
mov    %rdi,(%rsp)
mov    0x2ea3(%rip),%rbx
mov    0x2eac(%rip),%rbp
mov    0x2e8d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %r15,%r12
je     119e <safe_fgets+0x8e>
cs nopw 0x0(%rax,%rax,1)
nop
movq   $0x0,(%rbx)
mov    %r13,%rdi
call   1030 <fgetc@plt>
mov    0x0(%rbp),%ecx
cmp    %ecx,%eax
jne    117e <safe_fgets+0x6e>
mov    (%r14),%rdx
cmp    %rdx,(%rbx)
je     1160 <safe_fgets+0x50>
cmp    %ecx,%eax
je     1193 <safe_fgets+0x83>
mov    (%rsp),%rcx
mov    %al,(%rcx,%r12,1)
add    $0x1,%r12
cmp    $0xa,%eax
jne    1150 <safe_fgets+0x40>
mov    %r12d,%eax
mov    (%rsp),%rcx
movb   $0x0,(%rcx,%rax,1)
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EOF = global i32 0
@EINTR = global i64 0
define i8* @safe_fgets(i8* returned writeonly %0, i32 %1, i32* nocapture %2) {
%4 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%5 = zext i32 %4 to i64
br label %6
6: ; preds = %18, %3
%7 = phi i64 [ %20, %18 ], [ 0, %3 ]
%8 = icmp eq i64 %7, %5
br i1 %8, label %27, label %9
9: ; preds = %6, %9
store i64 0, i64* @errno
%10 = tail call i32 @fgetc(i32* %2)
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = select i1 %12, i1 %15, i1 false
br i1 %16, label %9, label %17
17: ; preds = %9
br i1 %12, label %23, label %18
18: ; preds = %17
%19 = trunc i32 %10 to i8
%20 = add nuw nsw i64 %7, 1
%21 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 %19, i8* %21
%22 = icmp eq i32 %10, 10
br i1 %22, label %23, label %6
23: ; preds = %18, %17
%24 = phi i64 [ %7, %17 ], [ %20, %18 ]
%25 = and i64 %24, 4294967295
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %6, %23
ret i8* %0
}