#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 */

/* Variables and functions */
int /*<<< orphan*/ gcr_convert_GCR_to_sector(unsigned char *, unsigned char *,
                                             unsigned char *, unsigned int);
unsigned char *gcr_find_sector_data(unsigned char *, unsigned char *,
                                    unsigned int);
unsigned char *gcr_find_sector_header(unsigned int, unsigned int,
                                      unsigned char *, unsigned int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int gcr_read_sector(unsigned char *gcr_track_start_ptr,
                    unsigned int gcr_current_track_size,
                    unsigned char *readdata, unsigned int track,
                    unsigned int sector) {
  unsigned char buffer[260], *offset;
  offset = gcr_find_sector_header(track, sector, gcr_track_start_ptr,
                                  gcr_current_track_size);
  if (offset == NULL)
    return -1;
  offset =
      gcr_find_sector_data(offset, gcr_track_start_ptr, gcr_current_track_size);
  if (offset == NULL)
    return -1;
  gcr_convert_GCR_to_sector(buffer, offset, gcr_track_start_ptr,
                            gcr_current_track_size);
  if (buffer[0] != 0x7)
    return -1;
  memcpy(readdata, &buffer[1], 256);
  return 0;
}


x86 assembly (clang, -O0):
<gcr_read_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <gcr_find_sector_header@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    1190 <gcr_read_sector+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <gcr_read_sector+0xda>
mov    -0x138(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <gcr_find_sector_data@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    11c4 <gcr_read_sector+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <gcr_read_sector+0xda>
lea    -0x130(%rbp),%rdi
mov    -0x138(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1060 <gcr_convert_GCR_to_sector@plt>
movzbl -0x130(%rbp),%eax
cmp    $0x7,%eax
je     11fa <gcr_read_sector+0xba>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <gcr_read_sector+0xda>
mov    -0x20(%rbp),%rdi
lea    -0x130(%rbp),%rsi
add    $0x1,%rsi
mov    $0x100,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gcr_read_sector>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%rbp
mov    %ecx,%edi
mov    %r8d,%esi
mov    %rbp,%rdx
mov    %ebx,%ecx
call   1030 <gcr_find_sector_header@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     11ac <gcr_read_sector+0x6c>
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <gcr_find_sector_data@plt>
test   %rax,%rax
je     11ac <gcr_read_sector+0x6c>
mov    %rsp,%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
mov    %ebx,%ecx
call   1060 <gcr_convert_GCR_to_sector@plt>
cmpb   $0x7,(%rsp)
jne    11ac <gcr_read_sector+0x6c>
lea    0x1(%rsp),%rsi
mov    %r14,%rdi
mov    $0x100,%edx
call   1040 <memcpy@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gcr_read_sector(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [260 x i8]
%13 = alloca i8*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load i32, i32* %10
%15 = load i32, i32* %11
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = call i8* @gcr_find_sector_header(i32 %14, i32 %15, i8* %16, i32 %17)
store i8* %18, i8** %13
%19 = load i8*, i8** %13
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %5
store i32 -1, i32* %6
br label %45
22: ; preds = %5
%23 = load i8*, i8** %13
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = call i8* @gcr_find_sector_data(i8* %23, i8* %24, i32 %25)
store i8* %26, i8** %13
%27 = load i8*, i8** %13
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 -1, i32* %6
br label %45
30: ; preds = %22
%31 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%32 = load i8*, i8** %13
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i32 @gcr_convert_GCR_to_sector(i8* %31, i8* %32, i8* %33, i32 %34)
%36 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = icmp ne i32 %38, 7
br i1 %39, label %40, label %41
40: ; preds = %30
store i32 -1, i32* %6
br label %45
41: ; preds = %30
%42 = load i8*, i8** %9
%43 = getelementptr inbounds [260 x i8], [260 x i8]* %12, i64 0, i64 1
%44 = call i32 @memcpy(i8* %42, i8* %43, i32 256)
store i32 0, i32* %6
br label %45
45: ; preds = %41, %40, %29, %21
%46 = load i32, i32* %6
ret i32 %46
}

LLVM IR (-O2):
define i32 @gcr_read_sector(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca [260 x i8]
%7 = getelementptr inbounds [260 x i8], [260 x i8]* %6, i64 0, i64 0
%8 = tail call i8* @gcr_find_sector_header(i32 %3, i32 %4, i8* %0, i32 %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %20, label %10
10: ; preds = %5
%11 = tail call i8* @gcr_find_sector_data(i8* %8, i8* %0, i32 %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = call i32 @gcr_convert_GCR_to_sector(i8* %7, i8* %11, i8* %0, i32 %1)
%15 = load i8, i8* %7
%16 = icmp eq i8 %15, 7
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = getelementptr inbounds [260 x i8], [260 x i8]* %6, i64 0, i64 1
%19 = call i32 @memcpy(i8* %2, i8* %18, i32 256)
br label %20
20: ; preds = %13, %10, %5, %17
%21 = phi i32 [ 0, %17 ], [ -1, %5 ], [ -1, %10 ], [ -1, %13 ]
ret i32 %21
}