#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 BUFSIZ;
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ _IOFBF;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ init_stream(int /*<<< orphan*/ *, int, unsigned int,
                               int /*<<< orphan*/, char *, int);
scalar_t__ malloc(int);
int parse_mode(char const *, unsigned int *);
int /*<<< orphan*/ set_stdio_exit_fun_and_add_stream(int /*<<< orphan*/ *);
FILE *fdopen(int fd, const char *mode) {
  FILE *stream;
  char *buf;
  unsigned stream_flags;
  int flags = parse_mode(mode, &stream_flags);
  if (flags == -1)
    return NULL;
  stream = (FILE *)malloc(sizeof(FILE));
  if (stream == NULL) {
    errno = ENOMEM;
    return NULL;
  }
  buf = (char *)malloc(BUFSIZ);
  if (buf == NULL) {
    errno = ENOMEM;
    free(buf);
    return NULL;
  }
  init_stream(stream, fd, stream_flags, _IOFBF, buf, -1);
  set_stdio_exit_fun_and_add_stream(stream);
  return stream;
}


x86 assembly (clang, -O0):
<fdopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1070 <parse_mode@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1186 <fdopen+0x36>
movq   $0x0,-0x8(%rbp)
jmp    1236 <fdopen+0xe6>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11be <fdopen+0x6e>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e21(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1236 <fdopen+0xe6>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1203 <fdopen+0xb3>
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
mov    0x2de5(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1236 <fdopen+0xe6>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    0x2db4(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8
mov    $0xffffffff,%r9d
call   1040 <init_stream@plt>
mov    -0x20(%rbp),%rdi
call   1060 <set_stdio_exit_fun_and_add_stream@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdopen>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %edi,%ebp
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
call   1070 <parse_mode@plt>
cmp    $0xffffffff,%eax
je     11b9 <fdopen+0x69>
mov    $0x4,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11bd <fdopen+0x6d>
mov    %rax,%rbx
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d3 <fdopen+0x83>
mov    0x4(%rsp),%edx
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%ecx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rax,%r8
mov    $0xffffffff,%r9d
call   1040 <init_stream@plt>
mov    %rbx,%rdi
call   1060 <set_stdio_exit_fun_and_add_stream@plt>
jmp    11ee <fdopen+0x9e>
xor    %ebx,%ebx
jmp    11ee <fdopen+0x9e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    0x2e03(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
jmp    11ee <fdopen+0x9e>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    0x2ded(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@BUFSIZ = global i32 0
@_IOFBF = global i32 0
define i32* @fdopen(i32 %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @parse_mode(i8* %10, i32* %8)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32* null, i32** %3
br label %42
15: ; preds = %2
%16 = call i64 @malloc(i32 4)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = load i32, i32* @ENOMEM
store i32 %21, i32* @errno
store i32* null, i32** %3
br label %42
22: ; preds = %15
%23 = load i32, i32* @BUFSIZ
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* @ENOMEM
store i32 %29, i32* @errno
%30 = load i8*, i8** %7
%31 = call i32 @free(i8* %30)
store i32* null, i32** %3
br label %42
32: ; preds = %22
%33 = load i32*, i32** %6
%34 = load i32, i32* %4
%35 = load i32, i32* %8
%36 = load i32, i32* @_IOFBF
%37 = load i8*, i8** %7
%38 = call i32 @init_stream(i32* %33, i32 %34, i32 %35, i32 %36, i8* %37, i32 -1)
%39 = load i32*, i32** %6
%40 = call i32 @set_stdio_exit_fun_and_add_stream(i32* %39)
%41 = load i32*, i32** %6
store i32* %41, i32** %3
br label %42
42: ; preds = %32, %28, %20, %14
%43 = load i32*, i32** %3
ret i32* %43
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@BUFSIZ = global i32 0
@_IOFBF = global i32 0
define i32* @fdopen(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @parse_mode(i8* %1, i32* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %26, label %7
7: ; preds = %2
%8 = call i64 @malloc(i32 4)
%9 = inttoptr i64 %8 to i32*
%10 = icmp eq i64 %8, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = load i32, i32* @ENOMEM
store i32 %12, i32* @errno
br label %26
13: ; preds = %7
%14 = load i32, i32* @BUFSIZ
%15 = call i64 @malloc(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @ENOMEM
store i32 %18, i32* @errno
%19 = call i32 @free(i8* null)
br label %26
20: ; preds = %13
%21 = inttoptr i64 %15 to i8*
%22 = load i32, i32* %3
%23 = load i32, i32* @_IOFBF
%24 = call i32 @init_stream(i32* %9, i32 %0, i32 %22, i32 %23, i8* %21, i32 -1)
%25 = call i32 @set_stdio_exit_fun_and_add_stream(i32* %9)
br label %26
26: ; preds = %2, %20, %17, %11
%27 = phi i32* [ null, %11 ], [ null, %17 ], [ %9, %20 ], [ null, %2 ]
ret i32* %27
}