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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *stderr;
scalar_t__ strlen(char *);
int main(void) {
  FILE *fp;
  char buf[48];
  memset(buf, 'a', 48 - 2);
  buf[48 - 2] = '\0';
  buf[48 - 1] = 'X';
  if ((fp = fmemopen(buf, 48, "w+")) == NULL) {
    fprintf(stderr, "fmemopen failed\n");
    exit(1);
  }
  printf("initial buffer contents: %s\n", buf);
  fprintf(fp, "hello, world");
  printf("before flush: %s\n", buf);
  fflush(fp);
  printf("after fflush: %s\n", buf);
  printf("len of string in buf = %ld\n", (long)strlen(buf));
  memset(buf, 'b', 48 - 2);
  buf[48 - 2] = '\0';
  buf[48 - 1] = 'X';
  fprintf(fp, "hello, world");
  fseek(fp, 0, SEEK_SET);
  printf("after  fseek: %s\n", buf);
  printf("len of string in buf = %ld\n", (long)strlen(buf));
  memset(buf, 'c', 48 - 2);
  buf[48 - 2] = '\0';
  buf[48 - 1] = 'X';
  fprintf(fp, "hello, world");
  fclose(fp);
  printf("after fclose: %s\n", buf);
  printf("len of string in buf = %ld\n", (long)strlen(buf));
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x61,%esi
mov    $0x2e,%edx
call   1070 <memset@plt>
movb   $0x0,-0x12(%rbp)
movb   $0x58,-0x11(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x30,%esi
lea    0xe36(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11fd <main+0x6d>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rdi
lea    0xe15(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x40(%rbp),%rsi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x40(%rbp),%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1090 <fflush@plt>
lea    -0x40(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    $0x62,%esi
mov    $0x2e,%edx
call   1070 <memset@plt>
movb   $0x0,-0x12(%rbp)
movb   $0x58,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xda5(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    -0x40(%rbp),%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    $0x63,%esi
mov    $0x2e,%edx
call   1070 <memset@plt>
movb   $0x0,-0x12(%rbp)
movb   $0x58,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd3a(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x40(%rbp),%rsi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rsi
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x61,%esi
mov    $0x2e,%edx
call   1070 <memset@plt>
movw   $0x5800,0x2e(%rsp)
lea    0xe42(%rip),%rdx
mov    %rbx,%rdi
mov    $0x30,%esi
call   1030 <fmemopen@plt>
test   %rax,%rax
je     131e <main+0x18e>
mov    %rax,%rbx
lea    0xe36(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3f(%rip),%r15
mov    $0xc,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
lea    0xe30(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <fflush@plt>
lea    0xe29(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0xe22(%rip),%r12
mov    %r12,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x62,%esi
mov    $0x2e,%edx
call   1070 <memset@plt>
movw   $0x5800,0x2e(%rsp)
mov    $0xc,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    0xde9(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0x63,%esi
mov    $0x2e,%edx
call   1070 <memset@plt>
movw   $0x5800,0x2e(%rsp)
mov    $0xc,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xd9f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2cbb(%rip),%rax
mov    (%rax),%rcx
lea    0xcd4(%rip),%rdi
mov    $0x10,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@stderr = global i32* null
@.str.1 = [17 x i8] c"fmemopen failed\0A\00"
@.str.2 = [29 x i8] c"initial buffer contents: %s\0A\00"
@.str.3 = [13 x i8] c"hello, world\00"
@.str.4 = [18 x i8] c"before flush: %s\0A\00"
@.str.5 = [18 x i8] c"after fflush: %s\0A\00"
@.str.6 = [28 x i8] c"len of string in buf = %ld\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [18 x i8] c"after fseek: %s\0A\00"
@.str.8 = [18 x i8] c"after fclose: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [48 x i8]
store i32 0, i32* %1
%4 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i8 signext 97, i32 46)
%6 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 46
store i8 0, i8* %6
%7 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 47
store i8 88, i8* %7
%8 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%9 = call i32* @fmemopen(i8* %8, i32 48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32*, i32** @stderr
%13 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i32*, i32** %2
%19 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%20 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%22 = load i32*, i32** %2
%23 = call i32 @fflush(i32* %22)
%24 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%27 = call i64 @strlen(i8* %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %27)
%29 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i8 signext 98, i32 46)
%31 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 46
store i8 0, i8* %31
%32 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 47
store i8 88, i8* %32
%33 = load i32*, i32** %2
%34 = call i32 @fprintf(i32* %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32*, i32** %2
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %35, i32 0, i32 %36)
%38 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %38)
%40 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%41 = call i64 @strlen(i8* %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %41)
%43 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i8 signext 99, i32 46)
%45 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 46
store i8 0, i8* %45
%46 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 47
store i8 88, i8* %46
%47 = load i32*, i32** %2
%48 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32*, i32** %2
%50 = call i32 @fclose(i32* %49)
%51 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %51)
%53 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%54 = call i64 @strlen(i8* %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %54)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@stderr = global i32* null
@.str.1 = [17 x i8] c"fmemopen failed\0A\00"
@.str.2 = [29 x i8] c"initial buffer contents: %s\0A\00"
@.str.3 = [13 x i8] c"hello, world\00"
@.str.4 = [18 x i8] c"before flush: %s\0A\00"
@.str.5 = [18 x i8] c"after fflush: %s\0A\00"
@.str.6 = [28 x i8] c"len of string in buf = %ld\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [18 x i8] c"after fseek: %s\0A\00"
@.str.8 = [18 x i8] c"after fclose: %s\0A\00"
define i32 @main() {
%1 = alloca [48 x i8]
%2 = getelementptr inbounds [48 x i8], [48 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i8 signext 97, i32 46)
%4 = getelementptr inbounds [48 x i8], [48 x i8]* %1, i64 0, i64 46
store i8 0, i8* %4
%5 = getelementptr inbounds [48 x i8], [48 x i8]* %1, i64 0, i64 47
store i8 88, i8* %5
%6 = call i32* @fmemopen(i8* %2, i32 48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32*, i32** @stderr
%10 = call i64 @fwrite(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 16, i64 1, i32* %9)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%14 = call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 12, i64 1, i32* %6)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%16 = call i32 @fflush(i32* %6)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%18 = call i64 @strlen(i8* %2)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %18)
%20 = call i32 @memset(i8* %2, i8 signext 98, i32 46)
store i8 0, i8* %4
store i8 88, i8* %5
%21 = call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 12, i64 1, i32* %6)
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %6, i32 0, i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%25 = call i64 @strlen(i8* %2)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %25)
%27 = call i32 @memset(i8* %2, i8 signext 99, i32 46)
store i8 0, i8* %4
store i8 88, i8* %5
%28 = call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 12, i64 1, i32* %6)
%29 = call i32 @fclose(i32* %6)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %2)
%31 = call i64 @strlen(i8* %2)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %31)
ret i32 0
}