#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 /*<<< orphan*/ perror(char *);
int read(int, void *, int);
int /*<<< orphan*/ write(int, char *, int);
int do_read(int fd, void *p, int len) {
  int l;
  if ((l = read(fd, p, len)) != len) {
    if (l < 0)
      perror("read");
    else
      write(2, "short read from tchelp.\n", 28);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    -0x14(%rbp),%eax
je     119a <do_read+0x6a>
cmpl   $0x0,-0x18(%rbp)
jge    1178 <do_read+0x48>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
jmp    118e <do_read+0x5e>
mov    $0x2,%edi
lea    0xe81(%rip),%rsi
mov    $0x1c,%edx
call   1030 <write@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a1 <do_read+0x71>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbx
mov    %edx,%ebx
call   1040 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebx,%ecx
je     115f <do_read+0x2f>
test   %ecx,%ecx
js     1161 <do_read+0x31>
lea    0xeba(%rip),%rsi
mov    $0x2,%edi
mov    $0x1c,%edx
call   1030 <write@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
lea    0xe98(%rip),%rdi
call   1050 <perror@plt>
jmp    115a <do_read+0x2a>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [25 x i8] c"short read from tchelp.\0A\00"
define i32 @do_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @read(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %7
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %3
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %22
20: ; preds = %15
%21 = call i32 @write(i32 2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 28)
br label %22
22: ; preds = %20, %18
store i32 -1, i32* %4
br label %24
23: ; preds = %3
store i32 0, i32* %4
br label %24
24: ; preds = %23, %22
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [25 x i8] c"short read from tchelp.\0A\00"
define i32 @do_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%5 = icmp eq i32 %4, %2
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = icmp slt i32 %4, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %6
%11 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 28)
br label %12
12: ; preds = %3, %8, %10
%13 = phi i32 [ -1, %10 ], [ -1, %8 ], [ 0, %3 ]
ret i32 %13
}