#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 */

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ Message(char *, int, char *);
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ flock(int, int /*<<< orphan*/);
int open(char *, int);
int FlockFile(char *filename) {
  int fh = 0;
  if ((fh = open(filename, O_WRONLY | O_CREAT | O_APPEND)) != (-1)) {
    if (flock(fh, LOCK_EX) == (0)) {
      return (fh);
    }
  }
  Message("simq", 3, "Cannot create lock file");
  return (-1);
}


x86 assembly (clang, -O0):
<FlockFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2ea1(%rip),%rax
or     (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     119d <FlockFile+0x6d>
mov    -0x14(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1050 <flock@plt>
cmp    $0x0,%eax
jne    1198 <FlockFile+0x68>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <FlockFile+0x8c>
jmp    119d <FlockFile+0x6d>
lea    0xe5c(%rip),%rdi
mov    $0x3,%esi
lea    0xe55(%rip),%rdx
call   1030 <Message@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FlockFile>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2eb9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     116c <FlockFile+0x3c>
mov    %eax,%ebx
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <flock@plt>
test   %eax,%eax
je     1189 <FlockFile+0x59>
lea    0xe8d(%rip),%rdi
lea    0xe8b(%rip),%rdx
mov    $0x3,%esi
call   1030 <Message@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@LOCK_EX = global i32 0
@.str = [5 x i8] c"simq\00"
@.str.1 = [24 x i8] c"Cannot create lock file\00"
define i32 @FlockFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_APPEND
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10)
store i32 %11, i32* %4
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %21
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i32, i32* @LOCK_EX
%16 = call i32 @flock(i32 %14, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %23
20: ; preds = %13
br label %21
21: ; preds = %20, %1
%22 = call i32 @Message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 3, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@LOCK_EX = global i32 0
@.str = [5 x i8] c"simq\00"
@.str.1 = [24 x i8] c"Cannot create lock file\00"
define i32 @FlockFile(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_APPEND
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @LOCK_EX
%11 = tail call i32 @flock(i32 %7, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9, %1
%14 = tail call i32 @Message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 3, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %9, %13
%16 = phi i32 [ -1, %13 ], [ %7, %9 ]
ret i32 %16
}