#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, size_t);
int main(void) {
  FILE *stream;
  char *buf;
  size_t len;
  stream = open_memstream(&buf, &len);
  if (stream == NULL)
    perror("open_memstream");
  else {
    fprintf(stream, "hello my world");
    fflush(stream);
    printf("buf=%s, len=%zu\n", buf, len);
    fseek(stream, 0, SEEK_SET);
    fprintf(stream, "good-bye cruel world");
    fclose(stream);
    printf("buf=%s, len=%zu\n", buf, len);
    free(buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bc <main+0x3c>
lea    0xe4e(%rip),%rdi
call   10a0 <perror@plt>
jmp    1233 <main+0xb3>
mov    -0x10(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe3a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
lea    0xe27(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdf9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <open_memstream@plt>
test   %rax,%rax
je     1236 <main+0xb6>
mov    %rax,%rbx
lea    0xe66(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
lea    0xe4a(%rip),%r14
mov    %r14,%rdi
call   1060 <printf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0xe39(%rip),%rdi
mov    $0x14,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    %r14,%rdi
call   1060 <printf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdc3(%rip),%rdi
call   1090 <perror@plt>
jmp    122c <main+0xac>

LLVM IR (-O0):
@.str = [15 x i8] c"open_memstream\00"
@.str.1 = [15 x i8] c"hello my world\00"
@.str.2 = [17 x i8] c"buf=%s, len=%zu\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [21 x i8] c"good-bye cruel world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
%5 = call i32* @open_memstream(i8** %3, i64* %4)
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %30
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32*, i32** %2
%14 = call i32 @fflush(i32* %13)
%15 = load i8*, i8** %3
%16 = load i64, i64* %4
%17 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %15, i64 %16)
%18 = load i32*, i32** %2
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %2
%22 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
%25 = load i8*, i8** %3
%26 = load i64, i64* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %25, i64 %26)
%28 = load i8*, i8** %3
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %10, %8
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"open_memstream\00"
@.str.1 = [15 x i8] c"hello my world\00"
@.str.2 = [17 x i8] c"buf=%s, len=%zu\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [21 x i8] c"good-bye cruel 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 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %0
%10 = call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 14, i64 1, i32* %5)
%11 = call i32 @fflush(i32* %5)
%12 = load i8*, i8** %1
%13 = load i64, i64* %2
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %12, i64 %13)
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %5, i32 0, i32 %15)
%17 = call i64 @fwrite(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i64 20, i64 1, i32* %5)
%18 = call i32 @fclose(i32* %5)
%19 = load i8*, i8** %1
%20 = load i64, i64* %2
%21 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %19, i64 %20)
%22 = load i8*, i8** %1
%23 = call i32 @free(i8* %22)
br label %24
24: ; preds = %9, %7
ret i32 0
}