#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 */
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ F_OK;
int LOCK_EX;
int LOCK_NB;
int LOCK_SH;
int LOCK_UN;
int O_CREAT;
int O_RDONLY;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int flock(int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Usage: ./filelock <file> <s/x/n>\n");
    exit(1);
  }
  char *filename;
  int lock;
  int fd, flags;
  int ret;
  if (strlen(argv[1]) > 32) {
    printf("exceed max file len");
    exit(1);
  }
  filename = argv[1];
  if (access(filename, F_OK) < 0) {
    flags = O_CREAT | O_RDONLY;
  } else {
    flags = O_RDONLY;
  }
  fd = open(filename, flags, 0755);
  if (fd == -1) {
    perror("create file fail");
    exit(1);
  }
  lock = (argv[2][0] == 's') ? LOCK_SH : LOCK_EX;
  if (argv[2][1] == 'n') {
    lock |= LOCK_NB;
  }
  printf("argv: %s, %s\n", argv[1], argv[2]);
  printf("request lock... lock:%d\n", lock);
  ret = flock(fd, lock);
  if (ret == -1) {
    if (errno == EWOULDBLOCK) {
      printf("fail get");
      exit(0);
    } else {
      perror("flock fail");
      exit(1);
    }
  }
  printf("get lock\n");
  sleep(10);
  printf("release lock\n");
  flock(fd, LOCK_UN);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b8 <main+0x38>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
