#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 EIO;
int errno;
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int);
int do_read(int fd, char *buf, int buf_sz) {
  int ret = read(fd, buf, buf_sz);
  if (ret < 0) {
    int err = errno;
    printf("do_read: error: %d (%s)\n", err, strerror(err));
    return err;
  }
  if (ret != buf_sz) {
    printf("do_read: short read\n");
    return -EIO;
  }
  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)
cmpl   $0x0,-0x18(%rbp)
jge    1197 <do_read+0x67>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%esi
mov    %rax,%rdx
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cb <do_read+0x9b>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11c4 <do_read+0x94>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    0x2e26(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11cb <do_read+0x9b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
call   1050 <read@plt>
test   %eax,%eax
js     116d <do_read+0x2d>
xor    %ebx,%ebx
cmp    %ebp,%eax
je     1190 <do_read+0x50>
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e79(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    1190 <do_read+0x50>
mov    0x2e64(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe7c(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [25 x i8] c"do_read: error: %d (%s)\0A\00"
@.str.1 = [21 x i8] c"do_read: short read\0A\00"
@EIO = global i32 0
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
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @read(i32 %10, i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %3
%17 = load i32, i32* @errno
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = load i32, i32* %9
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %18, i8* %20)
%22 = load i32, i32* %9
store i32 %22, i32* %4
br label %32
23: ; preds = %3
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EIO
%30 = sub nsw i32 0, %29
store i32 %30, i32* %4
br label %32
31: ; preds = %23
store i32 0, i32* %4
br label %32
32: ; preds = %31, %27, %16
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [25 x i8] c"do_read: error: %d (%s)\0A\00"
@EIO = global i32 0
@str = [20 x i8] c"do_read: short read\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 slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %8)
br label %16
10: ; preds = %3
%11 = icmp eq i32 %4, %2
br i1 %11, label %16, label %12
12: ; preds = %10
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @EIO
%15 = sub nsw i32 0, %14
br label %16
16: ; preds = %10, %12, %6
%17 = phi i32 [ %7, %6 ], [ %15, %12 ], [ 0, %10 ]
ret i32 %17
}