#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 /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int test_read(int, char *, int, int /*<<< orphan*/);
int test_write(int, char *, int, int /*<<< orphan*/);
int test_write_read(int fd, char *buffer1, size_t len, off_t offset) {
  char buffer2[32];
  int ret;
  memset(buffer2, 0, 32);
  if ((ret = test_write(fd, buffer1, 32, offset)) != 0)
    return ret;
  if ((ret = test_read(fd, buffer2, 32, offset)) != 0)
    return ret;
  if (memcmp(buffer1, buffer2, 32)) {
    fprintf(stderr, "Data read is not same as data written, offset = %lu",
            offset);
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_write_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
mov    $0x20,%edx
call   1030 <test_write@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     11a1 <test_write_read+0x51>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <test_write_read+0xc3>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
mov    $0x20,%edx
call   1050 <test_read@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     11cc <test_write_read+0x7c>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <test_write_read+0xc3>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     120c <test_write_read+0xbc>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe05(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1213 <test_write_read+0xc3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x20,%edx
mov    %r14d,%ecx
call   1030 <test_write@plt>
test   %eax,%eax
jne    11d1 <test_write_read+0x81>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x20,%edx
mov    %r14d,%ecx
call   1050 <test_read@plt>
test   %eax,%eax
jne    11d1 <test_write_read+0x81>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x20,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     11cf <test_write_read+0x7f>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    %r14d,%edx
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    11d1 <test_write_read+0x81>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [52 x i8] c"Data read is not same as data written, offset = %lu\00"
define i32 @test_write_read(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca [32 x i8]
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 32)
%14 = load i32, i32* %6
%15 = load i8*, i8** %7
%16 = load i32, i32* %9
%17 = call i32 @test_write(i32 %14, i8* %15, i32 32, i32 %16)
store i32 %17, i32* %11
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i32, i32* %11
store i32 %20, i32* %5
br label %39
21: ; preds = %4
%22 = load i32, i32* %6
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%24 = load i32, i32* %9
%25 = call i32 @test_read(i32 %22, i8* %23, i32 32, i32 %24)
store i32 %25, i32* %11
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* %11
store i32 %28, i32* %5
br label %39
29: ; preds = %21
%30 = load i8*, i8** %7
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%32 = call i64 @memcmp(i8* %30, i8* %31, i32 32)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i32, i32* %9
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %36)
store i32 1, i32* %5
br label %39
38: ; preds = %29
store i32 0, i32* %5
br label %39
39: ; preds = %38, %34, %27, %19
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [52 x i8] c"Data read is not same as data written, offset = %lu\00"
define i32 @test_write_read(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 32)
%8 = call i32 @test_write(i32 %0, i8* %1, i32 32, i32 %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %4
%11 = call i32 @test_read(i32 %0, i8* %6, i32 32, i32 %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = call i64 @memcmp(i8* %1, i8* %6, i32 32)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %19
19: ; preds = %13, %10, %4, %16
%20 = phi i32 [ 1, %16 ], [ %8, %4 ], [ %11, %10 ], [ 0, %13 ]
ret i32 %20
}