#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 S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ W_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ asprintf(char **, char *);
int creat(char *, int);
int /*<<< orphan*/ dbgp(char *, char *);
char *strchr(char const *, char);
char *strrchr(char const *, char);
int /*<<< orphan*/ unlink(char *);
bool file_check_config(const char *cfgstring, char **reason) {
  char *path;
  int fd;
  path = strchr(cfgstring, '/');
  if (!path) {
    asprintf(reason, "No path found");
    return false;
  }
  path += 1;
  path = strchr(cfgstring, '/');
  path += 1;
  path = strrchr(cfgstring, '/');
  dbgp("Check path = %s\n ", path);
  if (access(path, W_OK) != -1)
    return true;
  fd = creat(path, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    asprintf(reason, "Could not create file");
    return false;
  }
  unlink(path);
  return true;
}


x86 assembly (clang, -O0):
<file_check_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b9 <file_check_config+0x49>
mov    -0x18(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1070 <asprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1280 <file_check_config+0x110>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe0e(%rip),%rdi
call   1040 <dbgp@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     122c <file_check_config+0xbc>
movl   $0x1,-0x4(%rbp)
jmp    1280 <file_check_config+0x110>
mov    -0x20(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
call   1090 <creat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1270 <file_check_config+0x100>
mov    -0x18(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   1070 <asprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1280 <file_check_config+0x110>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_check_config>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11ec <file_check_config+0x7c>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbx
lea    0xe6e(%rip),%rdi
mov    %rax,%rsi
call   1040 <dbgp@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
jne    1206 <file_check_config+0x96>
mov    0x2e06(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1090 <creat@plt>
cmp    $0xffffffff,%eax
je     11f5 <file_check_config+0x85>
mov    %rbx,%rdi
call   1030 <unlink@plt>
jmp    1206 <file_check_config+0x96>
lea    0xe0d(%rip),%rsi
jmp    11fc <file_check_config+0x8c>
lea    0xe24(%rip),%rsi
mov    %r14,%rdi
call   1070 <asprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"No path found\00"
@.str.1 = [18 x i8] c"Check path = %s\0A \00"
@W_OK = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [22 x i8] c"Could not create file\00"
define i32 @file_check_config(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8*, i8** %4
%9 = call i8* @strchr(i8* %8, i8 signext 47)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = call i32 @asprintf(i8** %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %45
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %17, i8** %6
%18 = load i8*, i8** %4
%19 = call i8* @strchr(i8* %18, i8 signext 47)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8* %21, i8** %6
%22 = load i8*, i8** %4
%23 = call i8* @strrchr(i8* %22, i8 signext 47)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = call i32 @dbgp(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %6
%27 = load i32, i32* @W_OK
%28 = call i32 @access(i8* %26, i32 %27)
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %15
store i32 1, i32* %3
br label %45
31: ; preds = %15
%32 = load i8*, i8** %6
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = call i32 @creat(i8* %32, i32 %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = load i8**, i8*** %5
%41 = call i32 @asprintf(i8** %40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %45
42: ; preds = %31
%43 = load i8*, i8** %6
%44 = call i32 @unlink(i8* %43)
store i32 1, i32* %3
br label %45
45: ; preds = %42, %39, %30, %12
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [14 x i8] c"No path found\00"
@.str.1 = [18 x i8] c"Check path = %s\0A \00"
@W_OK = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [22 x i8] c"Could not create file\00"
define i32 @file_check_config(i8* %0, i8** %1) {
%3 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %23
7: ; preds = %2
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%9 = tail call i32 @dbgp(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @W_OK
%11 = tail call i32 @access(i8* %8, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %23
13: ; preds = %7
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = tail call i32 @creat(i8* %8, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %23
21: ; preds = %13
%22 = tail call i32 @unlink(i8* %8)
br label %23
23: ; preds = %7, %21, %19, %5
%24 = phi i32 [ 0, %19 ], [ 1, %21 ], [ 0, %5 ], [ 1, %7 ]
ret i32 %24
}