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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int S_IRGRP;
int S_ISGID;
int S_IWGRP;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getchar();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ setLock(int, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd;
  chmod("dummy_file.txt", S_ISGID | S_IRGRP | S_IWGRP);
  if ((fd = open("dummy_file.txt", O_RDWR)) < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  if (setLock(fd, F_RDLCK) == 0) {
    printf("Read lock is set.  Open another terminal window and run\n");
    printf("non_locker.  Don't hit ENTER in this terminal window until\n");
    printf("non_locker tells you to do so.\n");
    getchar();
  } else {
    printf("Unable to set read lock.  Program is terminating.\n");
    close(fd);
    exit(EXIT_FAILURE);
  }
  if (setLock(fd, F_UNLCK) == 0)
    printf("Read lock has been released\n\n");
  else {
    printf("Unable to release read lock.  Program is terminating.\n");
    close(fd);
    exit(EXIT_FAILURE);
  }
  if (setLock(fd, F_WRLCK) == 0) {
    printf("Write lock is set.  Switch to the non_locker terminal and hit "
           "ENTER.\n");
    getchar();
  } else {
    printf("Unable to set write lock.  Program is terminating.\n");
    close(fd);
    exit(EXIT_FAILURE);
  }
  if (setLock(fd, F_UNLCK) == 0)
    printf("Write lock has been released\n\n");
  else {
    printf("Unable to release write lock.  Program is terminating.\n");
    close(fd);
    exit(EXIT_FAILURE);
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
call   1060 <chmod@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f8 <main+0x78>
lea    0xe2a(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
call   1090 <setLock@plt>
cmp    $0x0,%rax
jne    1243 <main+0xc3>
lea    0xdfa(%rip),%rdi
call   1030 <printf@plt>
lea    0xe27(%rip),%rdi
call   1030 <printf@plt>
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
jmp    1265 <main+0xe5>
lea    0xe5f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
call   1090 <setLock@plt>
cmp    $0x0,%rax
jne    1291 <main+0x111>
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
jmp    12b3 <main+0x133>
lea    0xe62(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
call   1090 <setLock@plt>
cmp    $0x0,%rax
jne    12e6 <main+0x166>
lea    0xe5c(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
jmp    1308 <main+0x188>
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2cc6(%rip),%rax
mov    (%rax),%esi
call   1090 <setLock@plt>
cmp    $0x0,%rax
jne    1334 <main+0x1b4>
lea    0xe81(%rip),%rdi
call   1030 <printf@plt>
jmp    1356 <main+0x1d6>
lea    0xe8f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2c69(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2c43(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e3d(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
lea    0xe5a(%rip),%rbx
mov    %rbx,%rdi
call   1060 <chmod@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     128a <main+0x10a>
mov    %eax,%ebx
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <setLock@plt>
test   %rax,%rax
jne    1298 <main+0x118>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    0x2dc4(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1090 <setLock@plt>
test   %rax,%rax
jne    12a1 <main+0x121>
lea    0xee4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <setLock@plt>
test   %rax,%rax
jne    12aa <main+0x12a>
lea    0xf13(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    (%r14),%esi
mov    %ebx,%edi
call   1090 <setLock@plt>
test   %rax,%rax
jne    12b3 <main+0x133>
lea    0xf6d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd7e(%rip),%rdi
call   1080 <perror@plt>
jmp    12c6 <main+0x146>
lea    0xd75(%rip),%rdi
jmp    12ba <main+0x13a>
lea    0xe30(%rip),%rdi
jmp    12ba <main+0x13a>
lea    0xe7a(%rip),%rdi
jmp    12ba <main+0x13a>
lea    0xee9(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"dummy_file.txt\00"
@S_ISGID = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@F_RDLCK = global i32 0
@.str.2 = [57 x i8] c"Read lock is set. Open another terminal window and run\0A\00"
@.str.3 = [60 x i8] c"non_locker. Don't hit ENTER in this terminal window until\0A\00"
@.str.4 = [32 x i8] c"non_locker tells you to do so.\0A\00"
@.str.5 = [51 x i8] c"Unable to set read lock. Program is terminating.\0A\00"
@F_UNLCK = global i32 0
@.str.6 = [30 x i8] c"Read lock has been released\0A\0A\00"
@.str.7 = [55 x i8] c"Unable to release read lock. Program is terminating.\0A\00"
@F_WRLCK = global i32 0
@.str.8 = [70 x i8] c"Write lock is set. Switch to the non_locker terminal and hit ENTER.\0A\00"
@.str.9 = [52 x i8] c"Unable to set write lock. Program is terminating.\0A\00"
@.str.10 = [31 x i8] c"Write lock has been released\0A\0A\00"
@.str.11 = [56 x i8] c"Unable to release write lock. Program is terminating.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* @S_ISGID
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IWGRP
%11 = or i32 %9, %10
%12 = call i32 @chmod(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i32, i32* %6
%22 = load i32, i32* @F_RDLCK
%23 = call i64 @setLock(i32 %21, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 (...) @getchar()
br label %36
30: ; preds = %20
%31 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %25
%37 = load i32, i32* %6
%38 = load i32, i32* @F_UNLCK
%39 = call i64 @setLock(i32 %37, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %49
43: ; preds = %36
%44 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.7, i64 0, i64 0))
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %41
%50 = load i32, i32* %6
%51 = load i32, i32* @F_WRLCK
%52 = call i64 @setLock(i32 %50, i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = call i32 @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.8, i64 0, i64 0))
%56 = call i32 (...) @getchar()
br label %63
57: ; preds = %49
%58 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.9, i64 0, i64 0))
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %54
%64 = load i32, i32* %6
%65 = load i32, i32* @F_UNLCK
%66 = call i64 @setLock(i32 %64, i32 %65)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.10, i64 0, i64 0))
br label %76
70: ; preds = %63
%71 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.11, i64 0, i64 0))
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %68
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* @EXIT_SUCCESS
ret i32 %79
}

LLVM IR (-O2):
@.str = [15 x i8] c"dummy_file.txt\00"
@S_ISGID = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@F_RDLCK = global i32 0
@F_UNLCK = global i32 0
@F_WRLCK = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [50 x i8] c"Unable to set read lock. Program is terminating.\00"
@str.12 = [56 x i8] c"Read lock is set. Open another terminal window and run\00"
@str.13 = [59 x i8] c"non_locker. Don't hit ENTER in this terminal window until\00"
@str.14 = [31 x i8] c"non_locker tells you to do so.\00"
@str.15 = [54 x i8] c"Unable to release read lock. Program is terminating.\00"
@str.16 = [29 x i8] c"Read lock has been released\0A\00"
@str.17 = [51 x i8] c"Unable to set write lock. Program is terminating.\00"
@str.18 = [69 x i8] c"Write lock is set. Switch to the non_locker terminal and hit ENTER.\00"
@str.19 = [55 x i8] c"Unable to release write lock. Program is terminating.\00"
@str.20 = [30 x i8] c"Write lock has been released\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @S_ISGID
%4 = load i32, i32* @S_IRGRP
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IWGRP
%7 = or i32 %5, %6
%8 = tail call i32 @chmod(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i32, i32* @F_RDLCK
%18 = tail call i64 @setLock(i32 %10, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.12, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.13, i64 0, i64 0))
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.14, i64 0, i64 0))
%24 = tail call i32 (...) @getchar()
%25 = load i32, i32* @F_UNLCK
%26 = tail call i64 @setLock(i32 %10, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %38
28: ; preds = %16
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%30 = tail call i32 @close(i32 %10)
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %20
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.16, i64 0, i64 0))
%35 = load i32, i32* @F_WRLCK
%36 = tail call i64 @setLock(i32 %10, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %49
38: ; preds = %20
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.15, i64 0, i64 0))
%40 = tail call i32 @close(i32 %10)
%41 = load i32, i32* @EXIT_FAILURE
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %33
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @str.18, i64 0, i64 0))
%45 = tail call i32 (...) @getchar()
%46 = load i32, i32* @F_UNLCK
%47 = tail call i64 @setLock(i32 %10, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %54, label %58
49: ; preds = %33
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.17, i64 0, i64 0))
%51 = tail call i32 @close(i32 %10)
%52 = load i32, i32* @EXIT_FAILURE
%53 = tail call i32 @exit(i32 %52)
unreachable
54: ; preds = %43
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.20, i64 0, i64 0))
%56 = tail call i32 @close(i32 %10)
%57 = load i32, i32* @EXIT_SUCCESS
ret i32 %57
58: ; preds = %43
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.19, i64 0, i64 0))
%60 = tail call i32 @close(i32 %10)
%61 = load i32, i32* @EXIT_FAILURE
%62 = tail call i32 @exit(i32 %61)
unreachable
}