#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 */

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int O_CREAT;
int O_RDWR;
scalar_t__ PATH_MAX;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ ftruncate(int, int);
scalar_t__ isNFS(char const *);
int open(char *, int, ...);
scalar_t__ snprintf(char *, scalar_t__, char *, char const *, char const *);
int toFD(char *path, const char *dir, const char *filename, int size) {
  int fd;
  if (snprintf(path, PATH_MAX, "%s/%s", dir, filename) >= PATH_MAX)
    return -1;
  if (access(path, F_OK)) {
    if (isNFS(dir))
      return -1;
    fd = open(path, O_RDWR | O_CREAT, 0644);
    if (fd < 0)
      return -1;
    ftruncate(fd, size);
  } else {
    if (isNFS(path))
      return -1;
    fd = open(path, O_RDWR);
    if (fd < 0)
      return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<toFD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe7c(%rip),%rdx
call   1030 <snprintf@plt>
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
jl     11a5 <toFD+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <toFD+0x132>
mov    -0x10(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
je     122b <toFD+0xdb>
mov    -0x18(%rbp),%rdi
call   1070 <isNFS@plt>
cmp    $0x0,%rax
je     11e0 <toFD+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <toFD+0x132>
mov    -0x10(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    121b <toFD+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <toFD+0x132>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1040 <ftruncate@plt>
jmp    127c <toFD+0x12c>
mov    -0x10(%rbp),%rdi
call   1070 <isNFS@plt>
cmp    $0x0,%rax
je     124a <toFD+0xfa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <toFD+0x132>
mov    -0x10(%rbp),%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1277 <toFD+0x127>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <toFD+0x132>
jmp    127c <toFD+0x12c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toFD>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%r8
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e7d(%rip),%rbp
mov    0x0(%rbp),%rsi
lea    0xe8a(%rip),%rdx
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    $0xffffffff,%r15d
cmp    0x0(%rbp),%rax
jge    1209 <toFD+0xb9>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     11e0 <toFD+0x90>
mov    %rbx,%rdi
call   1070 <isNFS@plt>
test   %rax,%rax
jne    1209 <toFD+0xb9>
mov    0x2e24(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r12,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1050 <open@plt>
test   %eax,%eax
js     1209 <toFD+0xb9>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14d,%esi
call   1040 <ftruncate@plt>
jmp    1206 <toFD+0xb6>
mov    %r12,%rdi
call   1070 <isNFS@plt>
test   %rax,%rax
jne    1209 <toFD+0xb9>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1209 <toFD+0xb9>
mov    %ebx,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
@.str = [6 x i8] c"%s/%s\00"
@F_OK = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
define i32 @toFD(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i8*, i8** %6
%12 = load i64, i64* @PATH_MAX
%13 = load i8*, i8** %7
%14 = load i8*, i8** %8
%15 = call i64 @snprintf(i8* %11, i64 %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14)
%16 = load i64, i64* @PATH_MAX
%17 = icmp sge i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %57
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = load i32, i32* @F_OK
%22 = call i64 @access(i8* %20, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %19
%25 = load i8*, i8** %7
%26 = call i64 @isNFS(i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %5
br label %57
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = load i32, i32* @O_RDWR
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = call i32 (i8*, i32, ...) @open(i8* %30, i32 %33, i32 420)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %29
store i32 -1, i32* %5
br label %57
38: ; preds = %29
%39 = load i32, i32* %10
%40 = load i32, i32* %9
%41 = call i32 @ftruncate(i32 %39, i32 %40)
br label %55
42: ; preds = %19
%43 = load i8*, i8** %6
%44 = call i64 @isNFS(i8* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %5
br label %57
47: ; preds = %42
%48 = load i8*, i8** %6
%49 = load i32, i32* @O_RDWR
%50 = call i32 (i8*, i32, ...) @open(i8* %48, i32 %49)
store i32 %50, i32* %10
%51 = load i32, i32* %10
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %47
store i32 -1, i32* %5
br label %57
54: ; preds = %47
br label %55
55: ; preds = %54, %38
%56 = load i32, i32* %10
store i32 %56, i32* %5
br label %57
57: ; preds = %55, %53, %46, %37, %28, %18
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@.str = [6 x i8] c"%s/%s\00"
@F_OK = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
define i32 @toFD(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = load i64, i64* @PATH_MAX
%6 = tail call i64 @snprintf(i8* %0, i64 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%7 = load i64, i64* @PATH_MAX
%8 = icmp slt i64 %6, %7
br i1 %8, label %9, label %33
9: ; preds = %4
%10 = load i32, i32* @F_OK
%11 = tail call i64 @access(i8* %0, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %9
%14 = tail call i64 @isNFS(i8* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %13
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %19, i32 420)
%21 = icmp slt i32 %20, 0
br i1 %21, label %33, label %22
22: ; preds = %16
%23 = tail call i32 @ftruncate(i32 %20, i32 %3)
br label %31
24: ; preds = %9
%25 = tail call i64 @isNFS(i8* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* @O_RDWR
%29 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27, %22
%32 = phi i32 [ %20, %22 ], [ %29, %27 ]
br label %33
33: ; preds = %27, %24, %16, %13, %4, %31
%34 = phi i32 [ %32, %31 ], [ -1, %4 ], [ -1, %13 ], [ -1, %16 ], [ -1, %24 ], [ -1, %27 ]
ret i32 %34
}