#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ pid;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int getpid();
scalar_t__ link(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ unlink(char *);
int cgi_lock(const char *file) {
  char pfile[PATH_MAX + 1];
  char lfile[PATH_MAX + 1];
  FILE *pfp;
  int pid, loop;
  struct stat info;
  loop = 5;
  pid = getpid();
  strcpy(pfile, file);
  sprintf(pfile + strlen(file), ".%d", pid);
  strcpy(lfile, file);
  strcat(lfile, ".LCK");
  pfp = fopen(pfile, "wb");
  if (!pfp) {
    perror(pfile);
    return (0);
  }
  fwrite(&pid, sizeof(pid), 1, pfp);
  fclose(pfp);
  while ((stat(lfile, &info) == 0) || (link(pfile, lfile) != 0)) {
    sleep(1);
    loop--;
    if (loop < 1) {
      unlink(pfile);
      return (0);
    }
  }
  return (1);
}


x86 assembly (clang, -O0):
<cgi_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x5,-0x38(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x50(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x34(%rbp),%edx
lea    0xd9a(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x58(%rbp),%rdi
lea    0xd81(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x50(%rbp),%rdi
lea    0xd76(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12c3 <cgi_lock+0xf3>
mov    -0x50(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1375 <cgi_lock+0x1a5>
mov    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   10e0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x58(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x59(%rbp)
je     1319 <cgi_lock+0x149>
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdi
call   10d0 <link@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    1329 <cgi_lock+0x159>
jmp    1367 <cgi_lock+0x197>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x1,-0x38(%rbp)
jge    1362 <cgi_lock+0x192>
mov    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1375 <cgi_lock+0x1a5>
jmp    12e3 <cgi_lock+0x113>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgi_lock>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
mov    %eax,-0x34(%rbp)
mov    %r15,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
call   1070 <strlen@plt>
movslq %eax,%rdi
add    %r15,%rdi
lea    0xdc7(%rip),%rsi
mov    %ebx,%edx
call   10c0 <sprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
lea    0xdb2(%rip),%rsi
mov    %r12,%rdi
call   10b0 <strcat@plt>
lea    0xda8(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     13a1 <cgi_lock+0x1d1>
mov    %rax,%rbx
lea    -0x34(%rbp),%rdi
mov    $0x1,%r14d
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     12be <cgi_lock+0xee>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <link@plt>
test   %rax,%rax
je     13a9 <cgi_lock+0x1d9>
mov    $0x1,%r14d
mov    $0x1,%edi
call   10f0 <sleep@plt>
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     12f3 <cgi_lock+0x123>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <link@plt>
test   %rax,%rax
je     13a9 <cgi_lock+0x1d9>
mov    $0x1,%r14d
mov    $0x1,%edi
call   10f0 <sleep@plt>
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     1328 <cgi_lock+0x158>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <link@plt>
test   %rax,%rax
je     13a9 <cgi_lock+0x1d9>
mov    $0x1,%r14d
mov    $0x1,%edi
call   10f0 <sleep@plt>
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     1359 <cgi_lock+0x189>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <link@plt>
test   %rax,%rax
je     13a9 <cgi_lock+0x1d9>
mov    $0x1,%r14d
mov    $0x1,%edi
call   10f0 <sleep@plt>
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     138a <cgi_lock+0x1ba>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <link@plt>
test   %rax,%rax
je     13a9 <cgi_lock+0x1d9>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
xor    %r14d,%r14d
jmp    13a9 <cgi_lock+0x1d9>
mov    %r15,%rdi
call   10a0 <perror@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [4 x i8] c".%d\00"
@.str.1 = [5 x i8] c".LCK\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @cgi_lock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i32, i32* @PATH_MAX
%13 = add nsw i32 %12, 1
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %4
%16 = alloca i8, i64 %14
store i64 %14, i64* %5
%17 = load i32, i32* @PATH_MAX
%18 = add nsw i32 %17, 1
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %6
store i32 5, i32* %9
%21 = call i32 (...) @getpid()
store i32 %21, i32* %8
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i8* %16, i8* %22)
%24 = load i8*, i8** %3
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %16, i64 %26
%28 = load i32, i32* %8
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %3
%31 = call i32 @strcpy(i8* %20, i8* %30)
%32 = call i32 @strcat(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %7
%34 = load i32*, i32** %7
%35 = icmp ne i32* %34, null
br i1 %35, label %38, label %36
36: ; preds = %1
%37 = call i32 @perror(i8* %16)
store i32 0, i32* %2
store i32 1, i32* %11
br label %61
38: ; preds = %1
%39 = load i32*, i32** %7
%40 = call i32 @fwrite(i32* %8, i32 4, i32 1, i32* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
br label %43
43: ; preds = %59, %38
%44 = call i64 @stat(i8* %20, %struct.stat* %10)
%45 = icmp eq i64 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %43
%47 = call i64 @link(i8* %16, i8* %20)
%48 = icmp ne i64 %47, 0
br label %49
49: ; preds = %46, %43
%50 = phi i1 [ true, %43 ], [ %48, %46 ]
br i1 %50, label %51, label %60
51: ; preds = %49
%52 = call i32 @sleep(i32 1)
%53 = load i32, i32* %9
%54 = add nsw i32 %53, -1
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp slt i32 %55, 1
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @unlink(i8* %16)
store i32 0, i32* %2
store i32 1, i32* %11
br label %61
59: ; preds = %51
br label %43
60: ; preds = %49
store i32 1, i32* %2
store i32 1, i32* %11
br label %61
61: ; preds = %60, %57, %36
%62 = load i8*, i8** %4
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [4 x i8] c".%d\00"
@.str.1 = [5 x i8] c".LCK\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @cgi_lock(i8* %0) {
%2 = alloca i32
%3 = alloca %struct.stat
%4 = load i32, i32* @PATH_MAX
%5 = add nsw i32 %4, 1
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = alloca i8, i64 %6
%9 = bitcast i32* %2 to i8*
%10 = bitcast %struct.stat* %3 to i8*
%11 = tail call i32 (...) @getpid()
store i32 %11, i32* %2
%12 = call i32 @strcpy(i8* %7, i8* %0)
%13 = call i32 @strlen(i8* %0)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %7, i64 %14
%16 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11)
%17 = call i32 @strcpy(i8* %8, i8* %0)
%18 = call i32 @strcat(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 @perror(i8* %7)
br label %62
23: ; preds = %1
%24 = call i32 @fwrite(i32* %2, i32 4, i32 1, i32* %19)
%25 = call i32 @fclose(i32* %19)
%26 = call i64 @stat(i8* %8, %struct.stat* %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = call i64 @link(i8* %7, i8* %8)
%30 = icmp eq i64 %29, 0
br i1 %30, label %62, label %31
31: ; preds = %23, %28
%32 = call i32 @sleep(i32 1)
%33 = call i64 @stat(i8* %8, %struct.stat* %3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = call i64 @link(i8* %7, i8* %8)
%37 = icmp eq i64 %36, 0
br i1 %37, label %62, label %38
38: ; preds = %35, %31
%39 = call i32 @sleep(i32 1)
%40 = call i64 @stat(i8* %8, %struct.stat* %3)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = call i64 @link(i8* %7, i8* %8)
%44 = icmp eq i64 %43, 0
br i1 %44, label %62, label %45
45: ; preds = %42, %38
%46 = call i32 @sleep(i32 1)
%47 = call i64 @stat(i8* %8, %struct.stat* %3)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %45
%50 = call i64 @link(i8* %7, i8* %8)
%51 = icmp eq i64 %50, 0
br i1 %51, label %62, label %52
52: ; preds = %49, %45
%53 = call i32 @sleep(i32 1)
%54 = call i64 @stat(i8* %8, %struct.stat* %3)
%55 = icmp eq i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %52
%57 = call i64 @link(i8* %7, i8* %8)
%58 = icmp eq i64 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %56, %52
%60 = call i32 @sleep(i32 1)
%61 = call i32 @unlink(i8* %7)
br label %62
62: ; preds = %28, %35, %42, %49, %56, %59, %21
%63 = phi i32 [ 0, %59 ], [ 0, %21 ], [ 1, %56 ], [ 1, %49 ], [ 1, %42 ], [ 1, %35 ], [ 1, %28 ]
ret i32 %63
}