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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
void *SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ lseek(int, long, void *);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  struct flock fl;
  char *filename = "test.txt";
  if ((fd = open(filename, O_RDWR | O_CREAT)) == -1) {
    fprintf(stderr, "Uso: %s 'filename'\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 4;
  fl.l_len = 2;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    printf("Bloccato...");
  } else {
    getchar();
    lseek(fd, (long)2 * sizeof(char), SEEK_SET);
    write(fd, "*", 2);
    fl.l_type = F_UNLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 4;
    fl.l_len = 2;
  }
  close(fd);
  return (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)
lea    0xe53(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1202 <main+0x72>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1a(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x4,-0x30(%rbp)
movl   $0x2,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1259 <main+0xc9>
lea    0xdce(%rip),%rdi
call   1050 <printf@plt>
jmp    12b3 <main+0x123>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    -0x14(%rbp),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%rdx
mov    $0x2,%esi
call   1060 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    0xda8(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d1a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x4,-0x30(%rbp)
movl   $0x2,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    0x2e3d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4b(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1275 <main+0xe5>
mov    %eax,%ebx
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2ddf(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
movabs $0x200000004,%r15
mov    %r15,0x8(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     124f <main+0xbf>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    (%r14),%rdx
mov    $0x2,%esi
mov    %ebx,%edi
call   1060 <lseek@plt>
lea    0xe02(%rip),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
mov    %r15,0x8(%rsp)
jmp    125b <main+0xcb>
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd81(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i8*, i32 }
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"Uso: %s 'filename'\0A\00"
@EXIT_FAILURE = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.2 = [12 x i8] c"Bloccato...\00"
@.str.3 = [2 x i8] c"*\00"
@F_UNLCK = global i32 0
@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
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %8
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i32, i32* @F_WRLCK
%25 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i32 %24, i32* %25
%26 = load i8*, i8** @SEEK_SET
%27 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i8* %26, i8** %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 4, i32* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 2, i32* %29
%30 = load i32, i32* %6
%31 = load i32, i32* @F_SETLK
%32 = call i32 @fcntl(i32 %30, i32 %31, %struct.flock* %7)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %49
36: ; preds = %23
%37 = call i32 (...) @getchar()
%38 = load i32, i32* %6
%39 = load i8*, i8** @SEEK_SET
%40 = call i32 @lseek(i32 %38, i64 2, i8* %39)
%41 = load i32, i32* %6
%42 = call i32 @write(i32 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
%43 = load i32, i32* @F_UNLCK
%44 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i32 %43, i32* %44
%45 = load i8*, i8** @SEEK_SET
%46 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i8* %45, i8** %46
%47 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 4, i32* %47
%48 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 2, i32* %48
br label %49
49: ; preds = %36, %34
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @EXIT_SUCCESS
ret i32 %52
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i8*, i32 }
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"Uso: %s 'filename'\0A\00"
@EXIT_FAILURE = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.2 = [12 x i8] c"Bloccato...\00"
@.str.3 = [2 x i8] c"*\00"
@F_UNLCK = global i32 0
@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 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i32, i32* @F_WRLCK
%18 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %17, i32* %18
%19 = load i8*, i8** @SEEK_SET
%20 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %19, i8** %20
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 4, i32* %21
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 2, i32* %22
%23 = load i32, i32* @F_SETLK
%24 = call i32 @fcntl(i32 %8, i32 %23, %struct.flock* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %35
28: ; preds = %16
%29 = call i32 (...) @getchar()
%30 = load i8*, i8** @SEEK_SET
%31 = call i32 @lseek(i32 %8, i64 2, i8* %30)
%32 = call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
%33 = load i32, i32* @F_UNLCK
store i32 %33, i32* %18
%34 = load i8*, i8** @SEEK_SET
store i8* %34, i8** %20
store i32 4, i32* %21
store i32 2, i32* %22
br label %35
35: ; preds = %28, %26
%36 = call i32 @close(i32 %8)
%37 = load i32, i32* @EXIT_SUCCESS
ret i32 %37
}