#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ _IO(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int posix_memalign(void **, int, int);
int pread(int, void *, int, scalar_t__);
int /*<<< orphan*/ printf(char *, int, unsigned long long, int);
int verify_pattern(void *, int, scalar_t__);
int read_sync(int buf_align, uint64_t offset, int len) {
  printf("read_sync buf_align %d offset %llu len %d\n", buf_align,
         (unsigned long long)offset, len);
  int fd = open("foo", O_RDONLY);
  ioctl(fd, _IO(0x97, 5));
  void *rawbuf;
  int r = posix_memalign(&rawbuf, 4096, len + buf_align);
  void *buf = (char *)rawbuf + buf_align;
  memset(buf, 0, len);
  r = pread(fd, buf, len, offset);
  close(fd);
  r = verify_pattern(buf, len, offset);
  free(rawbuf);
  return r;
}


x86 assembly (clang, -O0):
<read_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    0xe3d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    $0x97,%edi
mov    $0x5,%esi
call   1050 <_IO@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
call   1070 <ioctl@plt>
mov    -0x14(%rbp),%edx
add    -0x4(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   10c0 <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),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   10b0 <pread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   10a0 <verify_pattern@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sync>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe4b(%rip),%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1040 <printf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r15d
mov    $0x97,%edi
mov    $0x5,%esi
call   1050 <_IO@plt>
mov    %r15d,%edi
mov    %eax,%esi
call   1070 <ioctl@plt>
lea    (%rbx,%rbp,1),%edx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   10c0 <posix_memalign@plt>
movslq %ebp,%rbp
add    (%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %r14,%rcx
call   10b0 <pread@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   10a0 <verify_pattern@plt>
mov    %eax,%ebx
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"read_sync buf_align %d offset %llu len %d\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
define i32 @read_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*
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%11 = load i32, i32* %4
%12 = load i64, i64* %5
%13 = load i32, i32* %6
%14 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %11, i64 %12, i32 %13)
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = call i32 @_IO(i32 151, i32 5)
%19 = call i32 @ioctl(i32 %17, i32 %18)
%20 = load i32, i32* %6
%21 = load i32, i32* %4
%22 = add nsw i32 %20, %21
%23 = call i32 @posix_memalign(i8** %8, i32 4096, i32 %22)
store i32 %23, i32* %9
%24 = load i8*, i8** %8
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8* %27, i8** %10
%28 = load i8*, i8** %10
%29 = load i32, i32* %6
%30 = call i32 @memset(i8* %28, i32 0, i32 %29)
%31 = load i32, i32* %7
%32 = load i8*, i8** %10
%33 = load i32, i32* %6
%34 = load i64, i64* %5
%35 = call i32 @pread(i32 %31, i8* %32, i32 %33, i64 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = load i8*, i8** %10
%39 = load i32, i32* %6
%40 = load i64, i64* %5
%41 = call i32 @verify_pattern(i8* %38, i32 %39, i64 %40)
store i32 %41, i32* %9
%42 = load i8*, i8** %8
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %9
ret i32 %44
}

LLVM IR (-O2):
@.str = [43 x i8] c"read_sync buf_align %d offset %llu len %d\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
define i32 @read_sync(i32 %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = tail call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %0, i64 %1, i32 %2)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = tail call i32 @_IO(i32 151, i32 5)
%9 = tail call i32 @ioctl(i32 %7, i32 %8)
%10 = bitcast i8** %4 to i8*
%11 = add nsw i32 %2, %0
%12 = call i32 @posix_memalign(i8** %4, i32 4096, i32 %11)
%13 = load i8*, i8** %4
%14 = sext i32 %0 to i64
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = call i32 @memset(i8* %15, i32 0, i32 %2)
%17 = call i32 @pread(i32 %7, i8* %15, i32 %2, i64 %1)
%18 = call i32 @close(i32 %7)
%19 = call i32 @verify_pattern(i8* %15, i32 %2, i64 %1)
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
ret i32 %19
}