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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fsync(int);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write_data(int, unsigned char *, size_t);
int pattern_pass(int fd, unsigned char *buf, size_t bufsz, size_t filesz) {
  size_t towrite;
  if (!bufsz || lseek(fd, 0, SEEK_SET) != 0)
    return -1;
  while (filesz > 0) {
    towrite = (filesz > bufsz ? bufsz : filesz);
    if (!write_data(fd, buf, towrite))
      return -1;
    filesz -= towrite;
  }
  fsync(fd);
  return 0;
}


x86 assembly (clang, -O0):
<pattern_pass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116f <pattern_pass+0x3f>
mov    -0x8(%rbp),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0x0,%rax
je     117b <pattern_pass+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <pattern_pass+0xce>
jmp    1180 <pattern_pass+0x50>
cmpq   $0x0,-0x20(%rbp)
jbe    11ef <pattern_pass+0xbf>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11a6 <pattern_pass+0x76>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11ae <pattern_pass+0x7e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <write_data@plt>
cmp    $0x0,%eax
jne    11db <pattern_pass+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <pattern_pass+0xce>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <pattern_pass+0x50>
mov    -0x8(%rbp),%edi
call   1050 <fsync@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pattern_pass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdx,%rdx
je     119c <pattern_pass+0x6c>
mov    %rcx,%rbp
mov    %rdx,%r13
mov    %rsi,%r12
mov    %edi,%r15d
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
test   %rax,%rax
jne    119c <pattern_pass+0x6c>
test   %rbp,%rbp
je     1191 <pattern_pass+0x61>
nopl   0x0(%rax)
cmp    %r13,%rbp
mov    %rbp,%rbx
cmova  %r13,%rbx
mov    %r15d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <write_data@plt>
test   %eax,%eax
je     119c <pattern_pass+0x6c>
sub    %rbx,%rbp
jne    1170 <pattern_pass+0x40>
mov    %r15d,%edi
call   1050 <fsync@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @pattern_pass(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%11 = load i64, i64* %8
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %4
%14 = load i32, i32* %6
%15 = load i32, i32* @SEEK_SET
%16 = call i64 @lseek(i32 %14, i32 0, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13, %4
store i32 -1, i32* %5
br label %46
19: ; preds = %13
br label %20
20: ; preds = %39, %19
%21 = load i64, i64* %9
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %20
%24 = load i64, i64* %9
%25 = load i64, i64* %8
%26 = icmp ugt i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i64, i64* %8
br label %31
29: ; preds = %23
%30 = load i64, i64* %9
br label %31
31: ; preds = %29, %27
%32 = phi i64 [ %28, %27 ], [ %30, %29 ]
store i64 %32, i64* %10
%33 = load i32, i32* %6
%34 = load i8*, i8** %7
%35 = load i64, i64* %10
%36 = call i32 @write_data(i32 %33, i8* %34, i64 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %31
store i32 -1, i32* %5
br label %46
39: ; preds = %31
%40 = load i64, i64* %10
%41 = load i64, i64* %9
%42 = sub i64 %41, %40
store i64 %42, i64* %9
br label %20
43: ; preds = %20
%44 = load i32, i32* %6
%45 = call i32 @fsync(i32 %44)
store i32 0, i32* %5
br label %46
46: ; preds = %43, %38, %18
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @pattern_pass(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %23, label %6
6: ; preds = %4
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @lseek(i32 %0, i32 0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %6
%11 = icmp eq i64 %3, 0
br i1 %11, label %21, label %14
12: ; preds = %14
%13 = icmp eq i64 %20, 0
br i1 %13, label %21, label %14
14: ; preds = %10, %12
%15 = phi i64 [ %20, %12 ], [ %3, %10 ]
%16 = icmp ugt i64 %15, %2
%17 = select i1 %16, i64 %2, i64 %15
%18 = tail call i32 @write_data(i32 %0, i8* %1, i64 %17)
%19 = icmp eq i32 %18, 0
%20 = sub i64 %15, %17
br i1 %19, label %23, label %12
21: ; preds = %12, %10
%22 = tail call i32 @fsync(i32 %0)
br label %23
23: ; preds = %14, %4, %6, %21
%24 = phi i32 [ 0, %21 ], [ -1, %6 ], [ -1, %4 ], [ -1, %14 ]
ret i32 %24
}