#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 /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE **files;
  int i;
  int fd, lock;
  if (argc <= 1) {
    fprintf(stderr, "USAGE: file_occupy FILE1 FILE2 ...");
  }
  files = (FILE **)malloc(sizeof(FILE *) * (argc + 1));
  for (i = 1; i < argc; i++) {
    files[i] = fopen(argv[i], "r");
    if (files[i] == NULL) {
      fprintf(stderr, "FILE %s does not exist.\n", argv[i]);
    } else {
      fd = fileno(files[i]);
      lock = flock(fd, LOCK_EX);
      fprintf(stderr, "FILE %s lock result:%d\n", argv[i], lock);
    }
  }
  getchar();
  for (i = 1; i < argc; i++) {
    fclose(files[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11a7 <main+0x37>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1294 <main+0x124>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe3f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1233 <main+0xc3>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1281 <main+0x111>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x24(%rbp),%ecx
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1286 <main+0x116>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c5 <main+0x55>
mov    $0x0,%al
call   1040 <getchar@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12cd <main+0x15d>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12a2 <main+0x132>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x1,%edi
jg     119c <main+0x2c>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0x8(,%rbp,8),%edi
call   1070 <malloc@plt>
cmp    $0x1,%ebp
jle    1244 <main+0xd4>
mov    %rax,%r15
mov    %rbp,(%rsp)
mov    %ebp,%r13d
mov    $0x1,%r14d
lea    0xe5b(%rip),%r12
mov    0x2e09(%rip),%rbp
jmp    1218 <main+0xa8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    0x2df9(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1090 <flock@plt>
mov    0x0(%rbp),%edi
mov    (%rbx,%r14,8),%rdx
lea    0xe38(%rip),%rsi
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r14
cmp    %r14,%r13
je     124d <main+0xdd>
mov    (%rbx,%r14,8),%rdi
mov    %r12,%rsi
call   1080 <fopen@plt>
mov    %rax,(%r15,%r14,8)
test   %rax,%rax
jne    11e0 <main+0x70>
mov    0x0(%rbp),%edi
mov    (%rbx,%r14,8),%rdx
lea    0xdea(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    120f <main+0x9f>
xor    %eax,%eax
call   1040 <getchar@plt>
jmp    1272 <main+0x102>
xor    %eax,%eax
call   1040 <getchar@plt>
cmpl   $0x2,(%rsp)
jl     1272 <main+0x102>
mov    $0x1,%ebx
nop
mov    (%r15,%rbx,8),%rdi
call   1030 <fclose@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1260 <main+0xf0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"USAGE: file_occupy FILE1 FILE2 ...\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"FILE %s does not exist.\0A\00"
@LOCK_EX = global i32 0
@.str.3 = [24 x i8] c"FILE %s lock result:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sle i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %2
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = mul i64 8, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i32**
store i32** %22, i32*** %6
store i32 1, i32* %7
br label %23
23: ; preds = %71, %15
%24 = load i32, i32* %7
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %74
27: ; preds = %23
%28 = load i8**, i8*** %5
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32**, i32*** %6
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32*, i32** %34, i64 %36
store i32* %33, i32** %37
%38 = load i32**, i32*** %6
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32*, i32** %38, i64 %40
%42 = load i32*, i32** %41
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %52
44: ; preds = %27
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %5
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %70
52: ; preds = %27
%53 = load i32**, i32*** %6
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32*, i32** %53, i64 %55
%57 = load i32*, i32** %56
%58 = call i32 @fileno(i32* %57)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = load i32, i32* @LOCK_EX
%61 = call i32 @flock(i32 %59, i32 %60)
store i32 %61, i32* %9
%62 = load i32, i32* @stderr
%63 = load i8**, i8*** %5
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8*, i8** %63, i64 %65
%67 = load i8*, i8** %66
%68 = load i32, i32* %9
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %67, i32 %68)
br label %70
70: ; preds = %52, %44
br label %71
71: ; preds = %70
%72 = load i32, i32* %7
%73 = add nsw i32 %72, 1
store i32 %73, i32* %7
br label %23
74: ; preds = %23
%75 = call i32 (...) @getchar()
store i32 1, i32* %7
br label %76
76: ; preds = %87, %74
%77 = load i32, i32* %7
%78 = load i32, i32* %4
%79 = icmp slt i32 %77, %78
br i1 %79, label %80, label %90
80: ; preds = %76
%81 = load i32**, i32*** %6
%82 = load i32, i32* %7
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i32*, i32** %81, i64 %83
%85 = load i32*, i32** %84
%86 = call i32 @fclose(i32* %85)
br label %87
87: ; preds = %80
%88 = load i32, i32* %7
%89 = add nsw i32 %88, 1
store i32 %89, i32* %7
br label %76
90: ; preds = %76
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"USAGE: file_occupy FILE1 FILE2 ...\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"FILE %s does not exist.\0A\00"
@LOCK_EX = global i32 0
@.str.3 = [24 x i8] c"FILE %s lock result:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %4, %2
%8 = shl i32 %0, 3
%9 = add i32 %8, 8
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i32**
%12 = icmp sgt i32 %0, 1
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = tail call i32 (...) @getchar()
br label %49
15: ; preds = %7
%16 = zext i32 %0 to i64
br label %17
17: ; preds = %15, %35
%18 = phi i64 [ 1, %15 ], [ %36, %35 ]
%19 = getelementptr inbounds i8*, i8** %1, i64 %18
%20 = load i8*, i8** %19
%21 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds i32*, i32** %11, i64 %18
store i32* %21, i32** %22
%23 = icmp eq i32* %21, null
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %19
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %35
28: ; preds = %17
%29 = tail call i32 @fileno(i32* %21)
%30 = load i32, i32* @LOCK_EX
%31 = tail call i32 @flock(i32 %29, i32 %30)
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %19
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %33, i32 %31)
br label %35
35: ; preds = %24, %28
%36 = add nuw nsw i64 %18, 1
%37 = icmp eq i64 %36, %16
br i1 %37, label %38, label %17
38: ; preds = %35
%39 = tail call i32 (...) @getchar()
br i1 %12, label %40, label %49
40: ; preds = %38
%41 = zext i32 %0 to i64
br label %42
42: ; preds = %40, %42
%43 = phi i64 [ 1, %40 ], [ %47, %42 ]
%44 = getelementptr inbounds i32*, i32** %11, i64 %43
%45 = load i32*, i32** %44
%46 = tail call i32 @fclose(i32* %45)
%47 = add nuw nsw i64 %43, 1
%48 = icmp eq i64 %47, %41
br i1 %48, label %49, label %42
49: ; preds = %42, %13, %38
ret i32 0
}