#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 */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ fchmod(int /*<<< orphan*/, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int flock(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
FILE *filelock_accuire(char *lockfile) {
  FILE *f;
  f = fopen(lockfile, "a+");
  if (!f)
    return 0;
  fchmod(fileno(f), 0666);
  if (flock(fileno(f), LOCK_EX) == -1) {
    fclose(f);
    return 0;
  }
  return f;
}


x86 assembly (clang, -O0):
<filelock_accuire>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <filelock_accuire+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11dd <filelock_accuire+0x8d>
mov    -0x18(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    $0x1b6,%esi
call   1050 <fchmod@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
jne    11d5 <filelock_accuire+0x85>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    11dd <filelock_accuire+0x8d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filelock_accuire>:
push   %rbx
lea    0xea8(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119e <filelock_accuire+0x4e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    $0x1b6,%esi
call   1050 <fchmod@plt>
mov    %rbx,%rdi
call   1040 <fileno@plt>
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
jne    11a0 <filelock_accuire+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"a+\00"
@LOCK_EX = global i32 0
define i32* @filelock_accuire(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32* null, i32** %2
br label %24
10: ; preds = %1
%11 = load i32*, i32** %4
%12 = call i32 @fileno(i32* %11)
%13 = call i32 @fchmod(i32 %12, i32 438)
%14 = load i32*, i32** %4
%15 = call i32 @fileno(i32* %14)
%16 = load i32, i32* @LOCK_EX
%17 = call i32 @flock(i32 %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
store i32* null, i32** %2
br label %24
22: ; preds = %10
%23 = load i32*, i32** %4
store i32* %23, i32** %2
br label %24
24: ; preds = %22, %19, %9
%25 = load i32*, i32** %2
ret i32* %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"a+\00"
@LOCK_EX = global i32 0
define noalias i32* @filelock_accuire(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = tail call i32 @fileno(i32* %2)
%6 = tail call i32 @fchmod(i32 %5, i32 438)
%7 = tail call i32 @fileno(i32* %2)
%8 = load i32, i32* @LOCK_EX
%9 = tail call i32 @flock(i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32 @fclose(i32* %2)
br label %13
13: ; preds = %4, %1, %11
%14 = phi i32* [ null, %11 ], [ null, %1 ], [ %2, %4 ]
ret i32* %14
}