#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 /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
char *safe_fgets(char *s, int size, FILE *stream) {
  char *ret;
  do {
    clearerr(stream);
    ret = fgets(s, size, stream);
  } while (ret == NULL && ferror(stream) && errno == EINTR);
  return ret;
}


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)
mov    -0x18(%rbp),%rdi
call   1050 <clearerr@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jne    11a5 <safe_fgets+0x75>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     11a5 <safe_fgets+0x75>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1143 <safe_fgets+0x13>
mov    -0x20(%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    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e95(%rip),%r12
mov    0x2e86(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <clearerr@plt>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1195 <safe_fgets+0x65>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1195 <safe_fgets+0x65>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1160 <safe_fgets+0x30>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 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 i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
br label %8
8: ; preds = %26, %3
%9 = load i32*, i32** %6
%10 = call i32 @clearerr(i32* %9)
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = load i32*, i32** %6
%14 = call i8* @fgets(i8* %11, i32 %12, i32* %13)
store i8* %14, i8** %7
br label %15
15: ; preds = %8
%16 = load i8*, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32*, i32** %6
%20 = call i64 @ferror(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br label %26
26: ; preds = %22, %18, %15
%27 = phi i1 [ false, %18 ], [ false, %15 ], [ %25, %22 ]
br i1 %27, label %8, label %28
28: ; preds = %26
%29 = load i8*, i8** %7
ret i8* %29
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i8* @safe_fgets(i8* %0, i32 %1, i32* nocapture %2) {
br label %4
4: ; preds = %11, %3
%5 = tail call i32 @clearerr(i32* %2)
%6 = tail call i8* @fgets(i8* %0, i32 %1, i32* %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = tail call i64 @ferror(i32* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %4, label %15
15: ; preds = %4, %8, %11
ret i8* %6
}