#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
int EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fwrite(char *, scalar_t__, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ puts(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stdout;
int do_read_command(char *filename) {
  if (!filename)
    return -EINVAL;
  int fd = open(filename, O_RDONLY);
  if (fd < 0)
    return -errno;
  char buf[BUFSIZ];
  ssize_t size;
  while ((size = read(fd, buf, sizeof(buf))) > 0) {
    printf("%zd\n", size);
    fwrite(buf, size, 1, stdout);
  }
  int ret;
  if (size == 0) {
    puts("ok");
    ret = 0;
  } else {
    ret = -errno;
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<do_read_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118a <do_read_command+0x2a>
xor    %eax,%eax
mov    0x2e48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1282 <do_read_command+0x122>
mov    -0x10(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bc <do_read_command+0x5c>
xor    %eax,%eax
mov    0x2e1e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1282 <do_read_command+0x122>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    -0x14(%rbp),%edi
mov    %eax,%edx
call   1060 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jle    123c <do_read_command+0xdc>
mov    -0x30(%rbp),%rsi
lea    0xde9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2d95(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    11ec <do_read_command+0x8c>
cmpq   $0x0,-0x30(%rbp)
jne    125f <do_read_command+0xff>
lea    0xdb7(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x34(%rbp)
jmp    126d <do_read_command+0x10d>
xor    %eax,%eax
mov    0x2d68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read_command>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     121b <do_read_command+0xbb>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1224 <do_read_command+0xc4>
mov    %eax,%r14d
mov    %rsp,-0x30(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    1209 <do_read_command+0xa9>
mov    0x2df1(%rip),%r13
nop
lea    0xe29(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x0(%r13),%ecx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jg     11d0 <do_read_command+0x70>
test   %rbx,%rbx
je     1231 <do_read_command+0xd1>
mov    0x2dbb(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    123f <do_read_command+0xdf>
mov    0x2da6(%rip),%rax
jmp    122b <do_read_command+0xcb>
mov    0x2da5(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    124b <do_read_command+0xeb>
lea    0xdcd(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@O_RDONLY = global i32 0
@errno = global i32 0
@BUFSIZ = global i32 0
@.str = [5 x i8] c"%zd\0A\00"
@stdout = global i32 0
@.str.1 = [3 x i8] c"ok\00"
define i32 @do_read_command(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %52
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %2
br label %52
23: ; preds = %14
%24 = load i32, i32* @BUFSIZ
%25 = zext i32 %24 to i64
%26 = call i8* @llvm.stacksave()
store i8* %26, i8** %5
%27 = alloca i8, i64 %25
store i64 %25, i64* %6
br label %28
28: ; preds = %33, %23
%29 = load i32, i32* %4
%30 = trunc i64 %25 to i32
%31 = call i64 @read(i32 %29, i8* %27, i32 %30)
store i64 %31, i64* %7
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i64, i64* %7
%35 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %34)
%36 = load i64, i64* %7
%37 = load i32, i32* @stdout
%38 = call i32 @fwrite(i8* %27, i64 %36, i32 1, i32 %37)
br label %28
39: ; preds = %28
%40 = load i64, i64* %7
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %8
br label %47
44: ; preds = %39
%45 = load i32, i32* @errno
%46 = sub nsw i32 0, %45
store i32 %46, i32* %8
br label %47
47: ; preds = %44, %42
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %8
store i32 %50, i32* %2
%51 = load i8*, i8** %5
br label %52
52: ; preds = %47, %20, %11
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@EINVAL = global i32 0
@O_RDONLY = global i32 0
@errno = global i32 0
@BUFSIZ = global i32 0
@.str = [5 x i8] c"%zd\0A\00"
@stdout = global i32 0
@.str.1 = [3 x i8] c"ok\00"
define i32 @do_read_command(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = load i32, i32* @EINVAL
%5 = sub nsw i32 0, %4
br label %38
6: ; preds = %1
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %38
13: ; preds = %6
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = tail call i8* @llvm.stacksave()
%17 = alloca i8, i64 %15
%18 = call i64 @read(i32 %8, i8* %17, i32 %14)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %13, %20
%21 = phi i64 [ %25, %20 ], [ %18, %13 ]
%22 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fwrite(i8* %17, i64 %21, i32 1, i32 %23)
%25 = call i64 @read(i32 %8, i8* %17, i32 %14)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %20, label %27
27: ; preds = %20, %13
%28 = phi i64 [ %18, %13 ], [ %25, %20 ]
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %35
32: ; preds = %27
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
br label %35
35: ; preds = %32, %30
%36 = phi i32 [ 0, %30 ], [ %34, %32 ]
%37 = call i32 @close(i32 %8)
br label %38
38: ; preds = %10, %35, %3
%39 = phi i32 [ %5, %3 ], [ %12, %10 ], [ %36, %35 ]
ret i32 %39
}