#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*/ str2;
typedef int /*<<< orphan*/ str;
typedef int /*<<< orphan*/ buf2;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ bzero(char *, int);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
void test_preexisting() {
  char buf[512];
  char buf2[512];
  char str[] = "Test writing some stuff";
  char str2[] = "AAAAAAAAA";
  char str3[] = "AAAA writing some stuff";
  FILE *fp;
  size_t nofw, nofr;
  int rc;
  fp = fmemopen(buf, sizeof(buf), "w");
  assert(fp != NULL);
  nofw = fwrite(str, 1, sizeof(str), fp);
  assert(nofw == sizeof(str));
  rc = fclose(fp);
  assert(rc == 0);
  fp = fmemopen(buf, sizeof(buf), "r");
  assert(fp != NULL);
  bzero(buf2, sizeof(buf2));
  nofr = fread(buf2, 1, sizeof(buf2), fp);
  assert(nofr == sizeof(buf2));
  assert(strcmp(str, buf2) == 0);
  rc = fclose(fp);
  assert(rc == 0);
  fp = fmemopen(str, 4, "w");
  assert(fp != NULL);
  nofw = fwrite(str2, 1, sizeof(str2), fp);
  assert(nofw == 4);
  rc = fclose(fp);
  assert(strcmp(str, str3) == 0);
}


