#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ printf(char *, char *, size_t);
int main(void) {
  char *bp;
  size_t size;
  FILE *stream;
  stream = open_memstream(&bp, &size);
  fprintf(stream, "hello");
  fflush(stream);
  printf("buf = `%s', size = %zu\n", bp, size);
  fprintf(stream, ", world");
  fclose(stream);
  printf("buf = `%s', size = %zu\n", bp, size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <open_memstream@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe6e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe41(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <open_memstream@plt>
mov    %rax,%rbx
lea    0xe90(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
lea    0xe6b(%rip),%r14
mov    %r14,%rdi
call   1050 <printf@plt>
lea    0xe74(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %r14,%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [24 x i8] c"buf = `%s', size = %zu\0A\00"
@.str.2 = [8 x i8] c", world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32* @open_memstream(i8** %2, i64* %3)
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = load i32*, i32** %4
%9 = call i32 @fflush(i32* %8)
%10 = load i8*, i8** %2
%11 = load i64, i64* %3
%12 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %10, i64 %11)
%13 = load i32*, i32** %4
%14 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32*, i32** %4
%16 = call i32 @fclose(i32* %15)
%17 = load i8*, i8** %2
%18 = load i64, i64* %3
%19 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %17, i64 %18)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [24 x i8] c"buf = `%s', size = %zu\0A\00"
@.str.2 = [8 x i8] c", world\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = bitcast i8** %1 to i8*
%4 = bitcast i64* %2 to i8*
%5 = call i32* @open_memstream(i8** %1, i64* %2)
%6 = call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 5, i64 1, i32* %5)
%7 = call i32 @fflush(i32* %5)
%8 = load i8*, i8** %1
%9 = load i64, i64* %2
%10 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %8, i64 %9)
%11 = call i64 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 7, i64 1, i32* %5)
%12 = call i32 @fclose(i32* %5)
%13 = load i8*, i8** %1
%14 = load i64, i64* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %13, i64 %14)
ret i32 0
}