#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;
typedef int off_t;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int lseek(int, long, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
char *realloc(char *, long);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int write(int /*<<< orphan*/, char *, int);
void read_bytes(int fd, int bytes_num) {
  ssize_t bytes_read, bytes_written;
  off_t offset;
  long int neg_num, size;
  char *buf, *buf_temp;
  char *temp = NULL;
  if (fd == STDIN_FILENO) {
    buf_temp = malloc(sizeof(char) * BUFSIZ + sizeof(char));
    if (buf_temp == NULL) {
      perror("malloc");
      exit(EXIT_FAILURE);
    }
    buf = malloc(sizeof(char) * BUFSIZ + sizeof(char));
    if (buf == NULL) {
      perror("malloc");
      exit(EXIT_FAILURE);
    }
    size = BUFSIZ;
    while (1) {
      bytes_read = read(fd, buf_temp, size);
      if (bytes_read == -1) {
        perror("read");
        exit(EXIT_FAILURE);
      }
      if (bytes_read == 0)
        break;
      buf_temp[bytes_read] = '\0';
      strncat(buf, buf_temp, bytes_read);
      if (bytes_read == BUFSIZ) {
        size += BUFSIZ;
        temp = realloc(buf, size + sizeof(char));
        if (temp == NULL) {
          perror("realloc");
          free(buf);
          exit(EXIT_FAILURE);
        }
        buf = temp;
      }
    }
    free(buf_temp);
    temp = buf;
    buf += strlen(buf) - bytes_num;
  } else {
    buf = malloc(sizeof(char) * bytes_num);
    if (buf == NULL) {
      perror("malloc");
      exit(EXIT_FAILURE);
    }
    offset = lseek(fd, 0, SEEK_END);
    neg_num = bytes_num * (-1);
    if (offset <= bytes_num) {
      offset = lseek(fd, 0, SEEK_SET);
      if (offset == -1) {
        perror("lseek ");
        exit(EXIT_FAILURE);
      }
    } else {
      offset = lseek(fd, neg_num, SEEK_END);
      if (offset == -1) {
        perror("lseek ");
        exit(EXIT_FAILURE);
      }
    }
    bytes_read = read(fd, buf, bytes_num);
    if (bytes_read == -1) {
      perror("read");
      exit(EXIT_FAILURE);
    }
  }
  close(fd);
  bytes_written = write(STDOUT_FILENO, buf, bytes_num);
  if (bytes_written == -1) {
    perror("write");
    exit(EXIT_FAILURE);
  }
  if (bytes_written < bytes_num) {
    fprintf(stderr, "Less than requested bytes written\n");
  }
  if (fd == STDIN_FILENO)
    free(temp);
  else
    free(buf);
}


x86 assembly (clang, -O0):
<read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%eax
jne    1384 <read_bytes+0x1c4>
mov    0x2dd9(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    122a <read_bytes+0x6a>
lea    0xde9(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d97(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    126c <read_bytes+0xac>
lea    0xda7(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d55(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1090 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12b3 <read_bytes+0xf3>
lea    0xd67(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    12c2 <read_bytes+0x102>
jmp    1352 <read_bytes+0x192>
mov    -0x38(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1070 <strncat@plt>
mov    -0xc(%rbp),%eax
mov    0x2ce0(%rip),%rcx
cmp    (%rcx),%eax
jne    134d <read_bytes+0x18d>
mov    0x2cd1(%rip),%rax
movslq (%rax),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   10c0 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1345 <read_bytes+0x185>
lea    0xce3(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    127a <read_bytes+0xba>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
sub    -0x8(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    14a7 <read_bytes+0x2e7>
movslq -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    13bc <read_bytes+0x1fc>
lea    0xc57(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c03(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2c1a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    %eax,-0x14(%rbp)
imul   $0xffffffff,-0x8(%rbp),%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     142b <read_bytes+0x26b>
mov    -0x4(%rbp),%edi
mov    0x2bcc(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1426 <read_bytes+0x266>
lea    0xc01(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2b99(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    146c <read_bytes+0x2ac>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2ba7(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1467 <read_bytes+0x2a7>
lea    0xbc0(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2b58(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    146c <read_bytes+0x2ac>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1090 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    14a2 <read_bytes+0x2e2>
lea    0xb78(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2b1d(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    14a7 <read_bytes+0x2e7>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
mov    0x2b3a(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    14eb <read_bytes+0x32b>
lea    0xb43(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2ad4(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    150c <read_bytes+0x34c>
mov    0x2ad2(%rip),%rax
mov    (%rax),%edi
lea    0xb1a(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x4(%rbp),%eax
mov    0x2a9a(%rip),%rcx
cmp    (%rcx),%eax
jne    152c <read_bytes+0x36c>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    1535 <read_bytes+0x375>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r12d
mov    0x2dd8(%rip),%rbp
cmp    %edi,0x0(%rbp)
jne    1296 <read_bytes+0xd6>
mov    0x2de0(%rip),%rax
movslq (%rax),%r15
lea    0x1(%r15),%ebp
mov    %ebp,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     139d <read_bytes+0x1dd>
mov    %rax,%rbx
mov    %r14d,0x4(%rsp)
mov    %ebp,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     139d <read_bytes+0x1dd>
mov    %rax,%r13
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1090 <read@plt>
test   %eax,%eax
je     12d1 <read_bytes+0x111>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1383 <read_bytes+0x1c3>
movslq %ebp,%r14
movb   $0x0,(%rbx,%r14,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <strncat@plt>
mov    0x2d6b(%rip),%rax
cmp    (%rax),%ebp
jne    1220 <read_bytes+0x60>
lea    (%r15,%r14,1),%rbp
lea    (%r15,%r14,1),%rsi
add    $0x1,%rsi
mov    %r13,%rdi
call   10c0 <realloc@plt>
mov    %rbp,%r15
test   %rax,%rax
jne    1217 <read_bytes+0x57>
lea    0xd88(%rip),%rdi
call   10d0 <perror@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    138f <read_bytes+0x1cf>
mov    %r14d,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     139d <read_bytes+0x1dd>
mov    %rax,%r15
mov    0x2d2f(%rip),%rbx
mov    (%rbx),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    %r14d,%eax
jle    12f8 <read_bytes+0x138>
mov    %r14d,%eax
neg    %eax
movslq %eax,%rsi
mov    (%rbx),%edx
mov    %r12d,%edi
jmp    1306 <read_bytes+0x146>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    0x4(%rsp),%r14d
sub    %r14d,%eax
movslq %eax,%r15
add    %r13,%r15
mov    0x2cba(%rip),%rbp
jmp    132a <read_bytes+0x16a>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
je     13af <read_bytes+0x1ef>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     1383 <read_bytes+0x1c3>
xor    %r13d,%r13d
mov    %r12d,%edi
call   1080 <close@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     13a6 <read_bytes+0x1e6>
cmp    %r14d,%eax
jge    1365 <read_bytes+0x1a5>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0xcc1(%rip),%rsi
call   10a0 <fprintf@plt>
cmp    %r12d,0x0(%rbp)
cmove  %r13,%r15
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xc7d(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    0xc5c(%rip),%rdi
jmp    138a <read_bytes+0x1ca>
lea    0xc6e(%rip),%rdi
jmp    138a <read_bytes+0x1ca>
lea    0xc5e(%rip),%rdi
jmp    138a <read_bytes+0x1ca>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@BUFSIZ = global i32 0
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [8 x i8] c"realloc\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"lseek \00"
@STDOUT_FILENO = global i32 0
@.str.4 = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.5 = [35 x i8] c"Less than requested bytes written\0A\00"
define void @read_bytes(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i8* null, i8** %12
%13 = load i32, i32* %3
%14 = load i32, i32* @STDIN_FILENO
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %104
16: ; preds = %2
%17 = load i32, i32* @BUFSIZ
%18 = sext i32 %17 to i64
%19 = mul i64 1, %18
%20 = add i64 %19, 1
%21 = trunc i64 %20 to i32
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %16
%30 = load i32, i32* @BUFSIZ
%31 = sext i32 %30 to i64
%32 = mul i64 1, %31
%33 = add i64 %32, 1
%34 = trunc i64 %33 to i32
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %10
%36 = load i8*, i8** %10
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %42
38: ; preds = %29
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %29
%43 = load i32, i32* @BUFSIZ
%44 = sext i32 %43 to i64
store i64 %44, i64* %9
br label %45
45: ; preds = %42, %92
%46 = load i32, i32* %3
%47 = load i8*, i8** %11
%48 = load i64, i64* %9
%49 = trunc i64 %48 to i32
%50 = call i32 @read(i32 %46, i8* %47, i32 %49)
store i32 %50, i32* %5
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %45
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %45
%58 = load i32, i32* %5
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %57
br label %93
61: ; preds = %57
%62 = load i8*, i8** %11
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %10
%67 = load i8*, i8** %11
%68 = load i32, i32* %5
%69 = call i32 @strncat(i8* %66, i8* %67, i32 %68)
%70 = load i32, i32* %5
%71 = load i32, i32* @BUFSIZ
%72 = icmp eq i32 %70, %71
br i1 %72, label %73, label %92
73: ; preds = %61
%74 = load i32, i32* @BUFSIZ
%75 = sext i32 %74 to i64
%76 = load i64, i64* %9
%77 = add nsw i64 %76, %75
store i64 %77, i64* %9
%78 = load i8*, i8** %10
%79 = load i64, i64* %9
%80 = add i64 %79, 1
%81 = call i8* @realloc(i8* %78, i64 %80)
store i8* %81, i8** %12
%82 = load i8*, i8** %12
%83 = icmp eq i8* %82, null
br i1 %83, label %84, label %90
84: ; preds = %73
%85 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%86 = load i8*, i8** %10
%87 = call i32 @free(i8* %86)
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %73
%91 = load i8*, i8** %12
store i8* %91, i8** %10
br label %92
92: ; preds = %90, %61
br label %45
93: ; preds = %60
%94 = load i8*, i8** %11
%95 = call i32 @free(i8* %94)
%96 = load i8*, i8** %10
store i8* %96, i8** %12
%97 = load i8*, i8** %10
%98 = call i32 @strlen(i8* %97)
%99 = load i32, i32* %4
%100 = sub nsw i32 %98, %99
%101 = load i8*, i8** %10
%102 = sext i32 %100 to i64
%103 = getelementptr inbounds i8, i8* %101, i64 %102
store i8* %103, i8** %10
br label %161
104: ; preds = %2
%105 = load i32, i32* %4
%106 = sext i32 %105 to i64
%107 = mul i64 1, %106
%108 = trunc i64 %107 to i32
%109 = call i8* @malloc(i32 %108)
store i8* %109, i8** %10
%110 = load i8*, i8** %10
%111 = icmp eq i8* %110, null
br i1 %111, label %112, label %116
112: ; preds = %104
%113 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%114 = load i32, i32* @EXIT_FAILURE
%115 = call i32 @exit(i32 %114)
unreachable
116: ; preds = %104
%117 = load i32, i32* %3
%118 = load i32, i32* @SEEK_END
%119 = call i32 @lseek(i32 %117, i64 0, i32 %118)
store i32 %119, i32* %7
%120 = load i32, i32* %4
%121 = mul nsw i32 %120, -1
%122 = sext i32 %121 to i64
store i64 %122, i64* %8
%123 = load i32, i32* %7
%124 = load i32, i32* %4
%125 = icmp sle i32 %123, %124
br i1 %125, label %126, label %137
126: ; preds = %116
%127 = load i32, i32* %3
%128 = load i32, i32* @SEEK_SET
%129 = call i32 @lseek(i32 %127, i64 0, i32 %128)
store i32 %129, i32* %7
%130 = load i32, i32* %7
%131 = icmp eq i32 %130, -1
br i1 %131, label %132, label %136
132: ; preds = %126
%133 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%134 = load i32, i32* @EXIT_FAILURE
%135 = call i32 @exit(i32 %134)
unreachable
136: ; preds = %126
br label %149
137: ; preds = %116
%138 = load i32, i32* %3
%139 = load i64, i64* %8
%140 = load i32, i32* @SEEK_END
%141 = call i32 @lseek(i32 %138, i64 %139, i32 %140)
store i32 %141, i32* %7
%142 = load i32, i32* %7
%143 = icmp eq i32 %142, -1
br i1 %143, label %144, label %148
144: ; preds = %137
%145 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%146 = load i32, i32* @EXIT_FAILURE
%147 = call i32 @exit(i32 %146)
unreachable
148: ; preds = %137
br label %149
149: ; preds = %148, %136
%150 = load i32, i32* %3
%151 = load i8*, i8** %10
%152 = load i32, i32* %4
%153 = call i32 @read(i32 %150, i8* %151, i32 %152)
store i32 %153, i32* %5
%154 = load i32, i32* %5
%155 = icmp eq i32 %154, -1
br i1 %155, label %156, label %160
156: ; preds = %149
%157 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%158 = load i32, i32* @EXIT_FAILURE
%159 = call i32 @exit(i32 %158)
unreachable
160: ; preds = %149
br label %161
161: ; preds = %160, %93
%162 = load i32, i32* %3
%163 = call i32 @close(i32 %162)
%164 = load i32, i32* @STDOUT_FILENO
%165 = load i8*, i8** %10
%166 = load i32, i32* %4
%167 = call i32 @write(i32 %164, i8* %165, i32 %166)
store i32 %167, i32* %6
%168 = load i32, i32* %6
%169 = icmp eq i32 %168, -1
br i1 %169, label %170, label %174
170: ; preds = %161
%171 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%172 = load i32, i32* @EXIT_FAILURE
%173 = call i32 @exit(i32 %172)
unreachable
174: ; preds = %161
%175 = load i32, i32* %6
%176 = load i32, i32* %4
%177 = icmp slt i32 %175, %176
br i1 %177, label %178, label %181
178: ; preds = %174
%179 = load i32, i32* @stderr
%180 = call i32 @fprintf(i32 %179, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %181
181: ; preds = %178, %174
%182 = load i32, i32* %3
%183 = load i32, i32* @STDIN_FILENO
%184 = icmp eq i32 %182, %183
br i1 %184, label %185, label %188
185: ; preds = %181
%186 = load i8*, i8** %12
%187 = call i32 @free(i8* %186)
br label %191
188: ; preds = %181
%189 = load i8*, i8** %10
%190 = call i32 @free(i8* %189)
br label %191
191: ; preds = %188, %185
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@BUFSIZ = global i32 0
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [8 x i8] c"realloc\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"lseek \00"
@STDOUT_FILENO = global i32 0
@.str.4 = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.5 = [35 x i8] c"Less than requested bytes written\0A\00"
define void @read_bytes(i32 %0, i32 %1) {
%3 = load i32, i32* @STDIN_FILENO
%4 = icmp eq i32 %3, %0
br i1 %4, label %5, label %55
5: ; preds = %2
%6 = load i32, i32* @BUFSIZ
%7 = add i32 %6, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %5
%15 = tail call i8* @malloc(i32 %7)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %14
%22 = sext i32 %6 to i64
br label %23
23: ; preds = %39, %21
%24 = phi i64 [ %40, %39 ], [ %22, %21 ]
%25 = phi i8* [ %42, %39 ], [ %15, %21 ]
%26 = trunc i64 %24 to i32
br label %27
27: ; preds = %23, %33
%28 = tail call i32 @read(i32 %0, i8* %8, i32 %26)
switch i32 %28, label %33 [
i32 -1, label %29
i32 0, label %49
]
29: ; preds = %27
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %27
%34 = sext i32 %28 to i64
%35 = getelementptr inbounds i8, i8* %8, i64 %34
store i8 0, i8* %35
%36 = tail call i32 @strncat(i8* %25, i8* %8, i32 %28)
%37 = load i32, i32* @BUFSIZ
%38 = icmp eq i32 %28, %37
br i1 %38, label %39, label %27
39: ; preds = %33
%40 = add nsw i64 %24, %34
%41 = add i64 %40, 1
%42 = tail call i8* @realloc(i8* %25, i64 %41)
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %23
44: ; preds = %39
%45 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%46 = tail call i32 @free(i8* %25)
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %27
%50 = tail call i32 @free(i8* %8)
%51 = tail call i32 @strlen(i8* %25)
%52 = sub nsw i32 %51, %1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %25, i64 %53
br label %91
55: ; preds = %2
%56 = tail call i8* @malloc(i32 %1)
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = tail call i32 @exit(i32 %60)
unreachable
62: ; preds = %55
%63 = load i32, i32* @SEEK_END
%64 = tail call i32 @lseek(i32 %0, i64 0, i32 %63)
%65 = icmp sgt i32 %64, %1
br i1 %65, label %74, label %66
66: ; preds = %62
%67 = load i32, i32* @SEEK_SET
%68 = tail call i32 @lseek(i32 %0, i64 0, i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %84
70: ; preds = %66
%71 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%72 = load i32, i32* @EXIT_FAILURE
%73 = tail call i32 @exit(i32 %72)
unreachable
74: ; preds = %62
%75 = sub nsw i32 0, %1
%76 = sext i32 %75 to i64
%77 = load i32, i32* @SEEK_END
%78 = tail call i32 @lseek(i32 %0, i64 %76, i32 %77)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %84
80: ; preds = %74
%81 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%82 = load i32, i32* @EXIT_FAILURE
%83 = tail call i32 @exit(i32 %82)
unreachable
84: ; preds = %74, %66
%85 = tail call i32 @read(i32 %0, i8* %56, i32 %1)
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %91
87: ; preds = %84
%88 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%89 = load i32, i32* @EXIT_FAILURE
%90 = tail call i32 @exit(i32 %89)
unreachable
91: ; preds = %84, %49
%92 = phi i8* [ %54, %49 ], [ %56, %84 ]
%93 = phi i8* [ %25, %49 ], [ null, %84 ]
%94 = tail call i32 @close(i32 %0)
%95 = load i32, i32* @STDOUT_FILENO
%96 = tail call i32 @write(i32 %95, i8* %92, i32 %1)
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %102
98: ; preds = %91
%99 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%100 = load i32, i32* @EXIT_FAILURE
%101 = tail call i32 @exit(i32 %100)
unreachable
102: ; preds = %91
%103 = icmp slt i32 %96, %1
br i1 %103, label %104, label %107
104: ; preds = %102
%105 = load i32, i32* @stderr
%106 = tail call i32 @fprintf(i32 %105, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %107
107: ; preds = %104, %102
%108 = load i32, i32* @STDIN_FILENO
%109 = icmp eq i32 %108, %0
%110 = select i1 %109, i8* %93, i8* %92
%111 = tail call i32 @free(i8* %110)
ret void
}