#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 timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
scalar_t__ flock(int, int);
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int check_file_busy(const char *lock_file) {
  int fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC, 0644);
  if (fd < 0) {
    do {
      struct timeval start;
      gettimeofday(&start, NULL);
      printf("pid [%5d] : %d.%d ", getpid(), (int)start.tv_sec,
             (int)start.tv_usec);
      printf("open file faild");
      printf("\n");
    } while (0);
    return -1;
  }
  if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<check_file_busy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
mov    0x2e77(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ef <check_file_busy+0x9f>
jmp    1197 <check_file_busy+0x47>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <check_file_busy+0xd6>
mov    -0x14(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
cmp    $0x0,%rax
jne    121f <check_file_busy+0xcf>
movl   $0x0,-0x4(%rbp)
jmp    1226 <check_file_busy+0xd6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_file_busy>:
sub    $0x18,%rsp
mov    0x2e6d(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e73(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11b6 <check_file_busy+0x56>
mov    0x2e3c(%rip),%rcx
mov    0x2e1d(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1080 <flock@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    $0x18,%rsp
ret
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%edx
lea    0xe28(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe2b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [19 x i8] c"pid [%5d] : %d.%d \00"
@.str.1 = [16 x i8] c"open file faild\00"
@.str.2 = [2 x i8] c"\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
define i32 @check_file_busy(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.timeval
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11, i32 420)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %1
br label %16
16: ; preds = %15
%17 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%18 = call i32 (...) @getpid()
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%20 = load i64, i64* %19
%21 = trunc i64 %20 to i32
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%23 = load i64, i64* %22
%24 = trunc i64 %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %21, i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %16
store i32 -1, i32* %2
br label %38
29: ; preds = %1
%30 = load i32, i32* %4
%31 = load i32, i32* @LOCK_EX
%32 = load i32, i32* @LOCK_NB
%33 = or i32 %31, %32
%34 = call i64 @flock(i32 %30, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 0, i32* %2
br label %38
37: ; preds = %29
store i32 1, i32* %2
br label %38
38: ; preds = %37, %36, %28
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [19 x i8] c"pid [%5d] : %d.%d \00"
@.str.1 = [16 x i8] c"open file faild\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
define i32 @check_file_busy(i8* nocapture %0) {
%2 = alloca %struct.timeval
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7, i32 420)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %1
%11 = bitcast %struct.timeval* %2 to i8*
%12 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%13 = tail call i32 (...) @getpid()
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%15 = load i64, i64* %14
%16 = trunc i64 %15 to i32
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16, i32 %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @putchar(i32 10)
br label %30
23: ; preds = %1
%24 = load i32, i32* @LOCK_EX
%25 = load i32, i32* @LOCK_NB
%26 = or i32 %25, %24
%27 = tail call i64 @flock(i32 %8, i32 %26)
%28 = icmp ne i64 %27, 0
%29 = zext i1 %28 to i32
br label %30
30: ; preds = %23, %10
%31 = phi i32 [ -1, %10 ], [ %29, %23 ]
ret i32 %31
}