#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EBADF;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOSYS;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
ssize_t __libc_read(int fd, void *buf, size_t nbytes) {
  if (nbytes == 0)
    return 0;
  if (fd < 0) {
    __set_errno(EBADF);
    return -1;
  }
  if (buf == NULL) {
    __set_errno(EINVAL);
    return -1;
  }
  __set_errno(ENOSYS);
  return -1;
}


x86 assembly (clang, -O0):
<__libc_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    113a <__libc_read+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    1198 <__libc_read+0x88>
cmpl   $0x0,-0x8(%rbp)
jge    115e <__libc_read+0x4e>
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1198 <__libc_read+0x88>
cmpq   $0x0,-0x10(%rbp)
jne    1183 <__libc_read+0x73>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1198 <__libc_read+0x88>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__libc_read>:
test   %rdx,%rdx
je     1128 <__libc_read+0x18>
push   %rax
test   %rsi,%rsi
jne    112b <__libc_read+0x1b>
mov    0x2eb6(%rip),%rax
test   %edi,%edi
js     1136 <__libc_read+0x26>
jmp    113d <__libc_read+0x2d>
xor    %eax,%eax
ret
mov    0x2eb6(%rip),%rax
test   %edi,%edi
jns    113d <__libc_read+0x2d>
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
ret

LLVM IR (-O0):
@EBADF = global i32 0
@EINVAL = global i32 0
@ENOSYS = global i32 0
define i32 @__libc_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load i64, i64* %7
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %3
store i32 0, i32* %4
br label %26
11: ; preds = %3
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EBADF
%16 = call i32 @__set_errno(i32 %15)
store i32 -1, i32* %4
br label %26
17: ; preds = %11
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @EINVAL
%22 = call i32 @__set_errno(i32 %21)
store i32 -1, i32* %4
br label %26
23: ; preds = %17
%24 = load i32, i32* @ENOSYS
%25 = call i32 @__set_errno(i32 %24)
store i32 -1, i32* %4
br label %26
26: ; preds = %23, %20, %14, %10
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@EBADF = global i32 0
@EINVAL = global i32 0
@ENOSYS = global i32 0
define i32 @__libc_read(i32 %0, i8* readnone %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = icmp slt i32 %0, 0
%7 = icmp eq i8* %1, null
%8 = load i32, i32* @EBADF
%9 = load i32, i32* @EINVAL
%10 = load i32, i32* @ENOSYS
%11 = select i1 %7, i32 %9, i32 %10
%12 = select i1 %6, i32 %8, i32 %11
%13 = tail call i32 @__set_errno(i32 %12)
br label %14
14: ; preds = %5, %3
%15 = phi i32 [ 0, %3 ], [ -1, %5 ]
ret i32 %15
}