#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 */
struct otrap {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
scalar_t__ memcmp(char const *, char *, size_t);
int /*<<< orphan*/ otrap_deinit(struct otrap *);
int /*<<< orphan*/ *otrap_file(struct otrap *);
size_t otrap_read(struct otrap *, char *, size_t);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
void read(struct otrap *trap, const char *output) {
  static int call = 0;
  ++call;
  FILE *f = otrap_file(trap);
  size_t output_size = strlen(output);
  fputs(output, f);
  char trapped[output_size];
  size_t read_size = otrap_read(trap, trapped, output_size);
  if (read_size != output_size || memcmp(output, trapped, read_size) != 0) {
    fprintf(stderr, "Output read back is different on #%d call.\n", call);
    otrap_deinit(trap);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ece(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ec5(%rip)
mov    -0x8(%rbp),%rdi
call   1040 <otrap_file@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x20(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x40(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
call   1070 <otrap_read@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1227 <read+0xa7>
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rdx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
je     1255 <read+0xd5>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%edx
lea    0xdc3(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <otrap_deinit@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
addl   $0x1,0x2ecc(%rip)
call   1040 <otrap_file@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <otrap_read@plt>
cmp    %r12,%rax
jne    11fb <read+0x7b>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    11fb <read+0x7b>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%edx
lea    0xdef(%rip),%rsi
call   1090 <fprintf@plt>
mov    %r14,%rdi
call   1030 <otrap_deinit@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.otrap = type { i32 }
@read.call = internal global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"Output read back is different on #%d call.\0A\00"
define void @read(%struct.otrap* %0, i8* %1) {
%3 = alloca %struct.otrap*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store %struct.otrap* %0, %struct.otrap** %3
store i8* %1, i8** %4
%10 = load i32, i32* @read.call
%11 = add nsw i32 %10, 1
store i32 %11, i32* @read.call
%12 = load %struct.otrap*, %struct.otrap** %3
%13 = call i32* @otrap_file(%struct.otrap* %12)
store i32* %13, i32** %5
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %6
%16 = load i8*, i8** %4
%17 = load i32*, i32** %5
%18 = call i32 @fputs(i8* %16, i32* %17)
%19 = load i64, i64* %6
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %7
%21 = alloca i8, i64 %19
store i64 %19, i64* %8
%22 = load %struct.otrap*, %struct.otrap** %3
%23 = load i64, i64* %6
%24 = call i64 @otrap_read(%struct.otrap* %22, i8* %21, i64 %23)
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = load i64, i64* %6
%27 = icmp ne i64 %25, %26
br i1 %27, label %33, label %28
28: ; preds = %2
%29 = load i8*, i8** %4
%30 = load i64, i64* %9
%31 = call i64 @memcmp(i8* %29, i8* %21, i64 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %28, %2
%34 = load i32, i32* @stderr
%35 = load i32, i32* @read.call
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load %struct.otrap*, %struct.otrap** %3
%38 = call i32 @otrap_deinit(%struct.otrap* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %28
%41 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
%struct.otrap = type { i32 }
@read.call = internal unnamed_addr global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"Output read back is different on #%d call.\0A\00"
define void @read(%struct.otrap* %0, i8* %1) {
%3 = load i32, i32* @read.call
%4 = add nsw i32 %3, 1
store i32 %4, i32* @read.call
%5 = tail call i32* @otrap_file(%struct.otrap* %0)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = tail call i32 @fputs(i8* %1, i32* %5)
%8 = alloca i8, i64 %6
%9 = call i64 @otrap_read(%struct.otrap* %0, i8* %8, i64 %6)
%10 = icmp eq i64 %9, %6
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i64 @memcmp(i8* %1, i8* %8, i64 %6)
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %11, %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @read.call
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @otrap_deinit(%struct.otrap* %0)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %11
ret void
}