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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ bzero(char *, int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  char buf[64];
  bzero(buf, sizeof(buf));
  FILE *f = fmemopen(buf, sizeof(buf), "r+");
  if (f == NULL) {
    perror("fmemopen");
    return errno;
  }
  printf("=> %s\n", buf);
  fwrite("hello", 1, 6, f);
  fflush(f);
  printf("=> %s\n", buf);
  char content[10];
  bzero(content, sizeof(content));
  fseek(f, 2, SEEK_SET);
  fread(content, 1, sizeof(content) - 1, f);
  printf("=> %s\n", content);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x40,%esi
call   10a0 <bzero@plt>
lea    -0x50(%rbp),%rdi
mov    $0x40,%esi
lea    0xe43(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11ee <main+0x5e>
lea    0xe2b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <main+0xfd>
lea    -0x50(%rbp),%rsi
lea    0xe13(%rip),%rdi
call   1060 <printf@plt>
mov    -0x58(%rbp),%rcx
lea    0xe0a(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   10b0 <fwrite@plt>
mov    -0x58(%rbp),%rdi
call   1070 <fflush@plt>
lea    -0x50(%rbp),%rsi
lea    0xde0(%rip),%rdi
call   1060 <printf@plt>
lea    -0x62(%rbp),%rdi
mov    $0xa,%esi
call   10a0 <bzero@plt>
mov    -0x58(%rbp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
mov    $0x2,%esi
call   1080 <fseek@plt>
lea    -0x62(%rbp),%rdi
mov    -0x58(%rbp),%rcx
mov    $0x1,%esi
mov    $0x9,%edx
call   1040 <fread@plt>
lea    -0x62(%rbp),%rsi
lea    0xd94(%rip),%rdi
call   1060 <printf@plt>
mov    -0x58(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   10a0 <bzero@plt>
lea    0xe4e(%rip),%rdx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <fmemopen@plt>
test   %rax,%rax
je     126a <main+0xda>
mov    %rax,%rbx
lea    0xe3a(%rip),%r14
lea    0x10(%rsp),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
lea    0xe2a(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
lea    0x6(%rsp),%r15
mov    %r15,%rdi
mov    $0xa,%esi
call   10a0 <bzero@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x9,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd92(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
jmp    1260 <main+0xd0>

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@.str.1 = [9 x i8] c"fmemopen\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"=> %s\0A\00"
@.str.3 = [6 x i8] c"hello\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca i32*
%4 = alloca [10 x i8]
store i32 0, i32* %1
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = call i32 @bzero(i8* %5, i32 64)
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%8 = call i32* @fmemopen(i8* %7, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @errno
store i32 %13, i32* %1
br label %35
14: ; preds = %0
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%16 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i32*, i32** %3
%18 = call i32 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 6, i32* %17)
%19 = load i32*, i32** %3
%20 = call i32 @fflush(i32* %19)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%22 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%24 = call i32 @bzero(i8* %23, i32 10)
%25 = load i32*, i32** %3
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 2, i32 %26)
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%29 = load i32*, i32** %3
%30 = call i32 @fread(i8* %28, i32 1, i32 9, i32* %29)
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %3
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %1
br label %35
35: ; preds = %14, %11
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@.str.1 = [9 x i8] c"fmemopen\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"=> %s\0A\00"
@.str.3 = [6 x i8] c"hello\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca [64 x i8]
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%4 = call i32 @bzero(i8* %3, i32 64)
%5 = call i32* @fmemopen(i8* %3, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @errno
br label %22
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%12 = call i32 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 6, i32* %5)
%13 = call i32 @fflush(i32* %5)
%14 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = call i32 @bzero(i8* %15, i32 10)
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @fseek(i32* %5, i32 2, i32 %17)
%19 = call i32 @fread(i8* %15, i32 1, i32 9, i32* %5)
%20 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%21 = call i32 @fclose(i32* %5)
br label %22
22: ; preds = %10, %7
%23 = phi i32 [ %9, %7 ], [ 0, %10 ]
ret i32 %23
}