#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 */
struct sansa_t {
  int dummy;
};
typedef int /*<<< orphan*/ loff_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int sansa_read(struct sansa_t *, unsigned char *, int);
scalar_t__ sansa_seek(struct sansa_t *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int sansa_seek_and_read(struct sansa_t *sansa, loff_t pos, unsigned char *buf,
                        int nbytes) {
  int n;
  if (sansa_seek(sansa, pos) < 0) {
    return -1;
  }
  if ((n = sansa_read(sansa, buf, nbytes)) < 0) {
    return -1;
  }
  if (n < nbytes) {
    fprintf(stderr, "[ERR]  Short read - requested %d bytes, received %d\n",
            nbytes, n);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sansa_seek_and_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <sansa_seek@plt>
cmp    $0x0,%rax
jge    1168 <sansa_seek_and_read+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <sansa_seek_and_read+0x9a>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <sansa_read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1190 <sansa_seek_and_read+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <sansa_seek_and_read+0x9a>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11c3 <sansa_seek_and_read+0x93>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xe4e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <sansa_seek_and_read+0x9a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sansa_seek_and_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rdi,%rbx
call   1050 <sansa_seek@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1187 <sansa_seek_and_read+0x57>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <sansa_read@plt>
test   %eax,%eax
js     1187 <sansa_seek_and_read+0x57>
xor    %ebp,%ebp
cmp    %r14d,%eax
jge    1187 <sansa_seek_and_read+0x57>
mov    0x2e71(%rip),%rcx
mov    (%rcx),%edi
lea    0xe88(%rip),%rsi
mov    %r14d,%edx
mov    %eax,%ecx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sansa_t = type { i32 }
@stderr = global i32 0
@.str = [53 x i8] c"[ERR] Short read - requested %d bytes, received %d\0A\00"
define i32 @sansa_seek_and_read(%struct.sansa_t* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sansa_t*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store %struct.sansa_t* %0, %struct.sansa_t** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load %struct.sansa_t*, %struct.sansa_t** %6
%12 = load i32, i32* %7
%13 = call i64 @sansa_seek(%struct.sansa_t* %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %33
16: ; preds = %4
%17 = load %struct.sansa_t*, %struct.sansa_t** %6
%18 = load i8*, i8** %8
%19 = load i32, i32* %9
%20 = call i32 @sansa_read(%struct.sansa_t* %17, i8* %18, i32 %19)
store i32 %20, i32* %10
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %5
br label %33
23: ; preds = %16
%24 = load i32, i32* %10
%25 = load i32, i32* %9
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i32, i32* %9
%30 = load i32, i32* %10
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %29, i32 %30)
store i32 -1, i32* %5
br label %33
32: ; preds = %23
store i32 0, i32* %5
br label %33
33: ; preds = %32, %27, %22, %15
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
%struct.sansa_t = type { i32 }
@stderr = global i32 0
@.str = [53 x i8] c"[ERR] Short read - requested %d bytes, received %d\0A\00"
define i32 @sansa_seek_and_read(%struct.sansa_t* %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i64 @sansa_seek(%struct.sansa_t* %0, i32 %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %4
%8 = tail call i32 @sansa_read(%struct.sansa_t* %0, i8* %2, i32 %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = icmp slt i32 %8, %3
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %8)
br label %15
15: ; preds = %10, %7, %4, %12
%16 = phi i32 [ -1, %12 ], [ -1, %4 ], [ -1, %7 ], [ 0, %10 ]
ret i32 %16
}