#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;
};
typedef int /*<<< orphan*/ lock;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ printout(char *);
int main(int argc, char *argv[], char *envp[]) {
  char *filename = argv[1];
  int fd;
  struct flock lock;
  printout("Apertura");
  if ((fd = open(filename, O_WRONLY)) == -1) {
    printf("Errore in open())\n");
    exit(1);
  }
  memset(&lock, 0, sizeof(lock));
  printout("Sto bloccando....");
  lock.l_type = F_WRLCK;
  fcntl(fd, F_SETLKW, &lock);
  printout("Bloccato....");
  getchar();
  lock.l_type = F_UNLCK;
  fcntl(fd, F_SETLKW, &lock);
  close(fd);
  return (EXIT_SUCCESS);
}


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)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
lea    0xe53(%rip),%rdi
call   1040 <printout@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11e6 <main+0x66>
lea    0xe32(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0xe1f(%rip),%rdi
call   1040 <printout@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
lea    0xe04(%rip),%rdi
call   1040 <printout@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rbx
lea    0xe6c(%rip),%rdi
call   1050 <printout@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     123d <main+0xbd>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0xe39(%rip),%rdi
call   1050 <printout@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dff(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
lea    0xe1e(%rip),%rdi
call   1050 <printout@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32 }
@.str = [9 x i8] c"Apertura\00"
@O_WRONLY = global i32 0
@.str.1 = [19 x i8] c"Errore in open())\0A\00"
@.str.2 = [18 x i8] c"Sto bloccando....\00"
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@.str.3 = [13 x i8] c"Bloccato....\00"
@F_UNLCK = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.flock
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%11 = load i8**, i8*** %6
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
store i8* %13, i8** %8
%14 = call i32 @printout(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%15 = load i8*, i8** %8
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %9
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
%23 = call i32 @memset(%struct.flock* %10, i32 0, i32 4)
%24 = call i32 @printout(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @F_WRLCK
%26 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %9
%28 = load i32, i32* @F_SETLKW
%29 = call i32 @fcntl(i32 %27, i32 %28, %struct.flock* %10)
%30 = call i32 @printout(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 (...) @getchar()
%32 = load i32, i32* @F_UNLCK
%33 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %9
%35 = load i32, i32* @F_SETLKW
%36 = call i32 @fcntl(i32 %34, i32 %35, %struct.flock* %10)
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @EXIT_SUCCESS
ret i32 %39
}

LLVM IR (-O2):
%struct.flock = type { i32 }
@.str = [9 x i8] c"Apertura\00"
@O_WRONLY = global i32 0
@.str.2 = [18 x i8] c"Sto bloccando....\00"
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@.str.3 = [13 x i8] c"Bloccato....\00"
@F_UNLCK = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [18 x i8] c"Errore in open())\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = alloca %struct.flock
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = bitcast %struct.flock* %4 to i8*
%8 = tail call i32 @printout(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @O_WRONLY
%10 = tail call i32 @open(i8* %6, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = call i32 @memset(%struct.flock* %4, i32 0, i32 4)
%17 = call i32 @printout(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @F_SETLKW
%21 = call i32 @fcntl(i32 %10, i32 %20, %struct.flock* %4)
%22 = call i32 @printout(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 (...) @getchar()
%24 = load i32, i32* @F_UNLCK
store i32 %24, i32* %19
%25 = load i32, i32* @F_SETLKW
%26 = call i32 @fcntl(i32 %10, i32 %25, %struct.flock* %4)
%27 = call i32 @close(i32 %10)
%28 = load i32, i32* @EXIT_SUCCESS
ret i32 %28
}