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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ full_read(int, char *, size_t);
scalar_t__ memcmp(char *, char *, size_t);
int open(char const *, int /*<<< orphan*/);
scalar_t__ xmalloc(int);
int compare_files(const char *filename1, const char *filename2, size_t size,
                  size_t blocksize) {
  int fd1, fd2;
  int ret = -1;
  fd1 = open(filename1, O_RDONLY);
  if (fd1 >= 0) {
    fd2 = open(filename2, O_RDONLY);
    if (fd2 >= 0) {
      char *buf1 = (char *)xmalloc(2 * blocksize);
      char *buf2 = buf1 + blocksize;
      ret = 0;
      while (size > 0) {
        size_t bytes = (size < blocksize ? size : blocksize);
        if (full_read(fd1, buf1, bytes) < (ssize_t)bytes) {
          ret = -1;
          break;
        }
        if (full_read(fd2, buf2, bytes) < (ssize_t)bytes) {
          ret = -1;
          break;
        }
        if (memcmp(buf1, buf2, bytes) != 0) {
          ret = 1;
          break;
        }
        size -= bytes;
      }
      free(buf1);
      close(fd2);
    }
    close(fd1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<compare_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12b9 <compare_files+0x159>
mov    -0x10(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     12b1 <compare_files+0x151>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    12a0 <compare_files+0x140>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1209 <compare_files+0xa9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1211 <compare_files+0xb1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <full_read@plt>
cmp    -0x48(%rbp),%rax
jge    123f <compare_files+0xdf>
movl   $0xffffffff,-0x2c(%rbp)
jmp    12a0 <compare_files+0x140>
mov    -0x28(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <full_read@plt>
cmp    -0x48(%rbp),%rax
jge    1265 <compare_files+0x105>
movl   $0xffffffff,-0x2c(%rbp)
jmp    12a0 <compare_files+0x140>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     128c <compare_files+0x12c>
movl   $0x1,-0x2c(%rbp)
jmp    12a0 <compare_files+0x140>
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11e3 <compare_files+0x83>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compare_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    0x2e6d(%rip),%r15
mov    (%r15),%esi
call   1080 <open@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     120d <compare_files+0xad>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,(%rsp)
test   %eax,%eax
js     1214 <compare_files+0xb4>
lea    (%r14,%r14,1),%edi
call   1050 <xmalloc@plt>
mov    %rax,%r15
test   %rbp,%rbp
je     1209 <compare_files+0xa9>
mov    %r15,%r13
add    %r14,%r13
mov    $0xffffffff,%ebx
nopl   (%rax)
cmp    %r14,%rbp
mov    %r14,%r12
cmovb  %rbp,%r12
mov    0x4(%rsp),%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <full_read@plt>
cmp    %r12,%rax
jl     1220 <compare_files+0xc0>
mov    (%rsp),%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <full_read@plt>
cmp    %r12,%rax
jl     1220 <compare_files+0xc0>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    121b <compare_files+0xbb>
sub    %r12,%rbp
jne    11c0 <compare_files+0x60>
xor    %ebx,%ebx
jmp    1220 <compare_files+0xc0>
mov    $0xffffffff,%ebx
jmp    1239 <compare_files+0xd9>
mov    $0xffffffff,%ebx
jmp    1230 <compare_files+0xd0>
mov    $0x1,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @compare_files(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
store i32 -1, i32* %11
%15 = load i8*, i8** %5
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %82
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %79
26: ; preds = %20
%27 = load i64, i64* %8
%28 = mul i64 2, %27
%29 = trunc i64 %28 to i32
%30 = call i64 @xmalloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %12
%32 = load i8*, i8** %12
%33 = load i64, i64* %8
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %13
store i32 0, i32* %11
br label %35
35: ; preds = %70, %26
%36 = load i64, i64* %7
%37 = icmp ugt i64 %36, 0
br i1 %37, label %38, label %74
38: ; preds = %35
%39 = load i64, i64* %7
%40 = load i64, i64* %8
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = load i64, i64* %7
br label %46
44: ; preds = %38
%45 = load i64, i64* %8
br label %46
46: ; preds = %44, %42
%47 = phi i64 [ %43, %42 ], [ %45, %44 ]
store i64 %47, i64* %14
%48 = load i32, i32* %9
%49 = load i8*, i8** %12
%50 = load i64, i64* %14
%51 = call i64 @full_read(i32 %48, i8* %49, i64 %50)
%52 = load i64, i64* %14
%53 = icmp slt i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %46
store i32 -1, i32* %11
br label %74
55: ; preds = %46
%56 = load i32, i32* %10
%57 = load i8*, i8** %13
%58 = load i64, i64* %14
%59 = call i64 @full_read(i32 %56, i8* %57, i64 %58)
%60 = load i64, i64* %14
%61 = icmp slt i64 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %55
store i32 -1, i32* %11
br label %74
63: ; preds = %55
%64 = load i8*, i8** %12
%65 = load i8*, i8** %13
%66 = load i64, i64* %14
%67 = call i64 @memcmp(i8* %64, i8* %65, i64 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %63
store i32 1, i32* %11
br label %74
70: ; preds = %63
%71 = load i64, i64* %14
%72 = load i64, i64* %7
%73 = sub i64 %72, %71
store i64 %73, i64* %7
br label %35
74: ; preds = %69, %62, %54, %35
%75 = load i8*, i8** %12
%76 = call i32 @free(i8* %75)
%77 = load i32, i32* %10
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %74, %20
%80 = load i32, i32* %9
%81 = call i32 @close(i32 %80)
br label %82
82: ; preds = %79, %4
%83 = load i32, i32* %11
ret i32 %83
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @compare_files(i8* nocapture %0, i8* nocapture %1, i64 %2, i64 %3) {
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %41
8: ; preds = %4
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %1, i32 %9)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %38
12: ; preds = %8
%13 = trunc i64 %3 to i32
%14 = shl i32 %13, 1
%15 = tail call i64 @xmalloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = getelementptr inbounds i8, i8* %16, i64 %3
%18 = icmp eq i64 %2, 0
br i1 %18, label %34, label %21
19: ; preds = %30
%20 = icmp eq i64 %33, 0
br i1 %20, label %34, label %21
21: ; preds = %12, %19
%22 = phi i64 [ %33, %19 ], [ %2, %12 ]
%23 = icmp ult i64 %22, %3
%24 = select i1 %23, i64 %22, i64 %3
%25 = tail call i64 @full_read(i32 %6, i8* %16, i64 %24)
%26 = icmp slt i64 %25, %24
br i1 %26, label %34, label %27
27: ; preds = %21
%28 = tail call i64 @full_read(i32 %10, i8* %17, i64 %24)
%29 = icmp slt i64 %28, %24
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = tail call i64 @memcmp(i8* %16, i8* %17, i64 %24)
%32 = icmp eq i64 %31, 0
%33 = sub i64 %22, %24
br i1 %32, label %19, label %34
34: ; preds = %19, %21, %27, %30, %12
%35 = phi i32 [ 0, %12 ], [ 1, %30 ], [ -1, %27 ], [ -1, %21 ], [ 0, %19 ]
%36 = tail call i32 @free(i8* %16)
%37 = tail call i32 @close(i32 %10)
br label %38
38: ; preds = %34, %8
%39 = phi i32 [ %35, %34 ], [ -1, %8 ]
%40 = tail call i32 @close(i32 %6)
br label %41
41: ; preds = %38, %4
%42 = phi i32 [ %39, %38 ], [ -1, %4 ]
ret i32 %42
}