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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
scalar_t__ lseek64(int, long long, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int write(int, unsigned char *, int);
int copy_block(int src_fd, int dst_fd, long block_num, int block_size,
               unsigned char *buffer) {
  long long filepos;
  ssize_t src_count;
  ssize_t dst_count;
  filepos = block_num;
  filepos *= block_size;
  if (lseek64(src_fd, filepos, SEEK_SET) < 0) {
    perror("lseek64 src_fd");
    return errno;
  }
  if (lseek64(dst_fd, filepos, SEEK_SET) < 0) {
    perror("lseek64 dst_fd");
    return errno;
  }
  src_count = read(src_fd, buffer, block_size);
  if (src_count != block_size) {
    if (src_count == -1) {
      perror("src read failed");
      return errno;
    } else {
      fprintf(stderr, "short read: %d of %d\n", src_count, block_size);
      return -1;
    }
  }
  dst_count = write(dst_fd, buffer, block_size);
  if (dst_count != block_size) {
    if (dst_count == -1) {
      perror("dst write failed");
      return errno;
    } else {
      fprintf(stderr, "short write: %d of %d\n", dst_count, block_size);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movslq -0x1c(%rbp),%rax
imul   -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
call   1070 <lseek64@plt>
cmp    $0x0,%rax
jge    11ba <copy_block+0x6a>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d5 <copy_block+0x185>
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   1070 <lseek64@plt>
cmp    $0x0,%rax
jge    11f6 <copy_block+0xa6>
lea    0xe2f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d5 <copy_block+0x185>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1262 <copy_block+0x112>
cmpl   $0xffffffff,-0x34(%rbp)
jne    123b <copy_block+0xeb>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d5 <copy_block+0x185>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xddd(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <copy_block+0x185>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12ce <copy_block+0x17e>
cmpl   $0xffffffff,-0x38(%rbp)
jne    12a7 <copy_block+0x157>
lea    0xdb3(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d5 <copy_block+0x185>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xd98(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <copy_block+0x185>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %ecx,%r12d
mov    %esi,%r15d
mov    %edi,%ebp
movslq %ecx,%rbx
imul   %rdx,%rbx
mov    0x2e5c(%rip),%r13
mov    0x0(%r13),%edx
mov    %rbx,%rsi
call   1070 <lseek64@plt>
test   %rax,%rax
js     1210 <copy_block+0xc0>
mov    0x0(%r13),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1070 <lseek64@plt>
test   %rax,%rax
js     1219 <copy_block+0xc9>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <read@plt>
cmp    %r12d,%eax
jne    11dd <copy_block+0x8d>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
mov    %eax,%edx
xor    %eax,%eax
cmp    %r12d,%edx
je     1201 <copy_block+0xb1>
cmp    $0xffffffff,%edx
je     122b <copy_block+0xdb>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
jmp    11f4 <copy_block+0xa4>
cmp    $0xffffffff,%eax
je     1222 <copy_block+0xd2>
mov    0x2df7(%rip),%rcx
mov    (%rcx),%edi
lea    0xe3c(%rip),%rsi
mov    %eax,%edx
mov    %r12d,%ecx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde9(%rip),%rdi
jmp    1232 <copy_block+0xe2>
lea    0xdef(%rip),%rdi
jmp    1232 <copy_block+0xe2>
lea    0xdf5(%rip),%rdi
jmp    1232 <copy_block+0xe2>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
jmp    1201 <copy_block+0xb1>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [15 x i8] c"lseek64 src_fd\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"lseek64 dst_fd\00"
@.str.2 = [16 x i8] c"src read failed\00"
@stderr = global i32 0
@.str.3 = [22 x i8] c"short read: %d of %d\0A\00"
@.str.4 = [17 x i8] c"dst write failed\00"
@.str.5 = [23 x i8] c"short write: %d of %d\0A\00"
define i32 @copy_block(i32 %0, i32 %1, i64 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%15 = load i64, i64* %9
store i64 %15, i64* %12
%16 = load i32, i32* %10
%17 = sext i32 %16 to i64
%18 = load i64, i64* %12
%19 = mul nsw i64 %18, %17
store i64 %19, i64* %12
%20 = load i32, i32* %7
%21 = load i64, i64* %12
%22 = load i32, i32* @SEEK_SET
%23 = call i64 @lseek64(i32 %20, i64 %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %5
%26 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @errno
store i32 %27, i32* %6
br label %76
28: ; preds = %5
%29 = load i32, i32* %8
%30 = load i64, i64* %12
%31 = load i32, i32* @SEEK_SET
%32 = call i64 @lseek64(i32 %29, i64 %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @errno
store i32 %36, i32* %6
br label %76
37: ; preds = %28
%38 = load i32, i32* %7
%39 = load i8*, i8** %11
%40 = load i32, i32* %10
%41 = call i32 @read(i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %13
%42 = load i32, i32* %13
%43 = load i32, i32* %10
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %56
45: ; preds = %37
%46 = load i32, i32* %13
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* @errno
store i32 %50, i32* %6
br label %76
51: ; preds = %45
%52 = load i32, i32* @stderr
%53 = load i32, i32* %13
%54 = load i32, i32* %10
%55 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %53, i32 %54)
store i32 -1, i32* %6
br label %76
56: ; preds = %37
%57 = load i32, i32* %8
%58 = load i8*, i8** %11
%59 = load i32, i32* %10
%60 = call i32 @write(i32 %57, i8* %58, i32 %59)
store i32 %60, i32* %14
%61 = load i32, i32* %14
%62 = load i32, i32* %10
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %75
64: ; preds = %56
%65 = load i32, i32* %14
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* @errno
store i32 %69, i32* %6
br label %76
70: ; preds = %64
%71 = load i32, i32* @stderr
%72 = load i32, i32* %14
%73 = load i32, i32* %10
%74 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %72, i32 %73)
store i32 -1, i32* %6
br label %76
75: ; preds = %56
store i32 0, i32* %6
br label %76
76: ; preds = %75, %70, %67, %51, %48, %34, %25
%77 = load i32, i32* %6
ret i32 %77
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [15 x i8] c"lseek64 src_fd\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"lseek64 dst_fd\00"
@.str.2 = [16 x i8] c"src read failed\00"
@stderr = global i32 0
@.str.3 = [22 x i8] c"short read: %d of %d\0A\00"
@.str.4 = [17 x i8] c"dst write failed\00"
@.str.5 = [23 x i8] c"short write: %d of %d\0A\00"
define i32 @copy_block(i32 %0, i32 %1, i64 %2, i32 %3, i8* nocapture %4) {
%6 = sext i32 %3 to i64
%7 = mul nsw i64 %6, %2
%8 = load i32, i32* @SEEK_SET
%9 = tail call i64 @lseek64(i32 %0, i64 %7, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @errno
br label %43
14: ; preds = %5
%15 = load i32, i32* @SEEK_SET
%16 = tail call i64 @lseek64(i32 %1, i64 %7, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @errno
br label %43
21: ; preds = %14
%22 = tail call i32 @read(i32 %0, i8* %4, i32 %3)
%23 = icmp eq i32 %22, %3
br i1 %23, label %32, label %24
24: ; preds = %21
%25 = icmp eq i32 %22, -1
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @errno
br label %43
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %22, i32 %3)
br label %43
32: ; preds = %21
%33 = tail call i32 @write(i32 %1, i8* %4, i32 %3)
%34 = icmp eq i32 %33, %3
br i1 %34, label %43, label %35
35: ; preds = %32
%36 = icmp eq i32 %33, -1
br i1 %36, label %37, label %40
37: ; preds = %35
%38 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* @errno
br label %43
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %33, i32 %3)
br label %43
43: ; preds = %32, %40, %37, %29, %26, %18, %11
%44 = phi i32 [ %13, %11 ], [ %20, %18 ], [ %28, %26 ], [ -1, %29 ], [ %39, %37 ], [ -1, %40 ], [ 0, %32 ]
ret i32 %44
}