#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 /*<<< orphan*/ compare;

/* Variables and functions */
scalar_t__ memcmp(void *, char *, int);
int read(int, char *, int);
int match_data(int fd, void *buffer, unsigned long size) {
  while (size) {
    char compare[1024];
    int ret = read(fd, compare, sizeof(compare));
    if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
      return -1;
    size -= ret;
    buffer += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<match_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d0 <match_data+0xb0>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    1199 <match_data+0x79>
movslq -0x424(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     1199 <match_data+0x79>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11a5 <match_data+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <match_data+0xb7>
movslq -0x424(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x424(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1136 <match_data+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
xor    %r14d,%r14d
test   %rdx,%rdx
je     118d <match_data+0x6d>
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %edi,%r15d
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1187 <match_data+0x67>
mov    %eax,%ebp
cmp    %rbp,%rbx
jb     1187 <match_data+0x67>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    1187 <match_data+0x67>
add    %rbp,%r13
sub    %rbp,%rbx
jne    1150 <match_data+0x30>
jmp    118d <match_data+0x6d>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @match_data(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %10
10: ; preds = %31, %3
%11 = load i64, i64* %7
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %10
%14 = load i32, i32* %5
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 1024)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp sle i32 %17, 0
br i1 %18, label %30, label %19
19: ; preds = %13
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = load i64, i64* %7
%23 = icmp ugt i64 %21, %22
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%27 = load i32, i32* %9
%28 = call i64 @memcmp(i8* %25, i8* %26, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24, %19, %13
store i32 -1, i32* %4
br label %41
31: ; preds = %24
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = load i64, i64* %7
%35 = sub i64 %34, %33
store i64 %35, i64* %7
%36 = load i32, i32* %9
%37 = load i8*, i8** %6
%38 = sext i32 %36 to i64
%39 = getelementptr i8, i8* %37, i64 %38
store i8* %39, i8** %6
br label %10
40: ; preds = %10
store i32 0, i32* %4
br label %41
41: ; preds = %40, %30
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @match_data(i32 %0, i8* %1, i64 %2) {
%4 = alloca [1024 x i8]
%5 = icmp eq i64 %2, 0
br i1 %5, label %24, label %6
6: ; preds = %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
br label %8
8: ; preds = %6, %20
%9 = phi i64 [ %2, %6 ], [ %21, %20 ]
%10 = phi i8* [ %1, %6 ], [ %22, %20 ]
%11 = call i32 @read(i32 %0, i8* %7, i32 1024)
%12 = icmp slt i32 %11, 1
br i1 %12, label %19, label %13
13: ; preds = %8
%14 = zext i32 %11 to i64
%15 = icmp ult i64 %9, %14
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = call i64 @memcmp(i8* %10, i8* %7, i32 %11)
%18 = icmp eq i64 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %16, %13, %8
br label %24
20: ; preds = %16
%21 = sub i64 %9, %14
%22 = getelementptr i8, i8* %10, i64 %14
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %8
24: ; preds = %20, %3, %19
%25 = phi i32 [ -1, %19 ], [ 0, %3 ], [ 0, %20 ]
ret i32 %25
}