#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_len;
  int /*<<< orphan*/ l_start;
  void *l_whence;
  void *l_type;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
void *F_RDLCK;
int /*<<< orphan*/ F_SETLK;
void *F_WRLCK;
int /*<<< orphan*/ O_RDWR;
void *SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  int fd;
  struct flock fl;
  if (argc < 3 || strcmp(argv[1], "--help") == 0) {
    fprintf(stderr, "Usage: %s file [option]\n", argv[0]);
    fprintf(stderr, "       -w    lock write\n");
    fprintf(stderr, "       -r    lock read\n");
    fprintf(stderr, "       -u    unlock\n");
    exit(EXIT_FAILURE);
  }
  if ((fd = open(argv[1], O_RDWR)) == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  switch (argv[2][1]) {
  case 'w':
    fl.l_type = F_WRLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;
    break;
  case 'r':
    fl.l_type = F_RDLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;
    break;
  case 'u':
    fl.l_type = F_RDLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;
    break;
  }
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("fcntl set lock");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     119e <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe71(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    120f <main+0xaf>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    124b <main+0xeb>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl 0x1(%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0x72,%eax
je     12b9 <main+0x159>
jmp    1268 <main+0x108>
mov    -0x34(%rbp),%eax
sub    $0x75,%eax
je     12e8 <main+0x188>
jmp    1279 <main+0x119>
mov    -0x34(%rbp),%eax
sub    $0x77,%eax
jne    1312 <main+0x1b2>
jmp    128a <main+0x12a>
mov    0x2d27(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d29(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
jmp    1312 <main+0x1b2>
mov    0x2ce0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2cfa(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
jmp    1312 <main+0x1b2>
mov    0x2cb1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2ccb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cc4(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    134a <main+0x1ea>
lea    0xd34(%rip),%rdi
call   1070 <perror@plt>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2c5f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jl     1184 <main+0x24>
mov    0x8(%rbx),%rdi
lea    0xe86(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11d7 <main+0x77>
mov    0x2e45(%rip),%r14
mov    (%r14),%edi
mov    (%rbx),%rdx
lea    0xe6f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xe77(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xe7f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xe86(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1261 <main+0x101>
mov    0x8(%rbx),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
jne    11f7 <main+0x97>
lea    0xe71(%rip),%rdi
jmp    125c <main+0xfc>
mov    0x10(%rbx),%rcx
movsbl 0x1(%rcx),%ecx
cmp    $0x72,%ecx
je     1217 <main+0xb7>
cmp    $0x75,%ecx
je     1217 <main+0xb7>
cmp    $0x77,%ecx
jne    123d <main+0xdd>
mov    0x2da3(%rip),%rcx
jmp    121e <main+0xbe>
mov    0x2d82(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,0x10(%rsp)
mov    0x2d9b(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    126f <main+0x10f>
lea    0xe0f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i8*, i8* }
@.str = [7 x i8] c"--help\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Usage: %s file [option]\0A\00"
@.str.2 = [25 x i8] c" -w lock write\0A\00"
@.str.3 = [24 x i8] c" -r lock read\0A\00"
@.str.4 = [21 x i8] c" -u unlock\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.5 = [5 x i8] c"open\00"
@F_WRLCK = global i8* null
@SEEK_SET = global i8* null
@F_RDLCK = global i8* null
@F_SETLK = global i32 0
@.str.6 = [15 x i8] c"fcntl set lock\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.flock
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 3
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %10, %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %10
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %6
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %30
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
switch i32 %47, label %69 [
i32 119, label %48
i32 114, label %55
i32 117, label %62
]
48: ; preds = %41
%49 = load i8*, i8** @F_WRLCK
%50 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i8* %49, i8** %50
%51 = load i8*, i8** @SEEK_SET
%52 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i8* %51, i8** %52
%53 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 0, i32* %53
%54 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 0, i32* %54
br label %69
55: ; preds = %41
%56 = load i8*, i8** @F_RDLCK
%57 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i8* %56, i8** %57
%58 = load i8*, i8** @SEEK_SET
%59 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i8* %58, i8** %59
%60 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 0, i32* %60
%61 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 0, i32* %61
br label %69
62: ; preds = %41
%63 = load i8*, i8** @F_RDLCK
%64 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i8* %63, i8** %64
%65 = load i8*, i8** @SEEK_SET
%66 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i8* %65, i8** %66
%67 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 0, i32* %67
%68 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 0, i32* %68
br label %69
69: ; preds = %41, %62, %55, %48
%70 = load i32, i32* %6
%71 = load i32, i32* @F_SETLK
%72 = call i32 @fcntl(i32 %70, i32 %71, %struct.flock* %7)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %78
74: ; preds = %69
%75 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %69
%79 = load i32, i32* @EXIT_SUCCESS
%80 = call i32 @exit(i32 %79)
unreachable
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i8*, i8* }
@.str = [7 x i8] c"--help\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Usage: %s file [option]\0A\00"
@.str.2 = [25 x i8] c" -w lock write\0A\00"
@.str.3 = [24 x i8] c" -r lock read\0A\00"
@.str.4 = [21 x i8] c" -u unlock\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.5 = [5 x i8] c"open\00"
@F_WRLCK = global i8* null
@SEEK_SET = global i8* null
@F_RDLCK = global i8* null
@F_SETLK = global i32 0
@.str.6 = [15 x i8] c"fcntl set lock\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %6, %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %6
%24 = load i8*, i8** %7
%25 = load i32, i32* @O_RDWR
%26 = tail call i32 @open(i8* %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
%33 = getelementptr inbounds i8*, i8** %1, i64 2
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
switch i32 %37, label %59 [
i32 119, label %38
i32 114, label %45
i32 117, label %52
]
38: ; preds = %32
%39 = load i8*, i8** @F_WRLCK
%40 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i8* %39, i8** %40
%41 = load i8*, i8** @SEEK_SET
%42 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %41, i8** %42
%43 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 0, i32* %43
%44 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 0, i32* %44
br label %59
45: ; preds = %32
%46 = load i8*, i8** @F_RDLCK
%47 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i8* %46, i8** %47
%48 = load i8*, i8** @SEEK_SET
%49 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %48, i8** %49
%50 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 0, i32* %50
%51 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 0, i32* %51
br label %59
52: ; preds = %32
%53 = load i8*, i8** @F_RDLCK
%54 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i8* %53, i8** %54
%55 = load i8*, i8** @SEEK_SET
%56 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %55, i8** %56
%57 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 0, i32* %57
%58 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 0, i32* %58
br label %59
59: ; preds = %32, %52, %45, %38
%60 = load i32, i32* @F_SETLK
%61 = call i32 @fcntl(i32 %26, i32 %60, %struct.flock* %3)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %59
%68 = load i32, i32* @EXIT_SUCCESS
%69 = call i32 @exit(i32 %68)
unreachable
}