#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*/ free(char *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ printf(char *, char *, size_t);
int /*<<< orphan*/ puts(char *);
int strncmp(char *, char *, int);
int main(void) {
  char *bp;
  size_t size;
  FILE *stream;
  int lose = 0;
  stream = open_memstream(&bp, &size);
  fprintf(stream, "hello");
  fflush(stream);
  printf("buf = %s, size = %Zu\n", bp, size);
  lose |= size != 5;
  lose |= strncmp(bp, "hello", size);
  fprintf(stream, ", world");
  fclose(stream);
  printf("buf = %s, size = %Zu\n", bp, size);
  lose |= size != 12;
  lose |= strncmp(bp, "hello, world", 12);
  puts(lose ? "Test FAILED!" : "Test succeeded.");
  free(bp);
  return lose;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <fflush@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe37(%rip),%rdi
call   1080 <printf@plt>
cmpq   $0x5,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe08(%rip),%rsi
call   1050 <strncmp@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xddb(%rip),%rdi
call   1080 <printf@plt>
cmpq   $0xc,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdce(%rip),%rsi
mov    $0xc,%edx
call   1050 <strncmp@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
lea    0xdce(%rip),%rdi
lea    0xdba(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdi
call   1060 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,%r12
lea    0xe5d(%rip),%r15
mov    $0x5,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %r12,%rdi
call   1090 <fflush@plt>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
lea    0xe36(%rip),%r14
mov    %r14,%rdi
call   1080 <printf@plt>
mov    0x8(%rsp),%rdx
xor    %ebx,%ebx
cmp    $0x5,%rdx
setne  %bl
mov    (%rsp),%rdi
mov    %r15,%rsi
call   1050 <strncmp@plt>
mov    %eax,%r15d
or     %ebx,%r15d
lea    0xe22(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
mov    %r12,%rdi
call   1070 <fclose@plt>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %r14,%rdi
call   1080 <printf@plt>
xor    %ebp,%ebp
cmpq   $0xc,0x8(%rsp)
setne  %bpl
or     %r15d,%ebp
mov    (%rsp),%rdi
lea    0xdd8(%rip),%rsi
mov    $0xc,%edx
call   1050 <strncmp@plt>
mov    %eax,%ebx
or     %ebp,%ebx
lea    0xddd(%rip),%rax
lea    0xdc9(%rip),%rdi
cmove  %rax,%rdi
call   1060 <puts@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [22 x i8] c"buf = %s, size = %Zu\0A\00"
@.str.2 = [8 x i8] c", world\00"
@.str.3 = [13 x i8] c"hello, world\00"
@.str.4 = [13 x i8] c"Test FAILED!\00"
@.str.5 = [16 x i8] c"Test succeeded.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%6 = call i32* @open_memstream(i8** %2, i64* %3)
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = load i32*, i32** %4
%10 = call i32 @fflush(i32* %9)
%11 = load i8*, i8** %2
%12 = load i64, i64* %3
%13 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %11, i64 %12)
%14 = load i64, i64* %3
%15 = icmp ne i64 %14, 5
%16 = zext i1 %15 to i32
%17 = load i32, i32* %5
%18 = or i32 %17, %16
store i32 %18, i32* %5
%19 = load i8*, i8** %2
%20 = load i64, i64* %3
%21 = trunc i64 %20 to i32
%22 = call i32 @strncmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %5
%24 = or i32 %23, %22
store i32 %24, i32* %5
%25 = load i32*, i32** %4
%26 = call i32 @fprintf(i32* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32*, i32** %4
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %2
%30 = load i64, i64* %3
%31 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %29, i64 %30)
%32 = load i64, i64* %3
%33 = icmp ne i64 %32, 12
%34 = zext i1 %33 to i32
%35 = load i32, i32* %5
%36 = or i32 %35, %34
store i32 %36, i32* %5
%37 = load i8*, i8** %2
%38 = call i32 @strncmp(i8* %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 12)
%39 = load i32, i32* %5
%40 = or i32 %39, %38
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp ne i32 %41, 0
%43 = zext i1 %42 to i64
%44 = select i1 %42, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0)
%45 = call i32 @puts(i8* %44)
%46 = load i8*, i8** %2
%47 = call i32 @free(i8* %46)
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [22 x i8] c"buf = %s, size = %Zu\0A\00"
@.str.2 = [8 x i8] c", world\00"
@.str.3 = [13 x i8] c"hello, world\00"
@.str.4 = [13 x i8] c"Test FAILED!\00"
@.str.5 = [16 x i8] c"Test succeeded.\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 ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %8, i64 %9)
%11 = load i64, i64* %2
%12 = icmp ne i64 %11, 5
%13 = zext i1 %12 to i32
%14 = load i8*, i8** %1
%15 = trunc i64 %11 to i32
%16 = call i32 @strncmp(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = or i32 %16, %13
%18 = call i64 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 7, i64 1, i32* %5)
%19 = call i32 @fclose(i32* %5)
%20 = load i8*, i8** %1
%21 = load i64, i64* %2
%22 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %20, i64 %21)
%23 = load i64, i64* %2
%24 = icmp ne i64 %23, 12
%25 = zext i1 %24 to i32
%26 = or i32 %17, %25
%27 = load i8*, i8** %1
%28 = call i32 @strncmp(i8* %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 12)
%29 = or i32 %26, %28
%30 = icmp eq i32 %29, 0
%31 = select i1 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0)
%32 = call i32 @puts(i8* dereferenceable(1) %31)
%33 = load i8*, i8** %1
%34 = call i32 @free(i8* %33)
ret i32 %29
}