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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ _IO(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ generate_pattern(void *, int, scalar_t__);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int open(char *, int, ...);
int posix_memalign(void **, int, int);
int pread(int, void *, int, scalar_t__);
int /*<<< orphan*/ printf(char *, int, unsigned long long, int);
int pwrite(int, void *, int, scalar_t__);
int /*<<< orphan*/ unlink(char *);
int verify_pattern(void *, int, scalar_t__);
int write_sync(int buf_align, uint64_t offset, int len) {
  printf("write_sync buf_align %d offset %llu len %d\n", buf_align,
         (unsigned long long)offset, len);
  int fd = open("foo", O_WRONLY | O_CREAT, 0644);
  ioctl(fd, _IO(0x97, 5));
  void *rawbuf;
  int r = posix_memalign(&rawbuf, 4096, len + buf_align);
  void *buf = (char *)rawbuf + buf_align;
  generate_pattern(buf, len, offset);
  r = pwrite(fd, buf, len, offset);
  close(fd);
  fd = open("foo", O_RDONLY);
  void *buf2 = malloc(len);
  memset(buf2, 0, len);
  r = pread(fd, buf2, len, offset);
  close(fd);
  r = verify_pattern(buf2, len, offset);
  unlink("foo");
  free(buf2);
  free(rawbuf);
  return r;
}


x86 assembly (clang, -O0):
<write_sync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
lea    0xe0b(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   10d0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x97,%edi
mov    $0x5,%esi
call   1060 <_IO@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
call   1080 <ioctl@plt>
mov    -0x14(%rbp),%edx
add    -0x4(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1100 <posix_memalign@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x4(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   10b0 <generate_pattern@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   10c0 <pwrite@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   10d0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   10f0 <pread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   10e0 <verify_pattern@plt>
mov    %eax,-0x24(%rbp)
lea    0xd1d(%rip),%rdi
call   1040 <unlink@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sync>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xe06(%rip),%rdi
mov    %r12d,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1050 <printf@plt>
mov    0x2dba(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe0c(%rip),%r15
mov    %r15,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   10d0 <open@plt>
mov    %eax,%r13d
mov    $0x97,%edi
mov    $0x5,%esi
call   1060 <_IO@plt>
mov    %r13d,%edi
mov    %eax,%esi
call   1080 <ioctl@plt>
lea    (%rbx,%r12,1),%edx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1100 <posix_memalign@plt>
movslq %r12d,%rbp
add    (%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   10b0 <generate_pattern@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %r14,%rcx
call   10c0 <pwrite@plt>
mov    %r13d,%edi
call   1090 <close@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   10d0 <open@plt>
mov    %eax,%r12d
mov    %ebx,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %r14,%rcx
call   10f0 <pread@plt>
mov    %r12d,%edi
call   1090 <close@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   10e0 <verify_pattern@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <unlink@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"write_sync buf_align %d offset %llu len %d\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
define i32 @write_sync(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%12 = load i32, i32* %4
%13 = load i64, i64* %5
%14 = load i32, i32* %6
%15 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %12, i64 %13, i32 %14)
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 420)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = call i32 @_IO(i32 151, i32 5)
%22 = call i32 @ioctl(i32 %20, i32 %21)
%23 = load i32, i32* %6
%24 = load i32, i32* %4
%25 = add nsw i32 %23, %24
%26 = call i32 @posix_memalign(i8** %8, i32 4096, i32 %25)
store i32 %26, i32* %9
%27 = load i8*, i8** %8
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8* %30, i8** %10
%31 = load i8*, i8** %10
%32 = load i32, i32* %6
%33 = load i64, i64* %5
%34 = call i32 @generate_pattern(i8* %31, i32 %32, i64 %33)
%35 = load i32, i32* %7
%36 = load i8*, i8** %10
%37 = load i32, i32* %6
%38 = load i64, i64* %5
%39 = call i32 @pwrite(i32 %35, i8* %36, i32 %37, i64 %38)
store i32 %39, i32* %9
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @O_RDONLY
%43 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %42)
store i32 %43, i32* %7
%44 = load i32, i32* %6
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %11
%46 = load i8*, i8** %11
%47 = load i32, i32* %6
%48 = call i32 @memset(i8* %46, i32 0, i32 %47)
%49 = load i32, i32* %7
%50 = load i8*, i8** %11
%51 = load i32, i32* %6
%52 = load i64, i64* %5
%53 = call i32 @pread(i32 %49, i8* %50, i32 %51, i64 %52)
store i32 %53, i32* %9
%54 = load i32, i32* %7
%55 = call i32 @close(i32 %54)
%56 = load i8*, i8** %11
%57 = load i32, i32* %6
%58 = load i64, i64* %5
%59 = call i32 @verify_pattern(i8* %56, i32 %57, i64 %58)
store i32 %59, i32* %9
%60 = call i32 @unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%61 = load i8*, i8** %11
%62 = call i32 @free(i8* %61)
%63 = load i8*, i8** %8
%64 = call i32 @free(i8* %63)
%65 = load i32, i32* %9
ret i32 %65
}

LLVM IR (-O2):
@.str = [44 x i8] c"write_sync buf_align %d offset %llu len %d\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
define i32 @write_sync(i32 %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %0, i64 %1, i32 %2)
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 420)
%10 = tail call i32 @_IO(i32 151, i32 5)
%11 = tail call i32 @ioctl(i32 %9, i32 %10)
%12 = bitcast i8** %4 to i8*
%13 = add nsw i32 %2, %0
%14 = call i32 @posix_memalign(i8** %4, i32 4096, i32 %13)
%15 = load i8*, i8** %4
%16 = sext i32 %0 to i64
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = call i32 @generate_pattern(i8* %17, i32 %2, i64 %1)
%19 = call i32 @pwrite(i32 %9, i8* %17, i32 %2, i64 %1)
%20 = call i32 @close(i32 %9)
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i8* @malloc(i32 %2)
%24 = call i32 @memset(i8* %23, i32 0, i32 %2)
%25 = call i32 @pread(i32 %22, i8* %23, i32 %2, i64 %1)
%26 = call i32 @close(i32 %22)
%27 = call i32 @verify_pattern(i8* %23, i32 %2, i64 %1)
%28 = call i32 @unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @free(i8* %23)
%30 = load i8*, i8** %4
%31 = call i32 @free(i8* %30)
ret i32 %27
}