#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 O_CREAT;
int O_EXCL;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ dv_error(char *);
scalar_t__ errno;
int /*<<< orphan*/ *fdopen(int, char *);
int open(char const *, int, int);
FILE *dv_fcreate(const char *path) {
  if (errno) {
    dv_error("");
  }
  int fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    dv_error("Failed to create file newly.");
  }
  FILE *fp = fdopen(fd, "w+");
  if (fp == NULL) {
    dv_error("Failed to wrap file descriptor in FILE.");
  }
  return fp;
}


x86 assembly (clang, -O0):
<dv_fcreate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
cmpq   $0x0,(%rax)
je     1159 <dv_fcreate+0x29>
lea    0xecb(%rip),%rdi
call   1050 <dv_error@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
or     (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    0x2e50(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a8 <dv_fcreate+0x78>
lea    0xe5d(%rip),%rdi
call   1050 <dv_error@plt>
mov    -0xc(%rbp),%edi
lea    0xe6b(%rip),%rsi
call   1030 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <dv_fcreate+0xa2>
lea    0xe53(%rip),%rdi
call   1050 <dv_error@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dv_fcreate>:
push   %rbx
mov    %rdi,%rbx
mov    0x2e85(%rip),%rax
cmpq   $0x0,(%rax)
je     114d <dv_fcreate+0x1d>
lea    0xed7(%rip),%rdi
call   1050 <dv_error@plt>
mov    0x2e8c(%rip),%rax
mov    0x2e9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
mov    0x2e49(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1194 <dv_fcreate+0x64>
lea    0xe71(%rip),%rdi
call   1050 <dv_error@plt>
lea    0xe82(%rip),%rsi
mov    %ebx,%edi
call   1030 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b6 <dv_fcreate+0x86>
lea    0xe6f(%rip),%rdi
call   1050 <dv_error@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [29 x i8] c"Failed to create file newly.\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [40 x i8] c"Failed to wrap file descriptor in FILE.\00"
define i32* @dv_fcreate(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i8* %0, i8** %2
%5 = load i64, i64* @errno
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i8*, i8** %2
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_EXCL
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %10, i32 %15, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %9
%23 = call i32 @dv_error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %9
%25 = load i32, i32* %3
%26 = call i32* @fdopen(i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %4
%27 = load i32*, i32** %4
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @dv_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = load i32*, i32** %4
ret i32* %32
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [29 x i8] c"Failed to create file newly.\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [40 x i8] c"Failed to wrap file descriptor in FILE.\00"
define noalias i32* @dv_fcreate(i8* nocapture %0) {
%2 = load i64, i64* @errno
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_EXCL
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %0, i32 %11, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %6
%18 = tail call i32 @dv_error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %6
%20 = tail call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @dv_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
ret i32* %20
}