#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 ENODATA;
int errno;
int pread(int, char *, size_t, int /*<<< orphan*/);
ssize_t read_string_safe(int fd, char *buf, size_t size) {
  ssize_t ret;
  if ((ret = pread(fd, buf, size - 1, 0)) > 0) {
    if (buf[ret - 1] == '\n') {
      buf[ret - 1] = '\0';
    } else {
      buf[ret] = '\0';
    }
  } else if (ret < 0) {
    ret = -errno;
  } else {
    errno = ENODATA;
    ret = -errno;
  }
  return ret;
}


x86 assembly (clang, -O0):
<read_string_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
xor    %ecx,%ecx
call   1030 <pread@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1186 <read_string_safe+0x76>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1175 <read_string_safe+0x65>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1181 <read_string_safe+0x71>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11c8 <read_string_safe+0xb8>
cmpl   $0x0,-0x1c(%rbp)
jge    11a3 <read_string_safe+0x93>
xor    %eax,%eax
mov    0x2e3f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    11c3 <read_string_safe+0xb3>
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e25(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2e1a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    11c8 <read_string_safe+0xb8>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string_safe>:
push   %rbx
mov    %rsi,%rbx
add    $0xffffffffffffffff,%rdx
xor    %ecx,%ecx
call   1030 <pread@plt>
test   %eax,%eax
jle    1134 <read_string_safe+0x24>
lea    -0x1(%rax),%ecx
cmpb   $0xa,(%rbx,%rcx,1)
je     112e <read_string_safe+0x1e>
mov    %eax,%ecx
movb   $0x0,(%rbx,%rcx,1)
pop    %rbx
ret
js     114c <read_string_safe+0x3c>
mov    0x2eab(%rip),%rax
mov    (%rax),%eax
mov    0x2e92(%rip),%rcx
mov    %eax,(%rcx)
neg    %eax
pop    %rbx
ret
mov    0x2e85(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i32 0
@ENODATA = global i32 0
define i32 @read_string_safe(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = sub i64 %10, 1
%12 = call i32 @pread(i32 %8, i8* %9, i64 %11, i32 0)
store i32 %12, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %35
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* %7
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %29
23: ; preds = %14
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 0, i8* %28
br label %34
29: ; preds = %14
%30 = load i8*, i8** %5
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %29, %23
br label %46
35: ; preds = %3
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %7
br label %45
41: ; preds = %35
%42 = load i32, i32* @ENODATA
store i32 %42, i32* @errno
%43 = load i32, i32* @errno
%44 = sub nsw i32 0, %43
store i32 %44, i32* %7
br label %45
45: ; preds = %41, %38
br label %46
46: ; preds = %45, %34
%47 = load i32, i32* %7
ret i32 %47
}

LLVM IR (-O2):
@errno = global i32 0
@ENODATA = global i32 0
define i32 @read_string_safe(i32 %0, i8* nocapture %1, i64 %2) {
%4 = add i64 %2, -1
%5 = tail call i32 @pread(i32 %0, i8* %1, i64 %4, i32 0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %3
%8 = add nsw i32 %5, -1
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %14
13: ; preds = %7
store i8 0, i8* %10
br label %25
14: ; preds = %7
%15 = zext i32 %5 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
br label %25
17: ; preds = %3
%18 = icmp slt i32 %5, 0
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %25
22: ; preds = %17
%23 = load i32, i32* @ENODATA
store i32 %23, i32* @errno
%24 = sub nsw i32 0, %23
br label %25
25: ; preds = %19, %22, %13, %14
%26 = phi i32 [ %5, %13 ], [ %5, %14 ], [ %21, %19 ], [ %24, %22 ]
ret i32 %26
}