#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 */
typedef int /*<<< orphan*/ time_t;
struct flock {
  scalar_t__ l_type;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ const *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char *path = argv[1];
  int fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0666);
  struct flock lock;
  fcntl(fd, F_GETLK, &lock);
  if (lock.l_type != F_UNLCK)
    printf("Locked\n");
  else {
    printf("Unlocked\n");
    lock.l_type = F_WRLCK;
    fcntl(fd, F_SETLK, &lock);
    const time_t t = time(0);
    char *hora = ctime(&t);
    write(fd, hora, strlen(hora));
    sleep(30);
    lock.l_type = F_UNLCK;
    fcntl(fd, F_SETLK, &lock);
  }
  close(fd);
}


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),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x28(%rbp),%rax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
je     1218 <main+0x88>
lea    0xdf2(%rip),%rdi
call   1070 <printf@plt>
jmp    12b0 <main+0x120>
lea    0xde9(%rip),%rdi
call   1070 <printf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <ctime@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    $0x1e,%edi
call   10b0 <sleep@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e0d(%rip),%rcx
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x8(%rsi),%rdi
mov    0x2e3f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
mov    (%rsp),%rax
mov    0x2de9(%rip),%r15
cmp    (%r15),%rax
jne    126b <main+0xdb>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2dd3(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%r14
mov    %ebp,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1060 <ctime@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    $0x1e,%edi
call   10b0 <sleep@plt>
mov    (%r15),%rax
mov    %rax,(%rsp)
mov    (%r12),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
jmp    1277 <main+0xe7>
lea    0xd97(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64 }
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@.str = [8 x i8] c"Locked\0A\00"
@.str.1 = [10 x i8] c"Unlocked\0A\00"
@F_WRLCK = global i64 0
@F_SETLK = 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 %struct.flock
%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
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_APPEND
%17 = or i32 %15, %16
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19, i32 438)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = load i32, i32* @F_GETLK
%23 = call i32 @fcntl(i32 %21, i32 %22, %struct.flock* %8)
%24 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @F_UNLCK
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %2
%29 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %50
30: ; preds = %2
%31 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%32 = load i64, i64* @F_WRLCK
%33 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i64 %32, i64* %33
%34 = load i32, i32* %7
%35 = load i32, i32* @F_SETLK
%36 = call i32 @fcntl(i32 %34, i32 %35, %struct.flock* %8)
%37 = call i32 @time(i32 0)
store i32 %37, i32* %9
%38 = call i8* @ctime(i32* %9)
store i8* %38, i8** %10
%39 = load i32, i32* %7
%40 = load i8*, i8** %10
%41 = load i8*, i8** %10
%42 = call i32 @strlen(i8* %41)
%43 = call i32 @write(i32 %39, i8* %40, i32 %42)
%44 = call i32 @sleep(i32 30)
%45 = load i64, i64* @F_UNLCK
%46 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i64 %45, i64* %46
%47 = load i32, i32* %7
%48 = load i32, i32* @F_SETLK
%49 = call i32 @fcntl(i32 %47, i32 %48, %struct.flock* %8)
br label %50
50: ; preds = %30, %28
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.flock = type { i64 }
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@F_WRLCK = global i64 0
@F_SETLK = global i32 0
@str = [9 x i8] c"Unlocked\00"
@str.2 = [7 x i8] c"Locked\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = alloca i32
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_APPEND
%9 = or i32 %8, %7
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %6, i32 %11, i32 438)
%13 = bitcast %struct.flock* %3 to i8*
%14 = load i32, i32* @F_GETLK
%15 = call i32 @fcntl(i32 %12, i32 %14, %struct.flock* %3)
%16 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @F_UNLCK
%19 = icmp eq i64 %17, %18
br i1 %19, label %22, label %20
20: ; preds = %2
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.2, i64 0, i64 0))
br label %36
22: ; preds = %2
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%24 = load i64, i64* @F_WRLCK
store i64 %24, i64* %16
%25 = load i32, i32* @F_SETLK
%26 = call i32 @fcntl(i32 %12, i32 %25, %struct.flock* %3)
%27 = bitcast i32* %4 to i8*
%28 = call i32 @time(i32 0)
store i32 %28, i32* %4
%29 = call i8* @ctime(i32* %4)
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %12, i8* %29, i32 %30)
%32 = call i32 @sleep(i32 30)
%33 = load i64, i64* @F_UNLCK
store i64 %33, i64* %16
%34 = load i32, i32* @F_SETLK
%35 = call i32 @fcntl(i32 %12, i32 %34, %struct.flock* %3)
br label %36
36: ; preds = %22, %20
%37 = call i32 @close(i32 %12)
ret i32 0
}