#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ _fileno;
};
typedef TYPE_1__ FILE;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ creat(char *, int);
int /*<<< orphan*/ fclose(TYPE_1__ *);
scalar_t__ flock(int /*<<< orphan*/, int /*<<< orphan*/);
TYPE_1__ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  FILE *fp = NULL;
  int i = 20;
  if (access("file_lock.test", F_OK) != 0) {
    creat("file_lock.test", 0777);
  }
  if ((fp = fopen("file_lock.test", "r+b")) == NULL)
    printf("file open error!\n");
  if (flock(fp->_fileno, LOCK_EX) != 0)
    printf("file lock by others\n");
  while (1) {
    printf("%d\n", i--);
    sleep(1);
    if (i == 0)
      break;
  }
  fclose(fp);
  flock(fp->_fileno, LOCK_UN);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x14,-0x1c(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1050 <access@plt>
cmp    $0x0,%rax
je     11c5 <main+0x55>
lea    0xe45(%rip),%rdi
mov    $0x1ff,%esi
call   1080 <creat@plt>
lea    0xe34(%rip),%rdi
lea    0xe3c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f4 <main+0x84>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
cmp    $0x0,%rax
je     1220 <main+0xb0>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1225 <main+0xb5>
mov    -0x1c(%rbp),%esi
mov    %esi,%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1257 <main+0xe7>
jmp    125c <main+0xec>
jmp    1225 <main+0xb5>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1060 <access@plt>
test   %rax,%rax
je     11af <main+0x2f>
lea    0xe5b(%rip),%rdi
mov    $0x1ff,%esi
call   1090 <creat@plt>
lea    0xe4a(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d6 <main+0x56>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
test   %rax,%rax
je     11f8 <main+0x78>
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
lea    0xe14(%rip),%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x13,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x12,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x11,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xf,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xe,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xd,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xb,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    (%r14),%edi
mov    0x2bd3(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32 }
@.str = [15 x i8] c"file_lock.test\00"
@F_OK = global i32 0
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [18 x i8] c"file open error!\0A\00"
@LOCK_EX = global i32 0
@.str.3 = [21 x i8] c"file lock by others\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@LOCK_UN = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.TYPE_4__*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.TYPE_4__* null, %struct.TYPE_4__** %6
store i32 20, i32* %7
%8 = load i32, i32* @F_OK
%9 = call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @creat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 511)
br label %13
13: ; preds = %11, %2
%14 = call %struct.TYPE_4__* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.TYPE_4__* %14, %struct.TYPE_4__** %6
%15 = icmp eq %struct.TYPE_4__* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%20 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @LOCK_EX
%23 = call i64 @flock(i32 %21, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
br label %28
28: ; preds = %27, %36
%29 = load i32, i32* %7
%30 = add nsw i32 %29, -1
store i32 %30, i32* %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%32 = call i32 @sleep(i32 1)
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28
br label %37
36: ; preds = %28
br label %28
37: ; preds = %35
%38 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%39 = call i32 @fclose(%struct.TYPE_4__* %38)
%40 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* @LOCK_UN
%44 = call i64 @flock(i32 %42, i32 %43)
ret i32 0
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32 }
@.str = [15 x i8] c"file_lock.test\00"
@F_OK = global i32 0
@.str.1 = [4 x i8] c"r+b\00"
@LOCK_EX = global i32 0
@.str.4 = [4 x i8] c"%d\0A\00"
@LOCK_UN = global i32 0
@str = [20 x i8] c"file lock by others\00"
@str.5 = [17 x i8] c"file open error!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @F_OK
%4 = tail call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @creat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 511)
br label %8
8: ; preds = %6, %2
%9 = tail call %struct.TYPE_4__* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq %struct.TYPE_4__* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %9, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @LOCK_EX
%17 = tail call i64 @flock(i32 %15, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 20)
%23 = tail call i32 @sleep(i32 1)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 19)
%25 = tail call i32 @sleep(i32 1)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 18)
%27 = tail call i32 @sleep(i32 1)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 17)
%29 = tail call i32 @sleep(i32 1)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 16)
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 15)
%33 = tail call i32 @sleep(i32 1)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 14)
%35 = tail call i32 @sleep(i32 1)
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 13)
%37 = tail call i32 @sleep(i32 1)
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 12)
%39 = tail call i32 @sleep(i32 1)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 11)
%41 = tail call i32 @sleep(i32 1)
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 10)
%43 = tail call i32 @sleep(i32 1)
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 9)
%45 = tail call i32 @sleep(i32 1)
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 8)
%47 = tail call i32 @sleep(i32 1)
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 7)
%49 = tail call i32 @sleep(i32 1)
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 6)
%51 = tail call i32 @sleep(i32 1)
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 5)
%53 = tail call i32 @sleep(i32 1)
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 4)
%55 = tail call i32 @sleep(i32 1)
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%57 = tail call i32 @sleep(i32 1)
%58 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 2)
%59 = tail call i32 @sleep(i32 1)
%60 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 1)
%61 = tail call i32 @sleep(i32 1)
%62 = tail call i32 @fclose(%struct.TYPE_4__* %9)
%63 = load i32, i32* %14
%64 = load i32, i32* @LOCK_UN
%65 = tail call i64 @flock(i32 %63, i32 %64)
ret i32 0
}