#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;
scalar_t__ errno;
char *fgets(char *, int, int /*<<< orphan*/ *);
bool s_fgets(char *buf, int size, FILE *stream) {
  char *ret;
  while (1) {
    if (((ret = fgets(buf, size, stream)) == NULL) && (errno == EINTR))
      continue;
    else if (ret == NULL && errno != EINTR)
      return false;
    break;
  }
  return true;
}


x86 assembly (clang, -O0):
<s_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1030 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1160 <s_fgets+0x50>
mov    0x2e98(%rip),%rax
mov    (%rax),%rax
mov    0x2e86(%rip),%rcx
cmp    (%rcx),%rax
jne    1160 <s_fgets+0x50>
jmp    1123 <s_fgets+0x13>
cmpq   $0x0,-0x28(%rbp)
jne    1191 <s_fgets+0x81>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
je     1191 <s_fgets+0x81>
movl   $0x0,-0x4(%rbp)
jmp    11a2 <s_fgets+0x92>
jmp    1196 <s_fgets+0x86>
jmp    119b <s_fgets+0x8b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_fgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2eb9(%rip),%r15
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
jne    114f <s_fgets+0x3f>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1130 <s_fgets+0x20>
xor    %eax,%eax
jmp    1154 <s_fgets+0x44>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @s_fgets(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
br label %9
9: ; preds = %3, %19
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = load i32*, i32** %7
%13 = call i8* @fgets(i8* %10, i32 %11, i32* %12)
store i8* %13, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %9
20: ; preds = %15, %9
%21 = load i8*, i8** %8
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %4
br label %31
28: ; preds = %23, %20
br label %29
29: ; preds = %28
br label %30
30: ; preds = %29
store i32 1, i32* %4
br label %31
31: ; preds = %30, %27
%32 = load i32, i32* %4
ret i32 %32
}

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