#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *strlen(char *);
int main() {
  char *str = NULL;
  size_t len = 64;
  FILE *f = open_memstream(&str, &len);
  if (f == NULL) {
    perror("open_memstream");
    return errno;
  }
  char *buf = "Open a stream that writes into a malloc'd buffer that is "
              "expanded as necessary.";
  fwrite(buf, 1, strlen(buf) + 1, f);
  fflush(f);
  printf("%s\n", str);
  printf("%lu\n", strlen(str));
  fseek(f, 5, SEEK_SET);
  free(str);
  fclose(f);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x40,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <main+0x58>
lea    0xe2e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <main+0xe7>
lea    0xe20(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
add    $0x1,%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x10(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1070 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rsi
lea    0xe1a(%rip),%rdi
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x40,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <open_memstream@plt>
test   %rax,%rax
je     124b <main+0xbb>
mov    %rax,%rbx
lea    0xe44(%rip),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1080 <fflush@plt>
mov    0x8(%rsp),%rsi
lea    0xe64(%rip),%rdi
call   1070 <printf@plt>
mov    0x8(%rsp),%rdi
call   1060 <strlen@plt>
lea    0xe52(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1090 <fseek@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdae(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
jmp    1243 <main+0xb3>

LLVM IR (-O0):
@.str = [15 x i8] c"open_memstream\00"
@errno = global i32 0
@.str.1 = [80 x i8] c"Open a stream that writes into a malloc'd buffer that is expanded as necessary.\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [5 x i8] c"%lu\0A\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %2
store i64 64, i64* %3
%6 = call i32* @open_memstream(i8** %2, i64* %3)
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
store i32 %11, i32* %1
br label %33
12: ; preds = %0
store i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i8** %5
%13 = load i8*, i8** %5
%14 = load i8*, i8** %5
%15 = call i8* @strlen(i8* %14)
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i32*, i32** %4
%18 = call i32 @fwrite(i8* %13, i32 1, i8* %16, i32* %17)
%19 = load i32*, i32** %4
%20 = call i32 @fflush(i32* %19)
%21 = load i8*, i8** %2
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %2
%24 = call i8* @strlen(i8* %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = load i32*, i32** %4
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %26, i32 5, i32 %27)
%29 = load i8*, i8** %2
%30 = call i32 @free(i8* %29)
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
br label %33
33: ; preds = %12, %9
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [15 x i8] c"open_memstream\00"
@errno = global i32 0
@.str.1 = [80 x i8] c"Open a stream that writes into a malloc'd buffer that is expanded as necessary.\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [5 x i8] c"%lu\0A\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = bitcast i64* %2 to i8*
store i64 64, i64* %2
%5 = call i32* @open_memstream(i8** %1, i64* %2)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @errno
br label %25
10: ; preds = %0
%11 = call i8* @strlen(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = call i32 @fwrite(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i32 1, i8* %12, i32* %5)
%14 = call i32 @fflush(i32* %5)
%15 = load i8*, i8** %1
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %1
%18 = call i8* @strlen(i8* %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %5, i32 5, i32 %20)
%22 = load i8*, i8** %1
%23 = call i32 @free(i8* %22)
%24 = call i32 @fclose(i32* %5)
br label %25
25: ; preds = %10, %7
%26 = phi i32 [ %9, %7 ], [ 0, %10 ]
ret i32 %26
}