#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 */
struct stat {
  scalar_t__ st_nlink;
  scalar_t__ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
scalar_t__ flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ unlink(char *);
int read_exclusive(int fd) {
  struct stat fileinfo;
  char readchar;
  if (flock(fd, LOCK_EX) != 0) {
    perror("flock");
    return -1;
  }
  if (unlink(".savefile") != 0) {
    perror("unlink");
    return -1;
  }
  if (fstat(fd, &fileinfo) != 0) {
    perror("fstat");
    return -1;
  }
  if (fileinfo.st_nlink != 0) {
    fprintf(stderr, "st_nlink == %d != 0\n", fileinfo.st_nlink);
    return -1;
  }
  if (fileinfo.st_size == 0) {
    fprintf(stderr, "st_size == %d\n", fileinfo.st_size);
    return -1;
  }
  if (read(fd, &readchar, 1) != 1) {
    perror("read");
    return -1;
  }
  if (ftruncate(fd, 0) != 0) {
    perror("ftruncate");
    return -1;
  }
  if (readchar == '1') {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_exclusive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%rax
je     11ae <read_exclusive+0x3e>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
lea    0xe51(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     11dc <read_exclusive+0x6c>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     120a <read_exclusive+0x9a>
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
cmpq   $0x0,-0x18(%rbp)
je     123a <read_exclusive+0xca>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdf4(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
cmpq   $0x0,-0x10(%rbp)
jne    126a <read_exclusive+0xfa>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd9(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     129c <read_exclusive+0x12c>
lea    0xdb6(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
je     12c8 <read_exclusive+0x158>
lea    0xd8f(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
movsbl -0x19(%rbp),%eax
cmp    $0x31,%eax
jne    12e1 <read_exclusive+0x171>
movl   $0x1,-0x4(%rbp)
jmp    12e8 <read_exclusive+0x178>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_exclusive>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
test   %rax,%rax
jne    1221 <read_exclusive+0xb1>
lea    0xe71(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jne    122a <read_exclusive+0xba>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jne    1233 <read_exclusive+0xc3>
mov    0x10(%rsp),%rdx
test   %rdx,%rdx
je     11d5 <read_exclusive+0x65>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1060 <fprintf@plt>
jmp    123f <read_exclusive+0xcf>
cmpq   $0x0,0x18(%rsp)
je     120d <read_exclusive+0x9d>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    124a <read_exclusive+0xda>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ftruncate@plt>
test   %rax,%rax
jne    1253 <read_exclusive+0xe3>
xor    %eax,%eax
cmpb   $0x31,0xf(%rsp)
sete   %al
jmp    1244 <read_exclusive+0xd4>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
xor    %edx,%edx
jmp    11ce <read_exclusive+0x5e>
lea    0xdd8(%rip),%rdi
jmp    123a <read_exclusive+0xca>
lea    0xddf(%rip),%rdi
jmp    123a <read_exclusive+0xca>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdf0(%rip),%rdi
jmp    123a <read_exclusive+0xca>
lea    0xdec(%rip),%rdi
jmp    123a <read_exclusive+0xca>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@LOCK_EX = global i32 0
@.str = [6 x i8] c"flock\00"
@.str.1 = [10 x i8] c".savefile\00"
@.str.2 = [7 x i8] c"unlink\00"
@.str.3 = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.4 = [21 x i8] c"st_nlink == %d != 0\0A\00"
@.str.5 = [15 x i8] c"st_size == %d\0A\00"
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [10 x i8] c"ftruncate\00"
define i32 @read_exclusive(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* @LOCK_EX
%8 = call i64 @flock(i32 %6, i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
12: ; preds = %1
%13 = call i64 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
17: ; preds = %12
%18 = load i32, i32* %3
%19 = call i64 @fstat(i32 %18, %struct.stat* %4)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
23: ; preds = %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%25 = load i64, i64* %24
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%30 = load i64, i64* %29
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i64 %30)
store i32 -1, i32* %2
br label %59
32: ; preds = %23
%33 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%34 = load i64, i64* %33
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%39 = load i64, i64* %38
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i64 %39)
store i32 -1, i32* %2
br label %59
41: ; preds = %32
%42 = load i32, i32* %3
%43 = call i32 @read(i32 %42, i8* %5, i32 1)
%44 = icmp ne i32 %43, 1
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
47: ; preds = %41
%48 = load i32, i32* %3
%49 = call i64 @ftruncate(i32 %48, i32 0)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
53: ; preds = %47
%54 = load i8, i8* %5
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 49
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 1, i32* %2
br label %59
58: ; preds = %53
store i32 0, i32* %2
br label %59
59: ; preds = %58, %57, %51, %45, %36, %27, %21, %15, %10
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@LOCK_EX = global i32 0
@.str = [6 x i8] c"flock\00"
@.str.1 = [10 x i8] c".savefile\00"
@.str.2 = [7 x i8] c"unlink\00"
@.str.3 = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.4 = [21 x i8] c"st_nlink == %d != 0\0A\00"
@.str.5 = [15 x i8] c"st_size == %d\0A\00"
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [10 x i8] c"ftruncate\00"
define i32 @read_exclusive(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca i8
%4 = bitcast %struct.stat* %2 to i8*
%5 = load i32, i32* @LOCK_EX
%6 = tail call i64 @flock(i32 %0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %48
10: ; preds = %1
%11 = tail call i64 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %48
15: ; preds = %10
%16 = call i64 @fstat(i32 %0, %struct.stat* %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %48
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%22 = load i64, i64* %21
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i64 %22)
br label %48
27: ; preds = %20
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%29 = load i64, i64* %28
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i64 0)
br label %48
34: ; preds = %27
%35 = call i32 @read(i32 %0, i8* %3, i32 1)
%36 = icmp eq i32 %35, 1
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %48
39: ; preds = %34
%40 = tail call i64 @ftruncate(i32 %0, i32 0)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %39
%43 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
br label %48
44: ; preds = %39
%45 = load i8, i8* %3
%46 = icmp eq i8 %45, 49
%47 = zext i1 %46 to i32
br label %48
48: ; preds = %44, %42, %37, %31, %24, %18, %13, %8
%49 = phi i32 [ -1, %8 ], [ -1, %13 ], [ -1, %18 ], [ -1, %24 ], [ -1, %31 ], [ -1, %37 ], [ -1, %42 ], [ %47, %44 ]
ret i32 %49
}