#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 flock {
  int /*<<< orphan*/ l_type;
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ system(char *);
int main(int argc, char const *argv[]) {
  struct flock lock;
  int fd;
  if (-1 == (fd = open(argv[1], O_RDWR))) {
    perror(argv[1]);
    exit(1);
  }
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  if (-1 == fcntl(fd, F_SETLK, &lock)) {
    if (EACCES == errno || EAGAIN == errno) {
      printf("%s is locked.", argv[1]);
      exit(1);
    }
  }
  int vim_length = strlen("vim");
  int arg_lenth = strlen(argv[1]);
  char *command = malloc(sizeof(char) * (arg_lenth + vim_length + 2));
  snprintf(command, arg_lenth + vim_length + 2, "vim %s", argv[1]);
  system(command);
  lock.l_type = F_UNLCK;
  fcntl(fd, F_SETLK, &lock);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11f5 <main+0x55>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1298 <main+0xf8>
mov    0x2d88(%rip),%rax
mov    (%rax),%rax
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%rax
je     1275 <main+0xd5>
mov    0x2d3e(%rip),%rax
mov    (%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
jne    1293 <main+0xf3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd7c(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1298 <main+0xf8>
lea    0xd6f(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x38(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
add    -0x38(%rbp),%esi
add    $0x2,%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd25(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x48(%rbp),%rdi
call   1050 <system@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12a8 <main+0x108>
mov    %eax,%r15d
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2deb(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1233 <main+0x93>
mov    0x2dc0(%rip),%rcx
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
cmp    %rax,(%rcx)
je     12bb <main+0x11b>
mov    0x2d76(%rip),%rcx
cmp    %rax,(%rcx)
je     12bb <main+0x11b>
lea    0xdd4(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x8(%r14),%rdi
call   1040 <strlen@plt>
add    %eax,%ebx
add    $0x2,%ebx
mov    %ebx,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    0x8(%r14),%rcx
lea    0xdae(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
call   1070 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    (%r12),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1030 <fcntl@plt>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r14),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x8(%r14),%rsi
lea    0xd3a(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i32 }
@O_RDWR = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@EACCES = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"%s is locked.\00"
@.str.1 = [4 x i8] c"vim\00"
@.str.2 = [7 x i8] c"vim %s\00"
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.flock
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @perror(i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i32, i32* @F_WRLCK
%25 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @SEEK_SET
%27 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i64 0, i64* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %29
%30 = load i32, i32* %7
%31 = load i32, i32* @F_SETLK
%32 = call i32 @fcntl(i32 %30, i32 %31, %struct.flock* %6)
%33 = icmp eq i32 -1, %32
br i1 %33, label %34, label %49
34: ; preds = %23
%35 = load i64, i64* @EACCES
%36 = load i64, i64* @errno
%37 = icmp eq i64 %35, %36
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i64, i64* @EAGAIN
%40 = load i64, i64* @errno
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %38, %34
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %38
br label %49
49: ; preds = %48, %23
%50 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32 %50, i32* %8
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i32 @strlen(i8* %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = load i32, i32* %8
%57 = add nsw i32 %55, %56
%58 = add nsw i32 %57, 2
%59 = sext i32 %58 to i64
%60 = mul i64 1, %59
%61 = trunc i64 %60 to i32
%62 = call i8* @malloc(i32 %61)
store i8* %62, i8** %10
%63 = load i8*, i8** %10
%64 = load i32, i32* %9
%65 = load i32, i32* %8
%66 = add nsw i32 %64, %65
%67 = add nsw i32 %66, 2
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = call i32 @snprintf(i8* %63, i32 %67, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %70)
%72 = load i8*, i8** %10
%73 = call i32 @system(i8* %72)
%74 = load i32, i32* @F_UNLCK
%75 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %74, i32* %75
%76 = load i32, i32* %7
%77 = load i32, i32* @F_SETLK
%78 = call i32 @fcntl(i32 %76, i32 %77, %struct.flock* %6)
%79 = load i32, i32* %7
%80 = call i32 @close(i32 %79)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32 }
@O_RDWR = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@EACCES = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"%s is locked.\00"
@.str.1 = [4 x i8] c"vim\00"
@.str.2 = [7 x i8] c"vim %s\00"
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = tail call i32 @perror(i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* @F_WRLCK
%16 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @SEEK_SET
%18 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%20 = bitcast i64* %19 to i8*
%21 = load i32, i32* @F_SETLK
%22 = call i32 @fcntl(i32 %8, i32 %21, %struct.flock* %3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %35
24: ; preds = %14
%25 = load i64, i64* @EACCES
%26 = load i64, i64* @errno
%27 = icmp eq i64 %25, %26
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %28, %26
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i8*, i8** %5
%33 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %24, %14
%36 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%37 = load i8*, i8** %5
%38 = call i32 @strlen(i8* %37)
%39 = add i32 %36, 2
%40 = add i32 %39, %38
%41 = call i8* @malloc(i32 %40)
%42 = load i8*, i8** %5
%43 = call i32 @snprintf(i8* %41, i32 %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = call i32 @system(i8* %41)
%45 = load i32, i32* @F_UNLCK
store i32 %45, i32* %16
%46 = load i32, i32* @F_SETLK
%47 = call i32 @fcntl(i32 %8, i32 %46, %struct.flock* %3)
%48 = call i32 @close(i32 %8)
ret i32 0
}