#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 */
int read_block(int, void *, size_t *);
int read_nonblock(int, void *, size_t *);
int read_timeout(int, void *, size_t *, int);
int block_read(int fd, void *data, size_t *p_nbytes, int timeout_ms) {
  if (timeout_ms == 0)
    return read_nonblock(fd, data, p_nbytes);
  else if (timeout_ms < 0)
    return read_block(fd, data, p_nbytes);
  else
    return read_timeout(fd, data, p_nbytes, timeout_ms);
}


x86 assembly (clang, -O0):
<block_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1168 <block_read+0x38>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read_nonblock@plt>
mov    %eax,-0x4(%rbp)
jmp    11a0 <block_read+0x70>
cmpl   $0x0,-0x1c(%rbp)
jge    118a <block_read+0x5a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read_block@plt>
mov    %eax,-0x4(%rbp)
jmp    11a0 <block_read+0x70>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <read_timeout@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_read>:
test   %ecx,%ecx
je     113b <block_read+0xb>
js     1140 <block_read+0x10>
jmp    1030 <read_timeout@plt>
jmp    1040 <read_nonblock@plt>
jmp    1050 <read_block@plt>

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

LLVM IR (-O2):
define i32 @block_read(i32 %0, i8* %1, i64* %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 @read_nonblock(i32 %0, i8* %1, i64* %2)
br label %14
8: ; preds = %4
%9 = icmp slt i32 %3, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @read_block(i32 %0, i8* %1, i64* %2)
br label %14
12: ; preds = %8
%13 = tail call i32 @read_timeout(i32 %0, i8* %1, i64* %2, i32 %3)
br label %14
14: ; preds = %12, %10, %6
%15 = phi i32 [ %7, %6 ], [ %11, %10 ], [ %13, %12 ]
ret i32 %15
}