#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*/ FILE;

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int fileno(int /*<<< orphan*/ *);
int flock(int, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int main(int argc, char **argv) {
  char c;
  FILE *file1 = fopen("test.flock.c", "r");
  int fd1 = fileno(file1);
  int result1 = flock(fd1, LOCK_EX | LOCK_NB);
  int result2 = flock(fd1, LOCK_EX | LOCK_NB);
  printf("flock1 = %d\n", result1);
  printf("flock2 = %d\n", result2);
  FILE *file2 = fopen("test.flock.c", "r");
  int fd2 = fileno(file2);
  int result3 = flock(fd2, LOCK_EX | LOCK_NB);
  printf("flock3 = %d\n", result3);
  printf("Press return...");
  scanf("%c", &c);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xea0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%esi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe1b(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%esi
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe02(%rip),%rdi
lea    -0x11(%rbp),%rsi
call   1050 <scanf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%r15
lea    0xea4(%rip),%r14
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rdi
call   1040 <fileno@plt>
mov    %eax,%ebp
mov    0x2e63(%rip),%r12
mov    0x2e44(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %eax,%edi
call   1070 <flock@plt>
mov    %eax,%ebx
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
mov    %eax,%ebp
lea    0xe5a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe57(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rdi
call   1040 <fileno@plt>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %eax,%edi
call   1070 <flock@plt>
lea    0xe32(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe2f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe31(%rip),%rdi
lea    0x7(%rsp),%rsi
call   1050 <scanf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"test.flock.c\00"
@.str.1 = [2 x i8] c"r\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [13 x i8] c"flock1 = %d\0A\00"
@.str.3 = [13 x i8] c"flock2 = %d\0A\00"
@.str.4 = [13 x i8] c"flock3 = %d\0A\00"
@.str.5 = [16 x i8] c"Press return...\00"
@.str.6 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%13 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = call i32 @fileno(i32* %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = load i32, i32* @LOCK_EX
%18 = load i32, i32* @LOCK_NB
%19 = or i32 %17, %18
%20 = call i32 @flock(i32 %16, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %7
%22 = load i32, i32* @LOCK_EX
%23 = load i32, i32* @LOCK_NB
%24 = or i32 %22, %23
%25 = call i32 @flock(i32 %21, i32 %24)
store i32 %25, i32* %9
%26 = load i32, i32* %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %9
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %10
%31 = load i32*, i32** %10
%32 = call i32 @fileno(i32* %31)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = load i32, i32* @LOCK_EX
%35 = load i32, i32* @LOCK_NB
%36 = or i32 %34, %35
%37 = call i32 @flock(i32 %33, i32 %36)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %5)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"test.flock.c\00"
@.str.1 = [2 x i8] c"r\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [13 x i8] c"flock1 = %d\0A\00"
@.str.3 = [13 x i8] c"flock2 = %d\0A\00"
@.str.4 = [13 x i8] c"flock3 = %d\0A\00"
@.str.5 = [16 x i8] c"Press return...\00"
@.str.6 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @fileno(i32* %4)
%6 = load i32, i32* @LOCK_EX
%7 = load i32, i32* @LOCK_NB
%8 = or i32 %7, %6
%9 = tail call i32 @flock(i32 %5, i32 %8)
%10 = load i32, i32* @LOCK_EX
%11 = load i32, i32* @LOCK_NB
%12 = or i32 %11, %10
%13 = tail call i32 @flock(i32 %5, i32 %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @fileno(i32* %16)
%18 = load i32, i32* @LOCK_EX
%19 = load i32, i32* @LOCK_NB
%20 = or i32 %19, %18
%21 = tail call i32 @flock(i32 %17, i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %3)
ret i32 0
}