#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 EINVAL;
int ENOMEM;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
int /*<<< orphan*/ printf(char *, int, ...);
int do_bz18820(void) {
  char ch;
  FILE *stream;
  errno = 0;
  stream = fmemopen(&ch, 1, "?");
  if (stream) {
    printf("fmemopen: expected NULL, got %p\n", stream);
    fclose(stream);
    return 1;
  }
  if (errno != EINVAL) {
    printf("fmemopen: got %i, expected EINVAL (%i)\n", errno, EINVAL);
    return 10;
  }
  stream = fmemopen(NULL, 42, "?");
  if (stream) {
    printf("fmemopen: expected NULL, got %p\n", stream);
    fclose(stream);
    return 2;
  }
  errno = 0;
  stream = fmemopen(NULL, ~0, "w");
  if (stream) {
    printf("fmemopen: expected NULL, got %p\n", stream);
    fclose(stream);
    return 3;
  }
  if (errno != ENOMEM) {
    printf("fmemopen: got %i, expected ENOMEM (%i)\n", errno, ENOMEM);
    return 20;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_bz18820>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
movl   $0x0,(%rax)
lea    -0x5(%rbp),%rdi
mov    $0x1,%esi
lea    0xeab(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1192 <do_bz18820+0x62>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <do_bz18820+0x198>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%eax
je     11d6 <do_bz18820+0xa6>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xa,-0x4(%rbp)
jmp    12c8 <do_bz18820+0x198>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2a,%esi
lea    0xe1a(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1223 <do_bz18820+0xf3>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x2,-0x4(%rbp)
jmp    12c8 <do_bz18820+0x198>
mov    0x2dae(%rip),%rax
movl   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%esi
lea    0xe0b(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     127d <do_bz18820+0x14d>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x3,-0x4(%rbp)
jmp    12c8 <do_bz18820+0x198>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%eax
je     12c1 <do_bz18820+0x191>
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x14,-0x4(%rbp)
jmp    12c8 <do_bz18820+0x198>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_bz18820>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e98(%rip),%r15
movl   $0x0,(%r15)
lea    0xeb2(%rip),%rdx
lea    0xf(%rsp),%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
call   1030 <fmemopen@plt>
test   %rax,%rax
je     1185 <do_bz18820+0x55>
mov    %rax,%rbx
lea    0xe90(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11e4 <do_bz18820+0xb4>
mov    (%r15),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
cmp    %edx,%esi
jne    11d0 <do_bz18820+0xa0>
lea    0xe64(%rip),%rdx
xor    %edi,%edi
mov    $0x2a,%esi
call   1030 <fmemopen@plt>
test   %rax,%rax
je     11f1 <do_bz18820+0xc1>
mov    %rax,%rbx
lea    0xe4b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x2,%r14d
jmp    11e4 <do_bz18820+0xb4>
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
movl   $0x0,(%r15)
lea    0xe4c(%rip),%rdx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0xffffffff,%esi
call   1030 <fmemopen@plt>
test   %rax,%rax
je     1236 <do_bz18820+0x106>
mov    %rax,%rbx
lea    0xde5(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x3,%r14d
jmp    11e4 <do_bz18820+0xb4>
mov    (%r15),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
cmp    %edx,%esi
je     11e4 <do_bz18820+0xb4>
lea    0xe00(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x14,%r14d
jmp    11e4 <do_bz18820+0xb4>

LLVM IR (-O0):
@errno = global i32 0
@.str = [2 x i8] c"?\00"
@.str.1 = [33 x i8] c"fmemopen: expected NULL, got %p\0A\00"
@EINVAL = global i32 0
@.str.2 = [40 x i8] c"fmemopen: got %i, expected EINVAL (%i)\0A\00"
@.str.3 = [2 x i8] c"w\00"
@ENOMEM = global i32 0
@.str.4 = [40 x i8] c"fmemopen: got %i, expected ENOMEM (%i)\0A\00"
define i32 @do_bz18820() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32*
store i32 0, i32* @errno
%4 = call i32* @fmemopen(i8* %2, i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i32*, i32** %3
%9 = ptrtoint i32* %8 to i32
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32*, i32** %3
%12 = call i32 @fclose(i32* %11)
store i32 1, i32* %1
br label %50
13: ; preds = %0
%14 = load i32, i32* @errno
%15 = load i32, i32* @EINVAL
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = load i32, i32* @EINVAL
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
store i32 10, i32* %1
br label %50
21: ; preds = %13
%22 = call i32* @fmemopen(i8* null, i32 42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %3
%23 = load i32*, i32** %3
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i32*, i32** %3
%27 = ptrtoint i32* %26 to i32
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32*, i32** %3
%30 = call i32 @fclose(i32* %29)
store i32 2, i32* %1
br label %50
31: ; preds = %21
store i32 0, i32* @errno
%32 = call i32* @fmemopen(i8* null, i32 -1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %32, i32** %3
%33 = load i32*, i32** %3
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i32*, i32** %3
%37 = ptrtoint i32* %36 to i32
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i32*, i32** %3
%40 = call i32 @fclose(i32* %39)
store i32 3, i32* %1
br label %50
41: ; preds = %31
%42 = load i32, i32* @errno
%43 = load i32, i32* @ENOMEM
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* @errno
%47 = load i32, i32* @ENOMEM
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %46, i32 %47)
store i32 20, i32* %1
br label %50
49: ; preds = %41
store i32 0, i32* %1
br label %50
50: ; preds = %49, %45, %35, %25, %17, %7
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [2 x i8] c"?\00"
@.str.1 = [33 x i8] c"fmemopen: expected NULL, got %p\0A\00"
@EINVAL = global i32 0
@.str.2 = [40 x i8] c"fmemopen: got %i, expected EINVAL (%i)\0A\00"
@.str.3 = [2 x i8] c"w\00"
@ENOMEM = global i32 0
@.str.4 = [40 x i8] c"fmemopen: got %i, expected ENOMEM (%i)\0A\00"
define i32 @do_bz18820() {
%1 = alloca i8
store i32 0, i32* @errno
%2 = call i32* @fmemopen(i8* %1, i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %9, label %4
4: ; preds = %0
%5 = ptrtoint i32* %2 to i64
%6 = trunc i64 %5 to i32
%7 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = call i32 @fclose(i32* %2)
br label %37
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = load i32, i32* @EINVAL
%12 = icmp eq i32 %10, %11
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %11)
br label %37
15: ; preds = %9
%16 = call i32* @fmemopen(i8* null, i32 42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = ptrtoint i32* %16 to i64
%20 = trunc i64 %19 to i32
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @fclose(i32* %16)
br label %37
23: ; preds = %15
store i32 0, i32* @errno
%24 = call i32* @fmemopen(i8* null, i32 -1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = ptrtoint i32* %24 to i64
%28 = trunc i64 %27 to i32
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i32 @fclose(i32* %24)
br label %37
31: ; preds = %23
%32 = load i32, i32* @errno
%33 = load i32, i32* @ENOMEM
%34 = icmp eq i32 %32, %33
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %33)
br label %37
37: ; preds = %31, %35, %26, %18, %13, %4
%38 = phi i32 [ 1, %4 ], [ 10, %13 ], [ 2, %18 ], [ 3, %26 ], [ 20, %35 ], [ 0, %31 ]
ret i32 %38
}