#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 char FILE;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ ENOMEM;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ _IOFBF;
int /*<<< orphan*/ _IOLBF;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ init_stream(char *, int, unsigned int, int /*<<< orphan*/,
                               char *, int);
scalar_t__ isatty(int);
scalar_t__ malloc(int);
int open(char const *, int, int);
int parse_mode(char const *, unsigned int *);
int /*<<< orphan*/ set_stdio_exit_fun_and_add_stream(char *);
FILE *fopen(const char *file_name, const char *mode) {
  FILE *stream;
  char *buf;
  unsigned stream_flags;
  int fd, 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;
  }
  fd = open(file_name, flags,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  if (fd == -1) {
    free(buf);
    free(stream);
    return NULL;
  }
  init_stream(stream, fd, stream_flags, (isatty(fd) ? _IOLBF : _IOFBF), buf,
              -1);
  set_stdio_exit_fun_and_add_stream(stream);
  return stream;
}


x86 assembly (clang, -O0):
<fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1090 <parse_mode@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11a7 <fopen+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1302 <fopen+0x192>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <fopen+0x6f>
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
mov    0x2df0(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1302 <fopen+0x192>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1224 <fopen+0xb4>
mov    0x2dcd(%rip),%rax
mov    (%rax),%ecx
mov    0x2db4(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1302 <fopen+0x192>
mov    -0x10(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
mov    0x2da5(%rip),%rax
or     (%rax),%edx
mov    0x2d6c(%rip),%rax
or     (%rax),%edx
mov    0x2d9b(%rip),%rax
or     (%rax),%edx
mov    0x2d52(%rip),%rax
or     (%rax),%edx
mov    0x2d29(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1292 <fopen+0x122>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1302 <fopen+0x192>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <isatty@plt>
cmp    $0x0,%rax
je     12c9 <fopen+0x159>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    12d5 <fopen+0x165>
mov    0x2cc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%edx
mov    -0x3c(%rbp),%esi
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    $0xffffffff,%r9d
call   1050 <init_stream@plt>
mov    -0x20(%rbp),%rdi
call   1080 <set_stdio_exit_fun_and_add_stream@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%r15
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
call   1090 <parse_mode@plt>
cmp    $0xffffffff,%eax
je     121e <fopen+0xae>
mov    %eax,%ebp
mov    $0x1,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1222 <fopen+0xb2>
mov    %rax,%rbx
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1238 <fopen+0xc8>
mov    %rax,%r14
mov    0x2ddc(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dda(%rip),%rax
or     (%rax),%edx
mov    0x2e09(%rip),%rax
or     (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
mov    0x2d97(%rip),%rax
or     (%rax),%edx
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1255 <fopen+0xe5>
mov    %eax,%ebp
mov    0x4(%rsp),%r15d
mov    %eax,%edi
call   1040 <isatty@plt>
test   %rax,%rax
jne    1269 <fopen+0xf9>
mov    0x2d7c(%rip),%rax
jmp    1270 <fopen+0x100>
xor    %ebx,%ebx
jmp    1290 <fopen+0x120>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    0x2d8e(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
jmp    1290 <fopen+0x120>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    0x2d78(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <free@plt>
jmp    1290 <fopen+0x120>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1290 <fopen+0x120>
mov    0x2d48(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r14,%r8
mov    $0xffffffff,%r9d
call   1050 <init_stream@plt>
mov    %rbx,%rdi
call   1080 <set_stdio_exit_fun_and_add_stream@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@BUFSIZ = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@_IOLBF = global i32 0
@_IOFBF = global i32 0
define i8* @fopen(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @parse_mode(i8* %11, i32* %8)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %73
16: ; preds = %2
%17 = call i64 @malloc(i32 1)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i32, i32* @ENOMEM
store i32 %22, i32* @errno
store i8* null, i8** %3
br label %73
23: ; preds = %16
%24 = load i32, i32* @BUFSIZ
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* @ENOMEM
store i32 %30, i32* @errno
%31 = load i8*, i8** %7
%32 = call i32 @free(i8* %31)
store i8* null, i8** %3
br label %73
33: ; preds = %23
%34 = load i8*, i8** %4
%35 = load i32, i32* %10
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IRGRP
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IWGRP
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IROTH
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IWOTH
%46 = or i32 %44, %45
%47 = call i32 @open(i8* %34, i32 %35, i32 %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %33
%51 = load i8*, i8** %7
%52 = call i32 @free(i8* %51)
%53 = load i8*, i8** %6
%54 = call i32 @free(i8* %53)
store i8* null, i8** %3
br label %73
55: ; preds = %33
%56 = load i8*, i8** %6
%57 = load i32, i32* %9
%58 = load i32, i32* %8
%59 = load i32, i32* %9
%60 = call i64 @isatty(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %55
%63 = load i32, i32* @_IOLBF
br label %66
64: ; preds = %55
%65 = load i32, i32* @_IOFBF
br label %66
66: ; preds = %64, %62
%67 = phi i32 [ %63, %62 ], [ %65, %64 ]
%68 = load i8*, i8** %7
%69 = call i32 @init_stream(i8* %56, i32 %57, i32 %58, i32 %67, i8* %68, i32 -1)
%70 = load i8*, i8** %6
%71 = call i32 @set_stdio_exit_fun_and_add_stream(i8* %70)
%72 = load i8*, i8** %6
store i8* %72, i8** %3
br label %73
73: ; preds = %66, %50, %29, %21, %15
%74 = load i8*, i8** %3
ret i8* %74
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@BUFSIZ = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@_IOLBF = global i32 0
@_IOFBF = global i32 0
define i8* @fopen(i8* nocapture %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 %47, label %7
7: ; preds = %2
%8 = call i64 @malloc(i32 1)
%9 = inttoptr i64 %8 to i8*
%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 %47
13: ; preds = %7
%14 = load i32, i32* @BUFSIZ
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @ENOMEM
store i32 %19, i32* @errno
%20 = call i32 @free(i8* null)
br label %47
21: ; preds = %13
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %23, %22
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IWGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IROTH
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IWOTH
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %0, i32 %5, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %21
%36 = call i32 @free(i8* %16)
%37 = call i32 @free(i8* %9)
br label %47
38: ; preds = %21
%39 = load i32, i32* %3
%40 = call i64 @isatty(i32 %33)
%41 = icmp eq i64 %40, 0
%42 = load i32, i32* @_IOLBF
%43 = load i32, i32* @_IOFBF
%44 = select i1 %41, i32 %43, i32 %42
%45 = call i32 @init_stream(i8* %9, i32 %33, i32 %39, i32 %44, i8* %16, i32 -1)
%46 = call i32 @set_stdio_exit_fun_and_add_stream(i8* %9)
br label %47
47: ; preds = %2, %38, %35, %18, %11
%48 = phi i8* [ null, %11 ], [ null, %18 ], [ null, %35 ], [ %9, %38 ], [ null, %2 ]
ret i8* %48
}