#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 long uLong;
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ Bytef;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EINVAL;
scalar_t__ EISDIR;
int STDIN_FILENO;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ *Z_NULL;
char *calloc(size_t, int);
long crc32(long, int /*<<< orphan*/ *, int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
size_t getpagesize();
int read(int, char *, size_t);
uLong digest_bytestream(int fd) {
  struct stat sb;
  uLong crc = 0;
  char *buf = NULL;
  ssize_t ret;
  size_t len = getpagesize();
  if (fd == STDIN_FILENO)
    goto db_digest;
  if (fstat(fd, &sb) == -1)
    goto db_out;
  if (S_ISDIR(sb.st_mode)) {
    errno = EISDIR;
    goto db_out;
  }
  if (!S_ISREG(sb.st_mode)) {
    errno = EINVAL;
    goto db_out;
  }
db_digest:
  buf = calloc(len, sizeof(char));
  if (buf == NULL)
    goto db_out;
  crc = crc32(0L, Z_NULL, 0);
  while ((ret = read(fd, buf, len)) != 0) {
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      break;
    }
    crc = crc32(crc, (Bytef *)buf, ret);
  }
  free(buf);
db_out:
  return crc;
}


x86 assembly (clang, -O0):
<digest_bytestream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%eax
jne    11bd <digest_bytestream+0x3d>
jmp    1231 <digest_bytestream+0xb1>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11d7 <digest_bytestream+0x57>
jmp    12d8 <digest_bytestream+0x158>
mov    -0x8(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1202 <digest_bytestream+0x82>
mov    0x2df8(%rip),%rax
mov    (%rax),%rcx
mov    0x2dde(%rip),%rax
mov    %rcx,(%rax)
jmp    12d8 <digest_bytestream+0x158>
mov    -0x8(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%eax
jne    122c <digest_bytestream+0xac>
mov    0x2da6(%rip),%rax
mov    (%rax),%rcx
mov    0x2db4(%rip),%rax
mov    %rcx,(%rax)
jmp    12d8 <digest_bytestream+0x158>
jmp    1231 <digest_bytestream+0xb1>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1253 <digest_bytestream+0xd3>
jmp    12d8 <digest_bytestream+0x158>
mov    0x2d76(%rip),%rax
mov    (%rax),%rsi
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1040 <crc32@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     12cf <digest_bytestream+0x14f>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12b6 <digest_bytestream+0x136>
mov    0x2d3f(%rip),%rax
mov    (%rax),%rax
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%rax
jne    12b1 <digest_bytestream+0x131>
jmp    126c <digest_bytestream+0xec>
jmp    12cf <digest_bytestream+0x14f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <crc32@plt>
mov    %rax,-0x10(%rbp)
jmp    126c <digest_bytestream+0xec>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<digest_bytestream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %rax,%r14
mov    0x2e1a(%rip),%rax
cmp    %ebp,(%rax)
jne    1214 <digest_bytestream+0x94>
mov    %r14,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1246 <digest_bytestream+0xc6>
mov    %rax,%rbx
mov    0x2e0e(%rip),%rax
mov    (%rax),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1040 <crc32@plt>
mov    %rax,%r15
mov    0x2e00(%rip),%r12
mov    0x2de9(%rip),%r13
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    11fe <digest_bytestream+0x7e>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11e0 <digest_bytestream+0x60>
jmp    124b <digest_bytestream+0xcb>
test   %eax,%eax
je     124b <digest_bytestream+0xcb>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <crc32@plt>
mov    %rax,%r15
jmp    11e0 <digest_bytestream+0x60>
mov    %rsp,%rsi
mov    %ebp,%edi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1246 <digest_bytestream+0xc6>
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     1265 <digest_bytestream+0xe5>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
mov    %rax,(%rcx)
xor    %r15d,%r15d
jmp    1253 <digest_bytestream+0xd3>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1080 <S_ISREG@plt>
test   %eax,%eax
jne    11a2 <digest_bytestream+0x22>
mov    0x2d45(%rip),%rax
jmp    1239 <digest_bytestream+0xb9>

LLVM IR (-O0):
%struct.stat = type { i32 }
@STDIN_FILENO = global i32 0
@EISDIR = global i64 0
@errno = global i64 0
@EINVAL = global i64 0
@Z_NULL = global i32* null
@EINTR = global i64 0
define i64 @digest_bytestream(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %2
store i64 0, i64* %4
store i8* null, i8** %5
%8 = call i64 (...) @getpagesize()
store i64 %8, i64* %7
%9 = load i32, i32* %2
%10 = load i32, i32* @STDIN_FILENO
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %1
br label %33
13: ; preds = %1
%14 = load i32, i32* %2
%15 = call i32 @fstat(i32 %14, %struct.stat* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %13
br label %66
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @S_ISDIR(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i64, i64* @EISDIR
store i64 %24, i64* @errno
br label %66
25: ; preds = %18
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISREG(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = load i64, i64* @EINVAL
store i64 %31, i64* @errno
br label %66
32: ; preds = %25
br label %33
33: ; preds = %32, %12
%34 = load i64, i64* %7
%35 = call i8* @calloc(i64 %34, i32 1)
store i8* %35, i8** %5
%36 = load i8*, i8** %5
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %39
38: ; preds = %33
br label %66
39: ; preds = %33
%40 = load i32*, i32** @Z_NULL
%41 = call i64 @crc32(i64 0, i32* %40, i32 0)
store i64 %41, i64* %4
br label %42
42: ; preds = %57, %55, %39
%43 = load i32, i32* %2
%44 = load i8*, i8** %5
%45 = load i64, i64* %7
%46 = call i32 @read(i32 %43, i8* %44, i64 %45)
store i32 %46, i32* %6
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %63
48: ; preds = %42
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i64, i64* @errno
%53 = load i64, i64* @EINTR
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %51
br label %42
56: ; preds = %51
br label %63
57: ; preds = %48
%58 = load i64, i64* %4
%59 = load i8*, i8** %5
%60 = bitcast i8* %59 to i32*
%61 = load i32, i32* %6
%62 = call i64 @crc32(i64 %58, i32* %60, i32 %61)
store i64 %62, i64* %4
br label %42
63: ; preds = %56, %42
%64 = load i8*, i8** %5
%65 = call i32 @free(i8* %64)
br label %66
66: ; preds = %63, %38, %30, %23, %17
%67 = load i64, i64* %4
ret i64 %67
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@STDIN_FILENO = global i32 0
@EISDIR = global i64 0
@errno = global i64 0
@EINVAL = global i64 0
@Z_NULL = global i32* null
@EINTR = global i64 0
define i64 @digest_bytestream(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i64 (...) @getpagesize()
%5 = load i32, i32* @STDIN_FILENO
%6 = icmp eq i32 %5, %0
br i1 %6, label %22, label %7
7: ; preds = %1
%8 = call i32 @fstat(i32 %0, %struct.stat* %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %41, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i64 @S_ISDIR(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = load i64, i64* @EISDIR
store i64 %16, i64* @errno
br label %41
17: ; preds = %10
%18 = tail call i32 @S_ISREG(i32 %12)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i64, i64* @EINVAL
store i64 %21, i64* @errno
br label %41
22: ; preds = %17, %1
%23 = tail call i8* @calloc(i64 %4, i32 1)
%24 = icmp eq i8* %23, null
br i1 %24, label %41, label %25
25: ; preds = %22
%26 = load i32*, i32** @Z_NULL
%27 = tail call i64 @crc32(i64 0, i32* %26, i32 0)
%28 = bitcast i8* %23 to i32*
br label %29
29: ; preds = %37, %25
%30 = phi i64 [ %38, %37 ], [ %27, %25 ]
br label %31
31: ; preds = %29, %33
%32 = tail call i32 @read(i32 %0, i8* %23, i64 %4)
switch i32 %32, label %37 [
i32 0, label %39
i32 -1, label %33
]
33: ; preds = %31
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %31, label %39
37: ; preds = %31
%38 = tail call i64 @crc32(i64 %30, i32* %28, i32 %32)
br label %29
39: ; preds = %31, %33
%40 = tail call i32 @free(i8* %23)
br label %41
41: ; preds = %22, %7, %39, %20, %15
%42 = phi i64 [ 0, %22 ], [ %30, %39 ], [ 0, %7 ], [ 0, %15 ], [ 0, %20 ]
ret i64 %42
}