#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*/ F_RDLCK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ lock_operation(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, nread;
  char buf[1024];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_RDONLY)) == -1) {
    perror(argv[1]);
    exit(2);
  };
  lock_operation(fd, F_RDLCK);
  while ((nread = read(fd, buf, 1024)) > 0)
    write(1, buf, nread);
  lock_operation(fd, F_UNLCK);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c9 <main+0x49>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1202 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
call   1050 <lock_operation@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    124c <main+0xcc>
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1213 <main+0x93>
mov    -0x14(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1050 <lock_operation@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1225 <main+0xa5>
mov    0x8(%rbx),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1247 <main+0xc7>
mov    %eax,%ebp
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <lock_operation@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1202 <main+0x82>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11e0 <main+0x60>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <lock_operation@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdc8(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x8(%rbx),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDONLY = global i32 0
@F_RDLCK = global i32 0
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @perror(i8* %28)
%30 = call i32 @exit(i32 2)
unreachable
31: ; preds = %18
%32 = load i32, i32* %6
%33 = load i32, i32* @F_RDLCK
%34 = call i32 @lock_operation(i32 %32, i32 %33)
br label %35
35: ; preds = %40, %31
%36 = load i32, i32* %6
%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* %7
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = load i32, i32* %7
%43 = call i32 @write(i32 1, i8* %41, i32 %42)
br label %35
44: ; preds = %35
%45 = load i32, i32* %6
%46 = load i32, i32* @F_UNLCK
%47 = call i32 @lock_operation(i32 %45, i32 %46)
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDONLY = global i32 0
@F_RDLCK = global i32 0
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i8*, i8** %12
%19 = tail call i32 @perror(i8* %18)
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %11
%22 = load i32, i32* @F_RDLCK
%23 = tail call i32 @lock_operation(i32 %15, i32 %22)
%24 = call i32 @read(i32 %15, i8* %4, i32 1024)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21, %26
%27 = phi i32 [ %29, %26 ], [ %24, %21 ]
%28 = call i32 @write(i32 1, i8* %4, i32 %27)
%29 = call i32 @read(i32 %15, i8* %4, i32 1024)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %26, label %31
31: ; preds = %26, %21
%32 = load i32, i32* @F_UNLCK
%33 = tail call i32 @lock_operation(i32 %15, i32 %32)
%34 = tail call i32 @close(i32 %15)
ret i32 0
}