#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int remove(char const *);
int main(int argc, char **argv) {
  FILE *file;
  int i;
  const char *fname;
  if (argc >= 2) {
    fname = argv[1];
  } else {
    fname = "lockedFile.txt";
  }
  file = fopen(fname, "w");
  for (i = 0; i < 10000; i++) {
    fprintf(file, "%s", "x");
    fflush(file);
  }
  fclose(file);
  return remove(fname);
}


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   $0x2,-0x8(%rbp)
jl     1181 <main+0x31>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    118c <main+0x3c>
lea    0xe78(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2710,-0x1c(%rbp)
jge    11e2 <main+0x92>
mov    -0x18(%rbp),%rdi
lea    0xe52(%rip),%rsi
lea    0xe4e(%rip),%rdx
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a7 <main+0x57>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <remove@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jl     115f <main+0xf>
mov    0x8(%rsi),%r14
jmp    1166 <main+0x16>
lea    0xe9a(%rip),%r14
lea    0xea2(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    $0x2710,%ebp
nopl   (%rax)
mov    $0x78,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
add    $0xffffffff,%ebp
jne    1180 <main+0x30>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <remove@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"lockedFile.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [2 x i8] c"x\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sge i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %8
br label %16
15: ; preds = %2
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %8
br label %16
16: ; preds = %15, %11
%17 = load i8*, i8** %8
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %6
store i32 0, i32* %7
br label %19
19: ; preds = %27, %16
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 10000
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i32*, i32** %6
%24 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32*, i32** %6
%26 = call i32 @fflush(i32* %25)
br label %27
27: ; preds = %22
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %19
30: ; preds = %19
%31 = load i32*, i32** %6
%32 = call i32 @fclose(i32* %31)
%33 = load i8*, i8** %8
%34 = call i32 @remove(i8* %33)
ret i32 %34
}

LLVM IR (-O2):
@.str = [15 x i8] c"lockedFile.txt\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %2, %4
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %7, %10
%11 = phi i32 [ 0, %7 ], [ %14, %10 ]
%12 = tail call i32 @fputc(i32 120, i32* %9)
%13 = tail call i32 @fflush(i32* %9)
%14 = add nuw nsw i32 %11, 1
%15 = icmp eq i32 %14, 10000
br i1 %15, label %16, label %10
16: ; preds = %10
%17 = tail call i32 @fclose(i32* %9)
%18 = tail call i32 @remove(i8* %8)
ret i32 %18
}