#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 */
scalar_t__ EROFS;
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ stderr;
char *tempnam(char *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int is_readonly_dir(char *dir) {
  char *name;
  FILE *f;
  name = tempnam(dir, 0);
  if (!name) {
    fprintf(stderr, "is_readonly: tempnam failed, think fs is not readonly\n");
    return 0;
  }
  f = fopen(name, "w");
  if (f) {
    unlink(name);
    free(name);
    return 0;
  }
  free(name);
  return (errno == EROFS) ? 1 : 0;
}


x86 assembly (clang, -O0):
<is_readonly_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <tempnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <is_readonly_dir+0x47>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1201 <is_readonly_dir+0xb1>
mov    -0x18(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d4 <is_readonly_dir+0x84>
mov    -0x18(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1201 <is_readonly_dir+0xb1>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%rdx
mov    0x2dfa(%rip),%rax
mov    (%rax),%rsi
xor    %eax,%eax
mov    $0x1,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_readonly_dir>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %esi,%esi
call   1060 <tempnam@plt>
test   %rax,%rax
je     118c <is_readonly_dir+0x3c>
mov    %rax,%rbx
lea    0xecc(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a3 <is_readonly_dir+0x53>
mov    %rbx,%rdi
call   1040 <unlink@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    11c5 <is_readonly_dir+0x75>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11c5 <is_readonly_dir+0x75>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
sete   %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [55 x i8] c"is_readonly: tempnam failed, think fs is not\0A\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i64 0
@EROFS = global i64 0
define i32 @is_readonly_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @tempnam(i8* %6, i32 0)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %31
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %5
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32 @unlink(i8* %19)
%21 = load i8*, i8** %4
%22 = call i32 @free(i8* %21)
store i32 0, i32* %2
br label %31
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = call i32 @free(i8* %24)
%26 = load i64, i64* @errno
%27 = load i64, i64* @EROFS
%28 = icmp eq i64 %26, %27
%29 = zext i1 %28 to i64
%30 = select i1 %28, i32 1, i32 0
store i32 %30, i32* %2
br label %31
31: ; preds = %23, %18, %10
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [55 x i8] c"is_readonly: tempnam failed, think fs is not\0A\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i64 0
@EROFS = global i64 0
define i32 @is_readonly_dir(i8* %0) {
%2 = tail call i8* @tempnam(i8* %0, i32 0)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
br label %19
7: ; preds = %1
%8 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call i32 @unlink(i8* %2)
%12 = tail call i32 @free(i8* %2)
br label %19
13: ; preds = %7
%14 = tail call i32 @free(i8* %2)
%15 = load i64, i64* @errno
%16 = load i64, i64* @EROFS
%17 = icmp eq i64 %15, %16
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %13, %10, %4
%20 = phi i32 [ 0, %10 ], [ %18, %13 ], [ 0, %4 ]
ret i32 %20
}