x86 assembly (clang, -O0):
<test_preexisting>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    0xe7e(%rip),%rax
mov    %rax,-0x420(%rbp)
mov    0xe78(%rip),%rax
mov    %rax,-0x418(%rbp)
mov    0xe72(%rip),%rax
mov    %rax,-0x410(%rbp)
mov    0xe8c(%rip),%rax
mov    %rax,-0x42a(%rbp)
mov    0xe86(%rip),%ax
mov    %ax,-0x422(%rbp)
mov    0xe58(%rip),%rax
mov    %rax,-0x450(%rbp)
mov    0xe52(%rip),%rax
mov    %rax,-0x448(%rbp)
mov    0xe4c(%rip),%rax
mov    %rax,-0x440(%rbp)
lea    -0x200(%rbp),%rdi
mov    $0x200,%esi
lea    0xe44(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x458(%rbp)
cmpq   $0x0,-0x458(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x420(%rbp),%rdi
mov    -0x458(%rbp),%rcx
mov    $0x1,%esi
mov    $0x18,%edx
call   1090 <fwrite@plt>
mov    %rax,-0x460(%rbp)
cmpq   $0x18,-0x460(%rbp)
18
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x458(%rbp),%rdi
call   1050 <fclose@plt>
mov    %eax,-0x46c(%rbp)
cmpl   $0x0,-0x46c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x200(%rbp),%rdi
mov    $0x200,%esi
lea    0xdb3(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x458(%rbp)
cmpq   $0x0,-0x458(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x400(%rbp),%rdi
mov    $0x200,%esi
call   1080 <bzero@plt>
lea    -0x400(%rbp),%rdi
mov    -0x458(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1040 <fread@plt>
mov    %rax,-0x468(%rbp)
cmpq   $0x200,-0x468(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x420(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x458(%rbp),%rdi
call   1050 <fclose@plt>
mov    %eax,-0x46c(%rbp)
cmpl   $0x0,-0x46c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x420(%rbp),%rdi
mov    $0x4,%esi
lea    0xce6(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x458(%rbp)
cmpq   $0x0,-0x458(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x42a(%rbp),%rdi
mov    -0x458(%rbp),%rcx
mov    $0x1,%esi
mov    $0xa,%edx
call   1090 <fwrite@plt>
mov    %rax,-0x460(%rbp)
cmpq   $0x4,-0x460(%rbp)
04
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x458(%rbp),%rdi
call   1050 <fclose@plt>
mov    %eax,-0x46c(%rbp)
lea    -0x420(%rbp),%rdi
lea    -0x450(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_preexisting>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x458,%rsp
movaps 0xe7b(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movabs $0x66667574732065,%rax
mov    %rax,0x40(%rsp)
movabs $0x4141414141414141,%rcx
41 41 41
mov    %rcx,(%rsp)
movw   $0x41,0x8(%rsp)
movaps 0xe6b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rax,0x20(%rsp)
lea    0xe7c(%rip),%r14
lea    0x250(%rsp),%r12
mov    %r12,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1030 <fmemopen@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x18,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
xor    %edi,%edi
cmp    $0x18,%rax
sete   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1070 <assert@plt>
lea    0xe10(%rip),%rdx
mov    %r12,%rdi
mov    $0x200,%esi
call   1030 <fmemopen@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
lea    0x50(%rsp),%r12
mov    %r12,%rdi
mov    $0x200,%esi
call   1080 <bzero@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
xor    %edi,%edi
cmp    $0x200,%rax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <fmemopen@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0xa,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
xor    %edi,%edi
cmp    $0x4,%rax
sete   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
add    $0x458,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.test_preexisting.str = [24 x i8] c"Test writing some stuff\00"
@__const.test_preexisting.str2 = [10 x i8] c"AAAAAAAAA\00"
@__const.test_preexisting.str3 = [24 x i8] c"AAAA writing some stuff\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
define void @test_preexisting() {
%1 = alloca [512 x i8]
%2 = alloca [512 x i8]
%3 = alloca [24 x i8]
%4 = alloca [10 x i8]
%5 = alloca [24 x i8]
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = bitcast [24 x i8]* %3 to i8*
%11 = bitcast [10 x i8]* %4 to i8*
%12 = bitcast [24 x i8]* %5 to i8*
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%14 = call i32* @fmemopen(i8* %13, i32 512, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp ne i32* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i64 @fwrite(i8* %19, i32 1, i32 24, i32* %20)
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = icmp eq i64 %22, 24
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%33 = call i32* @fmemopen(i8* %32, i32 512, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %33, i32** %6
%34 = load i32*, i32** %6
%35 = icmp ne i32* %34, null
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%39 = call i32 @bzero(i8* %38, i32 512)
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%41 = load i32*, i32** %6
%42 = call i64 @fread(i8* %40, i32 1, i32 512, i32* %41)
store i64 %42, i64* %8
%43 = load i64, i64* %8
%44 = icmp eq i64 %43, 512
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%49 = call i64 @strcmp(i8* %47, i8* %48)
%50 = icmp eq i64 %49, 0
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp eq i32 %55, 0
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
%59 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%60 = call i32* @fmemopen(i8* %59, i32 4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %60, i32** %6
%61 = load i32*, i32** %6
%62 = icmp ne i32* %61, null
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
%65 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%66 = load i32*, i32** %6
%67 = call i64 @fwrite(i8* %65, i32 1, i32 10, i32* %66)
store i64 %67, i64* %7
%68 = load i64, i64* %7
%69 = icmp eq i64 %68, 4
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
%72 = load i32*, i32** %6
%73 = call i32 @fclose(i32* %72)
store i32 %73, i32* %9
%74 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%75 = getelementptr inbounds [24 x i8], [24 x i8]* %5, i64 0, i64 0
%76 = call i64 @strcmp(i8* %74, i8* %75)
%77 = icmp eq i64 %76, 0
%78 = zext i1 %77 to i32
%79 = call i32 @assert(i32 %78)
ret void
}

LLVM IR (-O2):
@__const.test_preexisting.str = [24 x i8] c"Test writing some stuff\00"
@__const.test_preexisting.str2 = [10 x i8] c"AAAAAAAAA\00"
@__const.test_preexisting.str3 = [24 x i8] c"AAAA writing some stuff\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
define void @test_preexisting() {
%1 = alloca [512 x i8]
%2 = alloca [512 x i8]
%3 = alloca [24 x i8]
%4 = alloca [10 x i8]
%5 = alloca [24 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [24 x i8], [24 x i8]* %5, i64 0, i64 0
%11 = call i32* @fmemopen(i8* %6, i32 512, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i32* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i64 @fwrite(i8* %8, i32 1, i32 24, i32* %11)
%16 = icmp eq i64 %15, 24
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i32 @fclose(i32* %11)
%20 = icmp eq i32 %19, 0
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = call i32* @fmemopen(i8* %6, i32 512, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i32* %23, null
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = call i32 @bzero(i8* %7, i32 512)
%28 = call i64 @fread(i8* %7, i32 1, i32 512, i32* %23)
%29 = icmp eq i64 %28, 512
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = call i64 @strcmp(i8* %8, i8* %7)
%33 = icmp eq i64 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = call i32 @fclose(i32* %23)
%37 = icmp eq i32 %36, 0
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = call i32* @fmemopen(i8* %8, i32 4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = icmp ne i32* %40, null
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = call i64 @fwrite(i8* %9, i32 1, i32 10, i32* %40)
%45 = icmp eq i64 %44, 4
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = call i32 @fclose(i32* %40)
%49 = call i64 @strcmp(i8* %8, i8* %10)
%50 = icmp eq i64 %49, 0
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
ret void
}