#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*/ test;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
size_t ftell(int /*<<< orphan*/ *);
size_t fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int do_test(void) {
  char buffer[500] = "x";
  FILE *stream = fmemopen(buffer, sizeof(buffer), "r+");
  if (stream == NULL) {
    printf("error: fmemopen could not open stream\n");
    return 1;
  }
  const char test[] = "test";
  size_t r = fwrite(test, sizeof(char), sizeof(test), stream);
  if (r != sizeof(test)) {
    printf("error: fwrite returned %zu, expected %zu\n", r, sizeof(test));
    return 1;
  }
  r = ftell(stream);
  if (r != sizeof(test)) {
    printf("error: ftell return %zu, expected %zu\n", r, sizeof(test));
    return 1;
  }
  if (fflush(stream) != 0) {
    printf("error: fflush failed\n");
    return 1;
  }
  r = ftell(stream);
  if (r != sizeof(test)) {
    printf("error: ftell return %zu, expected %zu\n", r, sizeof(test));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
lea    -0x200(%rbp),%rdi
xor    %esi,%esi
mov    $0x1f4,%edx
call   1050 <memset@plt>
movb   $0x78,-0x200(%rbp)
lea    -0x200(%rbp),%rdi
mov    $0x1f4,%esi
lea    0xe68(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x208(%rbp)
cmpq   $0x0,-0x208(%rbp)
jne    11cc <do_test+0x6c>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <do_test+0x1a1>
mov    0xe58(%rip),%eax
mov    %eax,-0x20d(%rbp)
mov    0xe50(%rip),%al
mov    %al,-0x209(%rbp)
lea    -0x20d(%rbp),%rdi
mov    -0x208(%rbp),%rcx
mov    $0x1,%esi
mov    $0x5,%edx
call   1080 <fwrite@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x5,-0x218(%rbp)
05
je     123c <do_test+0xdc>
mov    -0x218(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x5,%edx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <do_test+0x1a1>
mov    -0x208(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x5,-0x218(%rbp)
05
je     1283 <do_test+0x123>
mov    -0x218(%rbp),%rsi
lea    0xdee(%rip),%rdi
mov    $0x5,%edx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <do_test+0x1a1>
mov    -0x208(%rbp),%rdi
call   1070 <fflush@plt>
cmp    $0x0,%rax
je     12b3 <do_test+0x153>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <do_test+0x1a1>
mov    -0x208(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x5,-0x218(%rbp)
05
je     12fa <do_test+0x19a>
mov    -0x218(%rbp),%rsi
lea    0xd77(%rip),%rdi
mov    $0x5,%edx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <do_test+0x1a1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
sub    $0x210,%rsp
lea    0x10(%rsp),%rbx
mov    $0x1f4,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
movb   $0x78,0x10(%rsp)
lea    0xe68(%rip),%rdx
mov    %rbx,%rdi
mov    $0x1f4,%esi
call   1030 <fmemopen@plt>
test   %rax,%rax
je     1200 <do_test+0x90>
mov    %rax,%rbx
movb   $0x0,0xc(%rsp)
movl   $0x74736574,0x8(%rsp)
74
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x5,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
cmp    $0x5,%rax
jne    120e <do_test+0x9e>
mov    %rbx,%rdi
call   1070 <ftell@plt>
cmp    $0x5,%rax
jne    1217 <do_test+0xa7>
mov    %rbx,%rdi
call   1080 <fflush@plt>
test   %rax,%rax
je     123b <do_test+0xcb>
lea    0xe60(%rip),%rdi
call   1040 <puts@plt>
jmp    122d <do_test+0xbd>
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
jmp    122d <do_test+0xbd>
lea    0xdf3(%rip),%rdi
jmp    121e <do_test+0xae>
lea    0xe14(%rip),%rdi
mov    $0x5,%edx
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x210,%rsp
pop    %rbx
ret
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%rsi
xor    %eax,%eax
cmp    $0x5,%rsi
je     1232 <do_test+0xc2>
lea    0xddd(%rip),%rdi
mov    $0x5,%edx
jmp    1226 <do_test+0xb6>

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@.str.1 = [39 x i8] c"error: fmemopen could not open stream\0A\00"
@__const.do_test.test = [5 x i8] c"test\00"
@.str.2 = [42 x i8] c"error: fwrite returned %zu, expected %zu\0A\00"
@.str.3 = [39 x i8] c"error: ftell return %zu, expected %zu\0A\00"
@.str.4 = [22 x i8] c"error: fflush failed\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca [500 x i8]
%3 = alloca i32*
%4 = alloca [5 x i8]
%5 = alloca i64
%6 = bitcast [500 x i8]* %2 to i8*
%7 = bitcast i8* %6 to [500 x i8]*
%8 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i32 0, i32 0
store i8 120, i8* %8
%9 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%10 = call i32* @fmemopen(i8* %9, i32 500, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %48
15: ; preds = %0
%16 = bitcast [5 x i8]* %4 to i8*
%17 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%18 = load i32*, i32** %3
%19 = call i64 @fwrite(i8* %17, i32 1, i32 5, i32* %18)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = icmp ne i64 %20, 5
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i64, i64* %5
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i64 %23, i64 5)
store i32 1, i32* %1
br label %48
25: ; preds = %15
%26 = load i32*, i32** %3
%27 = call i64 @ftell(i32* %26)
store i64 %27, i64* %5
%28 = load i64, i64* %5
%29 = icmp ne i64 %28, 5
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i64, i64* %5
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i64 %31, i64 5)
store i32 1, i32* %1
br label %48
33: ; preds = %25
%34 = load i32*, i32** %3
%35 = call i64 @fflush(i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %48
39: ; preds = %33
%40 = load i32*, i32** %3
%41 = call i64 @ftell(i32* %40)
store i64 %41, i64* %5
%42 = load i64, i64* %5
%43 = icmp ne i64 %42, 5
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i64, i64* %5
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i64 %45, i64 5)
store i32 1, i32* %1
br label %48
47: ; preds = %39
store i32 0, i32* %1
br label %48
48: ; preds = %47, %44, %37, %30, %22, %13
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@__const.do_test.test = [5 x i8] c"test\00"
@.str.2 = [42 x i8] c"error: fwrite returned %zu, expected %zu\0A\00"
@.str.3 = [39 x i8] c"error: ftell return %zu, expected %zu\0A\00"
@str = [21 x i8] c"error: fflush failed\00"
@str.5 = [38 x i8] c"error: fmemopen could not open stream\00"
define i32 @do_test() {
%1 = alloca [500 x i8]
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
store i8 120, i8* %3
%4 = call i32* @fmemopen(i8* %3, i32 500, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0))
br label %31
8: ; preds = %0
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%10 = call i64 @fwrite(i8* %9, i32 1, i32 5, i32* %4)
%11 = icmp eq i64 %10, 5
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i64 %10, i64 5)
br label %29
14: ; preds = %8
%15 = call i64 @ftell(i32* %4)
%16 = icmp eq i64 %15, 5
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i64 %15, i64 5)
br label %29
19: ; preds = %14
%20 = call i64 @fflush(i32* %4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %29
24: ; preds = %19
%25 = call i64 @ftell(i32* %4)
%26 = icmp eq i64 %25, 5
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i64 %25, i64 5)
br label %29
29: ; preds = %24, %27, %22, %17, %12
%30 = phi i32 [ 1, %12 ], [ 1, %17 ], [ 1, %22 ], [ 1, %27 ], [ 0, %24 ]
br label %31
31: ; preds = %29, %6
%32 = phi i32 [ 1, %6 ], [ %30, %29 ]
ret i32 %32
}