#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;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ memxor(void *, unsigned char *, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_and_xor(void *dest, int in, ssize_t n) {
  unsigned char *buf = malloc((1024 * 1024));
  while (n > 0) {
    ssize_t n_buf;
    if (n < (1024 * 1024))
      n_buf = n;
    else
      n_buf = (1024 * 1024);
    if (read(in, buf, n_buf) != n_buf) {
      fprintf(stderr, "cdrverify: read() failed (%s)\n", strerror(errno));
      free(buf);
      return 1;
    }
    memxor(dest, buf, n_buf);
    dest = n_buf + (unsigned char *)dest;
    n -= n_buf;
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<read_and_xor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1233 <read_and_xor+0xd3>
cmpl   $0x100000,-0x18(%rbp)
jge    11a2 <read_and_xor+0x42>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <read_and_xor+0x49>
movl   $0x100000,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <read@plt>
cmp    -0x24(%rbp),%eax
je     1202 <read_and_xor+0xa2>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe18(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1243 <read_and_xor+0xe3>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memxor@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    1180 <read_and_xor+0x20>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_and_xor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r13
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
xor    %r14d,%r14d
test   %ebp,%ebp
jle    11f5 <read_and_xor+0x95>
nopw   0x0(%rax,%rax,1)
cmp    $0x100000,%ebp
mov    $0x100000,%ebx
cmovb  %ebp,%ebx
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    %ebx,%eax
jne    11c7 <read_and_xor+0x67>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <memxor@plt>
mov    %ebx,%eax
add    %rax,%r13
sub    %ebx,%ebp
jg     1190 <read_and_xor+0x30>
jmp    11f5 <read_and_xor+0x95>
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%r14d
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"cdrverify: read() failed (%s)\0A\00"
@errno = global i32 0
define i32 @read_and_xor(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = call i8* @malloc(i32 1048576)
store i8* %10, i8** %8
br label %11
11: ; preds = %34, %3
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %46
14: ; preds = %11
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 1048576
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32, i32* %7
store i32 %18, i32* %9
br label %20
19: ; preds = %14
store i32 1048576, i32* %9
br label %20
20: ; preds = %19, %17
%21 = load i32, i32* %6
%22 = load i8*, i8** %8
%23 = load i32, i32* %9
%24 = call i32 @read(i32 %21, i8* %22, i32 %23)
%25 = load i32, i32* %9
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %34
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
store i32 1, i32* %4
br label %49
34: ; preds = %20
%35 = load i8*, i8** %5
%36 = load i8*, i8** %8
%37 = load i32, i32* %9
%38 = call i32 @memxor(i8* %35, i8* %36, i32 %37)
%39 = load i32, i32* %9
%40 = load i8*, i8** %5
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %5
%43 = load i32, i32* %9
%44 = load i32, i32* %7
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %7
br label %11
46: ; preds = %11
%47 = load i8*, i8** %8
%48 = call i32 @free(i8* %47)
store i32 0, i32* %4
br label %49
49: ; preds = %46, %27
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"cdrverify: read() failed (%s)\0A\00"
@errno = global i32 0
define i32 @read_and_xor(i8* %0, i32 %1, i32 %2) {
%4 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %24
6: ; preds = %3, %18
%7 = phi i32 [ %22, %18 ], [ %2, %3 ]
%8 = phi i8* [ %21, %18 ], [ %0, %3 ]
%9 = icmp ult i32 %7, 1048576
%10 = select i1 %9, i32 %7, i32 1048576
%11 = tail call i32 @read(i32 %1, i8* %4, i32 %10)
%12 = icmp eq i32 %11, %10
br i1 %12, label %18, label %13
13: ; preds = %6
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %24
18: ; preds = %6
%19 = tail call i32 @memxor(i8* %8, i8* %4, i32 %10)
%20 = zext i32 %10 to i64
%21 = getelementptr inbounds i8, i8* %8, i64 %20
%22 = sub nsw i32 %7, %10
%23 = icmp sgt i32 %22, 0
br i1 %23, label %6, label %24
24: ; preds = %18, %3, %13
%25 = phi i32 [ 1, %13 ], [ 0, %3 ], [ 0, %18 ]
%26 = tail call i32 @free(i8* %4)
ret i32 %25
}