cmp    $0x20,%eax
jle    11e6 <main+0x66>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jge    1228 <main+0xa8>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    0x2db2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1234 <main+0xb4>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    $0x1ed,%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1268 <main+0xe8>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x73,%eax
jne    128d <main+0x10d>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1299 <main+0x119>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x6e,%eax
jne    12c3 <main+0x143>
mov    0x2cdd(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1080 <flock@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    134f <main+0x1cf>
mov    0x2caf(%rip),%rax
mov    (%rax),%rax
mov    0x2c9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1339 <main+0x1b9>
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xd37(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2c31(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1292 <main+0x102>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1040 <strlen@plt>
cmp    $0x21,%eax
jge    12a8 <main+0x118>
mov    0x8(%rbx),%rbp
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <access@plt>
mov    0x2e2b(%rip),%rcx
mov    0x2dfc(%rip),%rdx
sar    $0x3f,%rax
and    (%rcx),%eax
or     (%rdx),%eax
mov    %rbp,%rdi
mov    %eax,%esi
mov    $0x1ed,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     12c0 <main+0x130>
mov    %eax,%ebp
mov    0x10(%rbx),%rdx
mov    0x1(%rdx),%cl
cmpb   $0x73,(%rdx)
jne    120b <main+0x7b>
mov    0x2dcf(%rip),%rax
jmp    1212 <main+0x82>
mov    0x2dce(%rip),%rax
mov    0x8(%rbx),%rsi
mov    $0x0,%ebx
cmp    $0x6e,%cl
jne    1229 <main+0x99>
mov    0x2d71(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdf3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1090 <flock@plt>
cmp    $0xffffffff,%eax
je     12c9 <main+0x139>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
lea    0xdf5(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1090 <flock@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd51(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd4d(%rip),%rdi
jmp    12fb <main+0x16b>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2cde(%rip),%rcx
cmp    (%rcx),%rax
jne    12f4 <main+0x164>
lea    0xd66(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd5a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"Usage: ./filelock <file> <s/x/n>\0A\00"
@.str.1 = [20 x i8] c"exceed max file len\00"
@F_OK = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [17 x i8] c"create file fail\00"
@LOCK_SH = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.3 = [14 x i8] c"argv: %s, %s\0A\00"
@.str.4 = [25 x i8] c"request lock... lock:%d\0A\00"
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.5 = [9 x i8] c"fail get\00"
@.str.6 = [11 x i8] c"flock fail\00"
@.str.7 = [10 x i8] c"get lock\0A\00"
@.str.8 = [14 x i8] c"release lock\0A\00"
@LOCK_UN = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @strlen(i8* %19)
%21 = icmp sgt i32 %20, 32
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = load i32, i32* @F_OK
%31 = call i64 @access(i8* %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %25
%34 = load i32, i32* @O_CREAT
%35 = load i32, i32* @O_RDONLY
%36 = or i32 %34, %35
store i32 %36, i32* %9
br label %39
37: ; preds = %25
%38 = load i32, i32* @O_RDONLY
store i32 %38, i32* %9
br label %39
39: ; preds = %37, %33
%40 = load i8*, i8** %6
%41 = load i32, i32* %9
%42 = call i32 @open(i8* %40, i32 %41, i32 493)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %39
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = getelementptr inbounds i8, i8* %51, i64 0
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 115
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = load i32, i32* @LOCK_SH
br label %60
58: ; preds = %48
%59 = load i32, i32* @LOCK_EX
br label %60
60: ; preds = %58, %56
%61 = phi i32 [ %57, %56 ], [ %59, %58 ]
store i32 %61, i32* %7
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = load i8*, i8** %63
%65 = getelementptr inbounds i8, i8* %64, i64 1
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 110
br i1 %68, label %69, label %73
69: ; preds = %60
%70 = load i32, i32* @LOCK_NB
%71 = load i32, i32* %7
%72 = or i32 %71, %70
store i32 %72, i32* %7
br label %73
73: ; preds = %69, %60
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 2
%79 = load i8*, i8** %78
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %76, i8* %79)
%81 = load i32, i32* %7
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %81)
%83 = load i32, i32* %8
%84 = load i32, i32* %7
%85 = call i32 @flock(i32 %83, i32 %84)
store i32 %85, i32* %10
%86 = load i32, i32* %10
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %98
88: ; preds = %73
%89 = load i64, i64* @errno
%90 = load i64, i64* @EWOULDBLOCK
%91 = icmp eq i64 %89, %90
br i1 %91, label %92, label %95
92: ; preds = %88
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%94 = call i32 @exit(i32 0)
unreachable
95: ; preds = %88
%96 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %73
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%100 = call i32 @sleep(i32 10)
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
%102 = load i32, i32* %8
%103 = load i32, i32* @LOCK_UN
%104 = call i32 @flock(i32 %102, i32 %103)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"exceed max file len\00"
@F_OK = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [17 x i8] c"create file fail\00"
@LOCK_SH = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.3 = [14 x i8] c"argv: %s, %s\0A\00"
@.str.4 = [25 x i8] c"request lock... lock:%d\0A\00"
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.5 = [9 x i8] c"fail get\00"
@.str.6 = [11 x i8] c"flock fail\00"
@LOCK_UN = global i32 0
@str = [9 x i8] c"get lock\00"
@str.9 = [13 x i8] c"release lock\00"
@str.10 = [33 x i8] c"Usage: ./filelock <file> <s/x/n>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.10, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strlen(i8* %9)
%11 = icmp sgt i32 %10, 32
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = load i8*, i8** %8
%17 = load i32, i32* @F_OK
%18 = tail call i64 @access(i8* %16, i32 %17)
%19 = icmp slt i64 %18, 0
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_RDONLY
%22 = select i1 %19, i32 %20, i32 0
%23 = or i32 %21, %22
%24 = tail call i32 @open(i8* %16, i32 %23, i32 493)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %15
%30 = getelementptr inbounds i8*, i8** %1, i64 2
%31 = load i8*, i8** %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 115
%34 = load i32, i32* @LOCK_SH
%35 = load i32, i32* @LOCK_EX
%36 = select i1 %33, i32 %34, i32 %35
%37 = getelementptr inbounds i8, i8* %31, i64 1
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 110
%40 = load i32, i32* @LOCK_NB
%41 = select i1 %39, i32 %40, i32 0
%42 = or i32 %41, %36
%43 = load i8*, i8** %8
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %43, i8* %31)
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%46 = tail call i32 @flock(i32 %24, i32 %42)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %58
48: ; preds = %29
%49 = load i64, i64* @errno
%50 = load i64, i64* @EWOULDBLOCK
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%54 = tail call i32 @exit(i32 0)
unreachable
55: ; preds = %48
%56 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%57 = tail call i32 @exit(i32 1)
unreachable
58: ; preds = %29
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%60 = tail call i32 @sleep(i32 10)
%61 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%62 = load i32, i32* @LOCK_UN
%63 = tail call i32 @flock(i32 %24, i32 %62)
ret i32 0
}