#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 check_file_busy(char const *);
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int do_check_file(const char *lock_file) {
  int ret = check_file_busy(lock_file);
  if (ret < 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);
  } else if (ret > 0) {
    do {
      struct timeval start;
      gettimeofday(&start, NULL);
      printf("pid [%5d] : %d.%d ", getpid(), (int)start.tv_sec,
             (int)start.tv_usec);
      printf("file [%s] is busy", lock_file);
      printf("\n");
    } while (0);
  } else {
    do {
      struct timeval start;
      gettimeofday(&start, NULL);
      printf("pid [%5d] : %d.%d ", getpid(), (int)start.tv_sec,
             (int)start.tv_usec);
      printf("lock file [%s] success", lock_file);
      printf("\n");
    } while (0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_check_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <check_file_busy@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b8 <do_check_file+0x78>
jmp    1167 <do_check_file+0x27>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127b <do_check_file+0x13b>
cmpl   $0x0,-0xc(%rbp)
jle    121c <do_check_file+0xdc>
jmp    11c7 <do_check_file+0x87>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x30(%rbp),%rax
mov    %eax,%ecx
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1276 <do_check_file+0x136>
jmp    1221 <do_check_file+0xe1>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
mov    -0x40(%rbp),%rax
mov    %eax,%ecx
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1276 <do_check_file+0x136>
jmp    127b <do_check_file+0x13b>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_check_file>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <check_file_busy@plt>
mov    %rsp,%rdi
test   %eax,%eax
js     1194 <do_check_file+0x44>
je     11c9 <do_check_file+0x79>
xor    %esi,%esi
call   1070 <gettimeofday@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    (%rsp),%ecx
mov    0x8(%rsp),%edx
lea    0xe7e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe91(%rip),%rdi
jmp    11f5 <do_check_file+0xa5>
xor    %esi,%esi
call   1070 <gettimeofday@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    (%rsp),%ecx
mov    0x8(%rsp),%edx
lea    0xe50(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11ff <do_check_file+0xaf>
xor    %esi,%esi
call   1070 <gettimeofday@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    (%rsp),%ecx
mov    0x8(%rsp),%edx
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe40(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.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"
@.str.3 = [18 x i8] c"file [%s] is busy\00"
@.str.4 = [23 x i8] c"lock file [%s] success\00"
define i32 @do_check_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @check_file_busy(i8* %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %1
br label %12
12: ; preds = %11
%13 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%14 = call i32 (...) @getpid()
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %17, i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %12
br label %59
25: ; preds = %1
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %25
br label %29
29: ; preds = %28
%30 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%31 = call i32 (...) @getpid()
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%33 = load i64, i64* %32
%34 = trunc i64 %33 to i32
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %31, i32 %34, i32 %37)
%39 = load i8*, i8** %2
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %29
br label %58
43: ; preds = %25
br label %44
44: ; preds = %43
%45 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%46 = call i32 (...) @getpid()
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%48 = load i64, i64* %47
%49 = trunc i64 %48 to i32
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%51 = load i64, i64* %50
%52 = trunc i64 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %49, i32 %52)
%54 = load i8*, i8** %2
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %44
br label %58
58: ; preds = %57, %42
br label %59
59: ; preds = %58, %24
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [19 x i8] c"pid [%5d] : %d.%d \00"
@.str.1 = [16 x i8] c"open file faild\00"
@.str.3 = [18 x i8] c"file [%s] is busy\00"
@.str.4 = [23 x i8] c"lock file [%s] success\00"
define i32 @do_check_file(i8* %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = tail call i32 @check_file_busy(i8* %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %1
%8 = bitcast %struct.timeval* %2 to i8*
%9 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%10 = tail call i32 (...) @getpid()
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%15 = load i64, i64* %14
%16 = trunc i64 %15 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %13, i32 %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @putchar(i32 10)
br label %48
20: ; preds = %1
%21 = icmp eq i32 %5, 0
br i1 %21, label %35, label %22
22: ; preds = %20
%23 = bitcast %struct.timeval* %3 to i8*
%24 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%25 = tail call i32 (...) @getpid()
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%27 = load i64, i64* %26
%28 = trunc i64 %27 to i32
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%30 = load i64, i64* %29
%31 = trunc i64 %30 to i32
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %28, i32 %31)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%34 = tail call i32 @putchar(i32 10)
br label %48
35: ; preds = %20
%36 = bitcast %struct.timeval* %4 to i8*
%37 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%38 = tail call i32 (...) @getpid()
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i32
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%43 = load i64, i64* %42
%44 = trunc i64 %43 to i32
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %38, i32 %41, i32 %44)
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%47 = tail call i32 @putchar(i32 10)
br label %48
48: ; preds = %22, %35, %7
ret i32 0
}