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

/* Variables and functions */
int /*<<< orphan*/ BLKGETSIZE;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, unsigned long long *);
int lseek(int, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, unsigned char *, int);
char *strerror(int);
scalar_t__ write(int, unsigned char *, int);
int main() {
  int fd;
  unsigned long long size;
  unsigned char *buf;
  off_t offset = 0;
  buf = (char *)malloc(4096);
  memset(buf, 0x2F, 4096);
  if ((fd = open("/dev/rd0", O_RDWR, O_NONBLOCK)) < 0) {
    printf("%s\n", strerror(errno));
    return errno;
  }
  if ((ioctl(fd, BLKGETSIZE, &size)) == -1) {
    printf("%s\n", strerror(errno));
    return errno;
  } else {
    printf("total block count: %llu\n", size);
    printf("total bytes count: %llu\n", (size * 512));
  }
  if ((write(fd, buf, 4096)) <= 0) {
    printf("%s\n", strerror(errno));
    return errno;
  }
  printf("wrote %d bytes at offset %lu\n", 4096, offset);
  offset = (offset + 65536);
  if ((lseek(fd, offset, SEEK_SET)) != offset) {
    printf("%s\n", strerror(errno));
    return errno;
  }
  printf("seeked to offset %lu\n", offset);
  if ((read(fd, buf, 4096)) <= 0) {
    printf("%s\n", strerror(errno));
    return errno;
  }
  printf("read %d bytes at offset %lu\n", 4096, offset);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x1000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    0xe10(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1231 <main+0x91>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e1 <main+0x241>
mov    -0x8(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xdf>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e1 <main+0x241>
mov    -0x10(%rbp),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
shl    $0x9,%rsi
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jg     12f2 <main+0x152>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e1 <main+0x241>
mov    -0x1c(%rbp),%edx
lea    0xd43(%rip),%rdi
mov    $0x1000,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x10000,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    -0x1c(%rbp),%eax
je     1360 <main+0x1c0>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e1 <main+0x241>
mov    -0x1c(%rbp),%esi
lea    0xcf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jg     13bc <main+0x21c>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xc65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e1 <main+0x241>
mov    -0x1c(%rbp),%edx
lea    0xcad(%rip),%rdi
mov    $0x1000,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x1000,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2f,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    0xe10(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     12d6 <main+0x126>
mov    %eax,%ebx
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0x126>
mov    0x8(%rsp),%rsi
lea    0xde0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rsi
shl    $0x9,%rsi
lea    0xde2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
test   %rax,%rax
jle    12d6 <main+0x126>
lea    0xdd9(%rip),%rdi
mov    $0x1000,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x10000,%esi
call   1060 <lseek@plt>
cmp    $0x10000,%eax
jne    12d6 <main+0x126>
lea    0xdc6(%rip),%rdi
xor    %ebp,%ebp
mov    $0x10000,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   10a0 <read@plt>
test   %rax,%rax
jle    12d6 <main+0x126>
lea    0xdb3(%rip),%rdi
mov    $0x1000,%esi
mov    $0x10000,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
jmp    12ee <main+0x13e>
mov    0x2cf3(%rip),%rbx
mov    (%rbx),%edi
call   10d0 <strerror@plt>
mov    %rax,%rdi
call   1030 <puts@plt>
mov    (%rbx),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/rd0\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@BLKGETSIZE = global i32 0
@.str.2 = [25 x i8] c"total block count: %llu\0A\00"
@.str.3 = [25 x i8] c"total bytes count: %llu\0A\00"
@.str.4 = [30 x i8] c"wrote %d bytes at offset %lu\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [22 x i8] c"seeked to offset %lu\0A\00"
@.str.6 = [29 x i8] c"read %d bytes at offset %lu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%6 = call i64 @malloc(i32 4096)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @memset(i8* %8, i32 47, i32 4096)
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NONBLOCK
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @errno
store i32 %18, i32* %1
br label %78
19: ; preds = %0
%20 = load i32, i32* %2
%21 = load i32, i32* @BLKGETSIZE
%22 = call i32 @ioctl(i32 %20, i32 %21, i64* %3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @errno
store i32 %28, i32* %1
br label %78
29: ; preds = %19
%30 = load i64, i64* %3
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 %30)
%32 = load i64, i64* %3
%33 = mul i64 %32, 512
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %33)
br label %35
35: ; preds = %29
%36 = load i32, i32* %2
%37 = load i8*, i8** %4
%38 = call i64 @write(i32 %36, i8* %37, i32 4096)
%39 = icmp sle i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @errno
store i32 %44, i32* %1
br label %78
45: ; preds = %35
%46 = load i32, i32* %5
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 4096, i32 %46)
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 65536
store i32 %49, i32* %5
%50 = load i32, i32* %2
%51 = load i32, i32* %5
%52 = load i32, i32* @SEEK_SET
%53 = call i32 @lseek(i32 %50, i32 %51, i32 %52)
%54 = load i32, i32* %5
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %61
56: ; preds = %45
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %58)
%60 = load i32, i32* @errno
store i32 %60, i32* %1
br label %78
61: ; preds = %45
%62 = load i32, i32* %5
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %2
%65 = load i8*, i8** %4
%66 = call i64 @read(i32 %64, i8* %65, i32 4096)
%67 = icmp sle i64 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %61
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %70)
%72 = load i32, i32* @errno
store i32 %72, i32* %1
br label %78
73: ; preds = %61
%74 = load i32, i32* %5
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 4096, i32 %74)
%76 = load i32, i32* %2
%77 = call i32 @close(i32 %76)
store i32 0, i32* %1
br label %78
78: ; preds = %73, %68, %56, %40, %24, %14
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/rd0\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@BLKGETSIZE = global i32 0
@.str.2 = [25 x i8] c"total block count: %llu\0A\00"
@.str.3 = [25 x i8] c"total bytes count: %llu\0A\00"
@.str.4 = [30 x i8] c"wrote %d bytes at offset %lu\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [22 x i8] c"seeked to offset %lu\0A\00"
@.str.6 = [29 x i8] c"read %d bytes at offset %lu\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i64 @malloc(i32 4096)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @memset(i8* %4, i32 47, i32 4096)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NONBLOCK
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @puts(i8* dereferenceable(1) %12)
%14 = load i32, i32* @errno
br label %59
15: ; preds = %0
%16 = load i32, i32* @BLKGETSIZE
%17 = call i32 @ioctl(i32 %8, i32 %16, i64* %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @puts(i8* dereferenceable(1) %21)
%23 = load i32, i32* @errno
br label %59
24: ; preds = %15
%25 = load i64, i64* %1
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 %25)
%27 = load i64, i64* %1
%28 = shl i64 %27, 9
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %28)
%30 = call i64 @write(i32 %8, i8* %4, i32 4096)
%31 = icmp slt i64 %30, 1
br i1 %31, label %32, label %37
32: ; preds = %24
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @puts(i8* dereferenceable(1) %34)
%36 = load i32, i32* @errno
br label %59
37: ; preds = %24
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 4096, i32 0)
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %8, i32 65536, i32 %39)
%41 = icmp eq i32 %40, 65536
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @puts(i8* dereferenceable(1) %44)
%46 = load i32, i32* @errno
br label %59
47: ; preds = %37
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 65536)
%49 = call i64 @read(i32 %8, i8* %4, i32 4096)
%50 = icmp slt i64 %49, 1
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @puts(i8* dereferenceable(1) %53)
%55 = load i32, i32* @errno
br label %59
56: ; preds = %47
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 4096, i32 65536)
%58 = call i32 @close(i32 %8)
br label %59
59: ; preds = %56, %51, %42, %32, %19, %10
%60 = phi i32 [ %14, %10 ], [ %23, %19 ], [ %36, %32 ], [ %46, %42 ], [ %55, %51 ], [ 0, %56 ]
ret i32 %60
}