#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 scalar_t__ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int fseeko(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/);
int fsync(int /*<<< orphan*/);
scalar_t__ ftello(int /*<<< orphan*/ *);
scalar_t__ fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ printAndFail(char *, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void updateBlockInFile(char *block, FILE *source, FILE *dest,
                       uint64_t readBytes, int sparseMode, char *readingMD4,
                       char *storedMD4, char *zeroBlockMD4) {
  if (fseeko(dest, ftello(source) - readBytes, SEEK_SET) == -1) {
    printAndFail("Failed to seek on file: %s\n", strerror(errno));
  }
  int isSourceBlockZero = strcmp(readingMD4, zeroBlockMD4) == 0 ? 1 : 0;
  int shouldWriteBlock = 0;
  if (sparseMode == 1) {
    if (isSourceBlockZero && storedMD4) {
      shouldWriteBlock = 1;
    } else if (isSourceBlockZero) {
      if (fseeko(dest, readBytes, SEEK_CUR) == -1) {
        printAndFail("Failed to sparse seek in file: %s\n", strerror(errno));
      }
      shouldWriteBlock = 0;
    } else {
      shouldWriteBlock = 1;
    }
  } else {
    shouldWriteBlock = 1;
  }
  if (shouldWriteBlock) {
    if (fwrite(block, 1, readBytes, dest) != readBytes) {
      printAndFail("Failed to write to file: %s\n", strerror(errno));
    }
    if (fsync(fileno(dest)) == -1) {
      printAndFail("Failed to sync destination file: %s\n", strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<updateBlockInFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ftello@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
sub    -0x20(%rbp),%rsi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   1080 <fseeko@plt>
cmp    $0xffffffff,%eax
jne    11f7 <updateBlockInFile+0x77>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xe0e(%rip),%rdi
call   1040 <printAndFail@plt>
mov    -0x30(%rbp),%rdi
mov    0x18(%rbp),%rsi
call   1050 <strcmp@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    12ac <updateBlockInFile+0x12c>
cmpl   $0x0,-0x34(%rbp)
je     124a <updateBlockInFile+0xca>
cmpq   $0x0,0x10(%rbp)
je     124a <updateBlockInFile+0xca>
movl   $0x1,-0x38(%rbp)
jmp    12a7 <updateBlockInFile+0x127>
cmpl   $0x0,-0x34(%rbp)
je     129b <updateBlockInFile+0x11b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
call   1080 <fseeko@plt>
cmp    $0xffffffff,%eax
jne    128f <updateBlockInFile+0x10f>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd92(%rip),%rdi
call   1040 <printAndFail@plt>
movl   $0x0,-0x38(%rbp)
jmp    12a2 <updateBlockInFile+0x122>
movl   $0x1,-0x38(%rbp)
jmp    12a7 <updateBlockInFile+0x127>
jmp    12b3 <updateBlockInFile+0x133>
movl   $0x1,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1333 <updateBlockInFile+0x1b3>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x20(%rbp),%rax
je     12f9 <updateBlockInFile+0x179>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd4b(%rip),%rdi
call   1040 <printAndFail@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
jne    132e <updateBlockInFile+0x1ae>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd33(%rip),%rdi
call   1040 <printAndFail@plt>
jmp    1333 <updateBlockInFile+0x1b3>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<updateBlockInFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r13
mov    %r8d,%r12d
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rdi,(%rsp)
mov    0x48(%rsp),%rbp
mov    0x40(%rsp),%r15
mov    %rsi,%rdi
call   1030 <ftello@plt>
sub    %rbx,%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%edx
mov    %r14,%rdi
mov    %rax,%rsi
call   1080 <fseeko@plt>
cmp    $0xffffffff,%eax
jne    11e5 <updateBlockInFile+0x65>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
call   1040 <printAndFail@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
sete   %cl
test   %r15,%r15
setne  %dl
cmp    $0x1,%r12d
jne    122d <updateBlockInFile+0xad>
and    %cl,%dl
jne    122d <updateBlockInFile+0xad>
test   %rax,%rax
jne    122d <updateBlockInFile+0xad>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <fseeko@plt>
cmp    $0xffffffff,%eax
jne    1276 <updateBlockInFile+0xf6>
lea    0xdf1(%rip),%rbx
jmp    128c <updateBlockInFile+0x10c>
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1090 <fwrite@plt>
cmp    %rbx,%rax
je     1262 <updateBlockInFile+0xe2>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
call   1040 <printAndFail@plt>
mov    %r14,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
je     1285 <updateBlockInFile+0x105>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd0(%rip),%rbx
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rbx,%rdi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printAndFail@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [28 x i8] c"Failed to seek on file: %s\0A\00"
@errno = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [35 x i8] c"Failed to sparse seek in file: %s\0A\00"
@.str.2 = [29 x i8] c"Failed to write to file: %s\0A\00"
@.str.3 = [37 x i8] c"Failed to sync destination file: %s\0A\00"
define void @updateBlockInFile(i8* %0, i32* %1, i32* %2, i64 %3, i32 %4, i8* %5, i8* %6, i8* %7) {
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %9
store i32* %1, i32** %10
store i32* %2, i32** %11
store i64 %3, i64* %12
store i32 %4, i32* %13
store i8* %5, i8** %14
store i8* %6, i8** %15
store i8* %7, i8** %16
%19 = load i32*, i32** %11
%20 = load i32*, i32** %10
%21 = call i64 @ftello(i32* %20)
%22 = load i64, i64* %12
%23 = sub nsw i64 %21, %22
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseeko(i32* %19, i64 %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %8
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @printAndFail(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %8
%32 = load i8*, i8** %14
%33 = load i8*, i8** %16
%34 = call i64 @strcmp(i8* %32, i8* %33)
%35 = icmp eq i64 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, i32 1, i32 0
store i32 %37, i32* %17
store i32 0, i32* %18
%38 = load i32, i32* %13
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %64
40: ; preds = %31
%41 = load i32, i32* %17
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i8*, i8** %15
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 1, i32* %18
br label %63
47: ; preds = %43, %40
%48 = load i32, i32* %17
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %61
50: ; preds = %47
%51 = load i32*, i32** %11
%52 = load i64, i64* %12
%53 = load i32, i32* @SEEK_CUR
%54 = call i32 @fseeko(i32* %51, i64 %52, i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %50
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 @printAndFail(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %56, %50
store i32 0, i32* %18
br label %62
61: ; preds = %47
store i32 1, i32* %18
br label %62
62: ; preds = %61, %60
br label %63
63: ; preds = %62, %46
br label %65
64: ; preds = %31
store i32 1, i32* %18
br label %65
65: ; preds = %64, %63
%66 = load i32, i32* %18
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %89
68: ; preds = %65
%69 = load i8*, i8** %9
%70 = load i64, i64* %12
%71 = load i32*, i32** %11
%72 = call i64 @fwrite(i8* %69, i32 1, i64 %70, i32* %71)
%73 = load i64, i64* %12
%74 = icmp ne i64 %72, %73
br i1 %74, label %75, label %79
75: ; preds = %68
%76 = load i32, i32* @errno
%77 = call i32 @strerror(i32 %76)
%78 = call i32 @printAndFail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %75, %68
%80 = load i32*, i32** %11
%81 = call i32 @fileno(i32* %80)
%82 = call i32 @fsync(i32 %81)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %88
84: ; preds = %79
%85 = load i32, i32* @errno
%86 = call i32 @strerror(i32 %85)
%87 = call i32 @printAndFail(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %86)
br label %88
88: ; preds = %84, %79
br label %89
89: ; preds = %88, %65
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [28 x i8] c"Failed to seek on file: %s\0A\00"
@errno = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [35 x i8] c"Failed to sparse seek in file: %s\0A\00"
@.str.2 = [29 x i8] c"Failed to write to file: %s\0A\00"
@.str.3 = [37 x i8] c"Failed to sync destination file: %s\0A\00"
define void @updateBlockInFile(i8* nocapture %0, i32* nocapture %1, i32* nocapture %2, i64 %3, i32 %4, i8* %5, i8* readnone %6, i8* %7) {
%9 = tail call i64 @ftello(i32* %1)
%10 = sub nsw i64 %9, %3
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseeko(i32* %2, i64 %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @printAndFail(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %8
%19 = tail call i64 @strcmp(i8* %5, i8* %7)
%20 = icmp eq i64 %19, 0
%21 = icmp ne i32 %4, 1
%22 = icmp ne i8* %6, null
%23 = and i1 %22, %20
%24 = select i1 %21, i1 true, i1 %23
%25 = xor i1 %20, true
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %31, label %27
27: ; preds = %18
%28 = load i32, i32* @SEEK_CUR
%29 = tail call i32 @fseeko(i32* %2, i64 %3, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %42, label %47
31: ; preds = %18
%32 = tail call i64 @fwrite(i8* %0, i32 1, i64 %3, i32* %2)
%33 = icmp eq i64 %32, %3
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i32, i32* @errno
%36 = tail call i32 @strerror(i32 %35)
%37 = tail call i32 @printAndFail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %31
%39 = tail call i32 @fileno(i32* %2)
%40 = tail call i32 @fsync(i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %38, %27
%43 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), %27 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), %38 ]
%44 = load i32, i32* @errno
%45 = tail call i32 @strerror(i32 %44)
%46 = tail call i32 @printAndFail(i8* %43, i32 %45)
br label %47
47: ; preds = %42, %27, %38
ret void
}