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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t,
                           int /*<<< orphan*/, ...);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
size_t read(int, char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int test_read(int fd, char *buffer, size_t len, off_t offset) {
  if (lseek(fd, offset, SEEK_SET) < 0) {
    fprintf(stderr, "Failed to seek to position %lu: %s\n", offset,
            strerror(errno));
    return (1);
  }
  if (read(fd, buffer, len) != len) {
    fprintf(stderr, "Failed to read %lu bytes, position %lu: %s\n", len, offset,
            strerror(errno));
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    11b9 <test_read+0x79>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movslq -0x1c(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <test_read+0xe9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
cmp    -0x18(%rbp),%rax
je     1222 <test_read+0xe2>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    %eax,%r8d
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <test_read+0xe9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
mov    %ecx,%esi
call   1030 <lseek@plt>
test   %rax,%rax
js     11b0 <test_read+0x70>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r15,%rcx
je     11e4 <test_read+0xa4>
mov    0x2e5b(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe88(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %r14d,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11df <test_read+0x9f>
mov    0x2e29(%rip),%rax
mov    (%rax),%ebx
movslq %r14d,%rbp
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe2f(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"Failed to seek to position %lu: %s\0A\00"
@errno = global i32 0
@.str.1 = [44 x i8] c"Failed to read %lu bytes, position %lu: %s\0A\00"
define i32 @test_read(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%10 = load i32, i32* %6
%11 = load i32, i32* %9
%12 = load i32, i32* @SEEK_SET
%13 = call i64 @lseek(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = load i32, i32* @stderr
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 (i32, i8*, i64, i32, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %18, i32 %20)
store i32 1, i32* %5
br label %37
22: ; preds = %4
%23 = load i32, i32* %6
%24 = load i8*, i8** %7
%25 = load i64, i64* %8
%26 = call i64 @read(i32 %23, i8* %24, i64 %25)
%27 = load i64, i64* %8
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = load i64, i64* %8
%32 = load i32, i32* %9
%33 = load i32, i32* @errno
%34 = call i32 @strerror(i32 %33)
%35 = call i32 (i32, i8*, i64, i32, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %31, i32 %32, i32 %34)
store i32 1, i32* %5
br label %37
36: ; preds = %22
store i32 0, i32* %5
br label %37
37: ; preds = %36, %29, %15
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"Failed to seek to position %lu: %s\0A\00"
@errno = global i32 0
@.str.1 = [44 x i8] c"Failed to read %lu bytes, position %lu: %s\0A\00"
define i32 @test_read(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i32 %3, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = sext i32 %3 to i64
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, i64, i32, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %10, i32 %12)
br label %22
14: ; preds = %4
%15 = tail call i64 @read(i32 %0, i8* %1, i64 %2)
%16 = icmp eq i64 %15, %2
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i32 @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, i64, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %2, i32 %3, i32 %20)
br label %22
22: ; preds = %14, %17, %8
%23 = phi i32 [ 1, %8 ], [ 1, %17 ], [ 0, %14 ]
ret i32 %23
}