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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOBUFS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ read_string_safe(int, char *, size_t);
ssize_t read_string(int fd, char *buf, size_t size) {
  if (!buf) {
    errno = EINVAL;
    return (ssize_t)-errno;
  } else if (!size) {
    errno = ENOBUFS;
    return (ssize_t)-errno;
  }
  return read_string_safe(fd, buf, size);
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <read_string+0x43>
mov    0x2e9b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9a(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2e8f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <read_string+0x8b>
cmpq   $0x0,-0x18(%rbp)
jne    1183 <read_string+0x73>
mov    0x2e93(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6a(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2e5f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <read_string+0x8b>
jmp    1188 <read_string+0x78>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read_string_safe@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
test   %rsi,%rsi
je     111f <read_string+0xf>
test   %rdx,%rdx
je     1128 <read_string+0x18>
jmp    1030 <read_string_safe@plt>
mov    0x2eaa(%rip),%rax
jmp    112f <read_string+0x1f>
mov    0x2ec9(%rip),%rax
mov    (%rax),%eax
mov    0x2ea0(%rip),%rcx
mov    %eax,(%rcx)
neg    %eax
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@ENOBUFS = global i32 0
define i32 @read_string(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 i8*, i8** %6
%9 = icmp ne i8* %8, null
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
store i32 %13, i32* %4
br label %27
14: ; preds = %3
%15 = load i64, i64* %7
%16 = icmp ne i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32, i32* @ENOBUFS
store i32 %18, i32* @errno
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %27
21: ; preds = %14
br label %22
22: ; preds = %21
%23 = load i32, i32* %5
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = call i32 @read_string_safe(i32 %23, i8* %24, i64 %25)
store i32 %26, i32* %4
br label %27
27: ; preds = %22, %17, %10
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@ENOBUFS = global i32 0
define i32 @read_string(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @EINVAL
store i32 %6, i32* @errno
%7 = sub nsw i32 0, %6
br label %15
8: ; preds = %3
%9 = icmp eq i64 %2, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = load i32, i32* @ENOBUFS
store i32 %11, i32* @errno
%12 = sub nsw i32 0, %11
br label %15
13: ; preds = %8
%14 = tail call i32 @read_string_safe(i32 %0, i8* %1, i64 %2)
br label %15
15: ; preds = %13, %10, %5
%16 = phi i32 [ %14, %13 ], [ %12, %10 ], [ %7, %5 ]
ret i32 %16
}