#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__ EINTR;
int /*<<< orphan*/ F_GETFL;
int O_NONBLOCK;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int read_socket(int fd, char *buf, int len, int ignore_timeout) {
  int rb = 0;
  int flags = fcntl(fd, F_GETFL, 0);
  if (flags == -1)
    return -1;
  do {
    rb = read(fd, buf, len);
  } while (rb < 0 && (errno == EINTR || (!(flags & O_NONBLOCK) &&
                                         ignore_timeout && errno == EAGAIN)));
  return rb;
}


x86 assembly (clang, -O0):
<read_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1168 <read_socket+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <read_socket+0xed>
jmp    116d <read_socket+0x4d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
jge    11fc <read_socket+0xdc>
mov    0x2e43(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e27(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x22(%rbp)
je     11f6 <read_socket+0xd6>
mov    -0x20(%rbp),%ecx
mov    0x2e31(%rip),%rax
and    (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x23(%rbp)
jne    11f0 <read_socket+0xd0>
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x23(%rbp)
je     11f0 <read_socket+0xd0>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x23(%rbp)
mov    -0x23(%rbp),%al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    116d <read_socket+0x4d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,0x4(%rsp)
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11ad <read_socket+0x8d>
mov    %eax,%r12d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11b2 <read_socket+0x92>
mov    0x2e71(%rip),%r13
mov    0x2e5a(%rip),%r14
jmp    1181 <read_socket+0x61>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11b2 <read_socket+0x92>
mov    0x0(%r13),%rcx
cmp    (%r14),%rcx
je     1170 <read_socket+0x50>
cmpl   $0x0,0x4(%rsp)
je     11b2 <read_socket+0x92>
mov    0x2e50(%rip),%rdx
mov    (%rdx),%edx
and    %r12d,%edx
jne    11b2 <read_socket+0x92>
mov    0x2e12(%rip),%rdx
cmp    (%rdx),%rcx
je     1170 <read_socket+0x50>
jmp    11b2 <read_socket+0x92>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@O_NONBLOCK = global i32 0
@EAGAIN = global i64 0
define i32 @read_socket(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = load i32, i32* @F_GETFL
%6 = tail call i32 @fcntl(i32 %0, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %29, label %8
8: ; preds = %4
%9 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %29
11: ; preds = %8
%12 = icmp ne i32 %3, 0
br label %13
13: ; preds = %11, %18
%14 = phi i32 [ %9, %11 ], [ %19, %18 ]
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %13, %21
%19 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%20 = icmp slt i32 %19, 0
br i1 %20, label %13, label %29
21: ; preds = %13
%22 = load i32, i32* @O_NONBLOCK
%23 = and i32 %22, %6
%24 = icmp eq i32 %23, 0
%25 = and i1 %12, %24
%26 = load i64, i64* @EAGAIN
%27 = icmp eq i64 %15, %26
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %18, label %29
29: ; preds = %21, %18, %8, %4
%30 = phi i32 [ -1, %4 ], [ %9, %8 ], [ %14, %21 ], [ %19, %18 ]
ret i32 %30
}