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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
void *F_RDLCK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int /*<<< orphan*/);
int read(int, char **, int);
int /*<<< orphan*/ write(int, char **, int);
int main(int argc, char **argv) {
  struct flock fl;
  fl.l_type = F_RDLCK;
  fl.l_whence = SEEK_CUR;
  fl.l_start = 0;
  fl.l_len = 1024;
  int fic = open("/Users/ByTeK/Documents/Cours/L3/S5/SY5/Etienne/TP7/test.txt",
                 O_RDONLY);
  char *buf[1024];
  int rc;
  while (42) {
    fcntl(fic, F_GETLK, &fl);
    if (fl.l_type == F_UNLCK) {
      fl.l_type = F_RDLCK;
      if (fcntl(fic, F_SETLK, &fl) != -1 && (rc = read(fic, buf, 1024)) != 0) {
        write(2, buf, rc);
        fl.l_type = F_UNLCK;
        fcntl(fic, F_GETLK, &fl);
        fl.l_type = F_RDLCK;
      } else
        break;
    } else
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x400,-0x28(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x30(%rbp),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    1269 <main+0x129>
mov    0x2de6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     125f <main+0x11f>
mov    -0x34(%rbp),%edi
lea    -0x2040(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x2044(%rbp)
cmp    $0x0,%eax
je     125f <main+0x11f>
lea    -0x2040(%rbp),%rsi
mov    -0x2044(%rbp),%edx
mov    $0x2,%edi
call   1040 <write@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1264 <main+0x124>
jmp    1273 <main+0x133>
jmp    126e <main+0x12e>
jmp    1273 <main+0x133>
jmp    119a <main+0x5a>
xor    %eax,%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2028,%rsp
mov    0x2e58(%rip),%r12
mov    (%r12),%rax
mov    %rax,(%rsp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movl   $0x400,0x8(%rsp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e34(%rip),%r13
mov    0x0(%r13),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1030 <fcntl@plt>
mov    (%rsp),%rax
mov    0x2e13(%rip),%rbp
cmp    0x0(%rbp),%rax
jne    1248 <main+0x108>
mov    %rsp,%r14
lea    0x20(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rax
mov    %rax,(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1248 <main+0x108>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1248 <main+0x108>
mov    $0x2,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x0(%rbp),%rax
mov    %rax,(%rsp)
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
mov    (%r12),%rax
mov    %rax,(%rsp)
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
mov    (%rsp),%rax
cmp    0x0(%rbp),%rax
je     11d0 <main+0x90>
xor    %eax,%eax
add    $0x2028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i64, i32 }
@F_RDLCK = global i8* null
@SEEK_CUR = global i32 0
@.str = [60 x i8] c"/Users/ByTeK/Documents/Cours/L3/S5/SY5/Etienne/TP7/test.txt\00"
@O_RDONLY = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = 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 %struct.flock
%7 = alloca i32
%8 = alloca [1024 x i8*]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8*, i8** @F_RDLCK
%11 = ptrtoint i8* %10 to i64
%12 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 %11, i64* %12
%13 = load i32, i32* @SEEK_CUR
%14 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i32 1024, i32* %16
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %7
br label %19
19: ; preds = %2, %55
%20 = load i32, i32* %7
%21 = load i32, i32* @F_GETLK
%22 = call i32 @fcntl(i32 %20, i32 %21, %struct.flock* %6)
%23 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @F_UNLCK
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %54
27: ; preds = %19
%28 = load i8*, i8** @F_RDLCK
%29 = ptrtoint i8* %28 to i64
%30 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 %29, i64* %30
%31 = load i32, i32* %7
%32 = load i32, i32* @F_SETLK
%33 = call i32 @fcntl(i32 %31, i32 %32, %struct.flock* %6)
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %52
35: ; preds = %27
%36 = load i32, i32* %7
%37 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %8, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8** %37, i32 1024)
store i32 %38, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %35
%41 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %8, i64 0, i64 0
%42 = load i32, i32* %9
%43 = call i32 @write(i32 2, i8** %41, i32 %42)
%44 = load i64, i64* @F_UNLCK
%45 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 %44, i64* %45
%46 = load i32, i32* %7
%47 = load i32, i32* @F_GETLK
%48 = call i32 @fcntl(i32 %46, i32 %47, %struct.flock* %6)
%49 = load i8*, i8** @F_RDLCK
%50 = ptrtoint i8* %49 to i64
%51 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 %50, i64* %51
br label %53
52: ; preds = %35, %27
br label %56
53: ; preds = %40
br label %55
54: ; preds = %19
br label %56
55: ; preds = %53
br label %19
56: ; preds = %54, %52
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i64, i32 }
@F_RDLCK = global i8* null
@SEEK_CUR = global i32 0
@.str = [60 x i8] c"/Users/ByTeK/Documents/Cours/L3/S5/SY5/Etienne/TP7/test.txt\00"
@O_RDONLY = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@F_SETLK = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.flock
%4 = alloca [1024 x i8*]
%5 = bitcast %struct.flock* %3 to i8*
%6 = load i8*, i8** @F_RDLCK
%7 = ptrtoint i8* %6 to i64
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 %7, i64* %8
%9 = load i32, i32* @SEEK_CUR
%10 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 1024, i32* %12
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = bitcast [1024 x i8*]* %4 to i8*
%16 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %4, i64 0, i64 0
%17 = load i32, i32* @F_GETLK
%18 = call i32 @fcntl(i32 %14, i32 %17, %struct.flock* %3)
%19 = load i64, i64* %8
%20 = load i64, i64* @F_UNLCK
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %43
22: ; preds = %2, %31
%23 = load i8*, i8** @F_RDLCK
%24 = ptrtoint i8* %23 to i64
store i64 %24, i64* %8
%25 = load i32, i32* @F_SETLK
%26 = call i32 @fcntl(i32 %14, i32 %25, %struct.flock* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %43, label %28
28: ; preds = %22
%29 = call i32 @read(i32 %14, i8** %16, i32 1024)
%30 = icmp eq i32 %29, 0
br i1 %30, label %43, label %31
31: ; preds = %28
%32 = call i32 @write(i32 2, i8** %16, i32 %29)
%33 = load i64, i64* @F_UNLCK
store i64 %33, i64* %8
%34 = load i32, i32* @F_GETLK
%35 = call i32 @fcntl(i32 %14, i32 %34, %struct.flock* %3)
%36 = load i8*, i8** @F_RDLCK
%37 = ptrtoint i8* %36 to i64
store i64 %37, i64* %8
%38 = load i32, i32* @F_GETLK
%39 = call i32 @fcntl(i32 %14, i32 %38, %struct.flock* %3)
%40 = load i64, i64* %8
%41 = load i64, i64* @F_UNLCK
%42 = icmp eq i64 %40, %41
br i1 %42, label %22, label %43
43: ; preds = %28, %22, %31, %2
ret i32 0
}