#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 O_CREAT;
int O_EXCL;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int IncrementIndex(char *sPathname, int dMaxIndex) {
  int dLockname = -1;
  FILE *hPathname;
  int i;
  char sLockname[256];
  char sAccum[256];
  int dIndex, dNewIndex;
  strcpy(sLockname, sPathname);
  strcat(sLockname, ".LCK");
  i = 0;
  while (dLockname < 0 && i < 10) {
    dLockname = open(sLockname, O_WRONLY | O_EXCL | O_CREAT,
                     S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
    i++;
  }
  if (dLockname < 0) {
    return -1;
  }
  sprintf(sAccum, "%d", getpid());
  write(dLockname, sAccum, strlen(sAccum));
  close(dLockname);
  hPathname = fopen(sPathname, "r");
  if (hPathname == NULL) {
    unlink(sLockname);
    return -1;
  }
  if (fscanf(hPathname, "%d", &dIndex) != 1) {
    fclose(hPathname);
    unlink(sLockname);
    return -1;
  }
  fclose(hPathname);
  if ((dIndex < dMaxIndex) || (dMaxIndex == 0)) {
    dNewIndex = dIndex + 1;
  } else {
    dNewIndex = 1;
  }
  hPathname = fopen(sPathname, "w");
  if (hPathname == NULL) {
    unlink(sLockname);
    return -1;
  }
  fprintf(hPathname, "%d", dNewIndex);
  fclose(hPathname);
  unlink(sLockname);
  if ((dIndex > dMaxIndex) && (dMaxIndex != 0)) {
    dIndex = 1;
  }
  return dIndex;
}


x86 assembly (clang, -O0):
<IncrementIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x130(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   10e0 <strcat@plt>
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x239(%rbp)
jge    1232 <IncrementIndex+0x62>
cmpl   $0xa,-0x24(%rbp)
setl   %al
mov    %al,-0x239(%rbp)
mov    -0x239(%rbp),%al
test   $0x1,%al
jne    1245 <IncrementIndex+0x75>
jmp    12aa <IncrementIndex+0xda>
lea    -0x130(%rbp),%rdi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
or     (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
mov    0x2d61(%rip),%rax
or     (%rax),%edx
mov    0x2d40(%rip),%rax
or     (%rax),%edx
mov    0x2d57(%rip),%rax
or     (%rax),%edx
mov    0x2d26(%rip),%rax
or     (%rax),%edx
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1213 <IncrementIndex+0x43>
cmpl   $0x0,-0x18(%rbp)
jge    12c0 <IncrementIndex+0xf0>
movl   $0xffffffff,-0x4(%rbp)
jmp    146f <IncrementIndex+0x29f>
lea    -0x230(%rbp),%rax
mov    %rax,-0x258(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x258(%rbp),%rdi
mov    %eax,%edx
lea    0xd20(%rip),%rsi
call   10f0 <sprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x230(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x24c(%rbp),%edi
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    -0x18(%rbp),%edi
call   10a0 <close@plt>
mov    -0x10(%rbp),%rdi
lea    0xcd4(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1360 <IncrementIndex+0x190>
lea    -0x130(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    146f <IncrementIndex+0x29f>
mov    -0x20(%rbp),%rdi
lea    0xc9a(%rip),%rsi
lea    -0x234(%rbp),%rdx
call   1090 <fscanf@plt>
cmp    $0x1,%eax
je     13a1 <IncrementIndex+0x1d1>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
lea    -0x130(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    146f <IncrementIndex+0x29f>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x234(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     13c3 <IncrementIndex+0x1f3>
cmpl   $0x0,-0x14(%rbp)
jne    13d7 <IncrementIndex+0x207>
mov    -0x234(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x238(%rbp)
jmp    13e1 <IncrementIndex+0x211>
movl   $0x1,-0x238(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xc1e(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1418 <IncrementIndex+0x248>
lea    -0x130(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    146f <IncrementIndex+0x29f>
mov    -0x20(%rbp),%rdi
mov    -0x238(%rbp),%edx
lea    0xbdc(%rip),%rsi
call   10b0 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
lea    -0x130(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x234(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1466 <IncrementIndex+0x296>
cmpl   $0x0,-0x14(%rbp)
je     1466 <IncrementIndex+0x296>
movl   $0x1,-0x234(%rbp)
mov    -0x234(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IncrementIndex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %esi,0x4(%rsp)
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rsi,0x8(%rsp)
call   1040 <strcpy@plt>
lea    0xdff(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
xor    %ebp,%ebp
mov    0x2dc6(%rip),%r13
mov    0x2da7(%rip),%r14
mov    0x2dc0(%rip),%r15
mov    0x2d91(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    0x2d60(%rip),%rax
or     (%rax),%esi
mov    0x2daf(%rip),%rax
or     (%rax),%esi
mov    0x0(%r13),%edx
mov    0x2d5a(%rip),%rax
or     (%rax),%edx
or     (%r14),%edx
or     (%r15),%edx
or     (%r12),%edx
lea    0x10(%rsp),%rdi
call   10c0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    127c <IncrementIndex+0xac>
lea    0x1(%rbp),%eax
cmp    $0x9,%ebp
mov    %eax,%ebp
jb     1230 <IncrementIndex+0x60>
mov    $0xffffffff,%r12d
test   %ebx,%ebx
js     137b <IncrementIndex+0x1ab>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd6d(%rip),%rsi
lea    0x110(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   10f0 <sprintf@plt>
mov    %rbp,%rdi
call   1080 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
lea    0xd3c(%rip),%rsi
mov    0x8(%rsp),%r15
mov    %r15,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1371 <IncrementIndex+0x1a1>
mov    %rax,%rbx
lea    0xd19(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1090 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <fclose@plt>
cmp    $0x1,%ebp
jne    1371 <IncrementIndex+0x1a1>
mov    (%rsp),%r14d
lea    0xcf9(%rip),%rsi
mov    %r15,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1371 <IncrementIndex+0x1a1>
mov    %rax,%rbx
lea    0x1(%r14),%eax
mov    0x4(%rsp),%ebp
cmp    %ebp,%r14d
mov    $0x1,%edx
cmovl  %eax,%edx
mov    $0x1,%r12d
test   %ebp,%ebp
cmove  %eax,%edx
lea    0xcbf(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    (%rsp),%eax
cmp    %ebp,%eax
cmovle %eax,%r12d
test   %ebp,%ebp
cmove  %eax,%r12d
jmp    137b <IncrementIndex+0x1ab>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    %r12d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c".LCK\00"
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @IncrementIndex(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %6
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%14 = load i8*, i8** %4
%15 = call i32 @strcpy(i8* %13, i8* %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%17 = call i32 @strcat(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %8
br label %18
18: ; preds = %26, %2
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 10
br label %24
24: ; preds = %21, %18
%25 = phi i1 [ false, %18 ], [ %23, %21 ]
br i1 %25, label %26, label %45
26: ; preds = %24
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_EXCL
%30 = or i32 %28, %29
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IRGRP
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IWGRP
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IROTH
%41 = or i32 %39, %40
%42 = call i32 @open(i8* %27, i32 %32, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %18
45: ; preds = %24
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
store i32 -1, i32* %3
br label %114
49: ; preds = %45
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%51 = call i32 (...) @getpid()
%52 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %6
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = call i32 @write(i32 %53, i8* %54, i32 %56)
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
%60 = load i8*, i8** %4
%61 = call i32* @fopen(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %61, i32** %7
%62 = load i32*, i32** %7
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %49
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%66 = call i32 @unlink(i8* %65)
store i32 -1, i32* %3
br label %114
67: ; preds = %49
%68 = load i32*, i32** %7
%69 = call i32 @fscanf(i32* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %11)
%70 = icmp ne i32 %69, 1
br i1 %70, label %71, label %76
71: ; preds = %67
%72 = load i32*, i32** %7
%73 = call i32 @fclose(i32* %72)
%74 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%75 = call i32 @unlink(i8* %74)
store i32 -1, i32* %3
br label %114
76: ; preds = %67
%77 = load i32*, i32** %7
%78 = call i32 @fclose(i32* %77)
%79 = load i32, i32* %11
%80 = load i32, i32* %5
%81 = icmp slt i32 %79, %80
br i1 %81, label %85, label %82
82: ; preds = %76
%83 = load i32, i32* %5
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %82, %76
%86 = load i32, i32* %11
%87 = add nsw i32 %86, 1
store i32 %87, i32* %12
br label %89
88: ; preds = %82
store i32 1, i32* %12
br label %89
89: ; preds = %88, %85
%90 = load i8*, i8** %4
%91 = call i32* @fopen(i8* %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %91, i32** %7
%92 = load i32*, i32** %7
%93 = icmp eq i32* %92, null
br i1 %93, label %94, label %97
94: ; preds = %89
%95 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%96 = call i32 @unlink(i8* %95)
store i32 -1, i32* %3
br label %114
97: ; preds = %89
%98 = load i32*, i32** %7
%99 = load i32, i32* %12
%100 = call i32 @fprintf(i32* %98, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %99)
%101 = load i32*, i32** %7
%102 = call i32 @fclose(i32* %101)
%103 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%104 = call i32 @unlink(i8* %103)
%105 = load i32, i32* %11
%106 = load i32, i32* %5
%107 = icmp sgt i32 %105, %106
br i1 %107, label %108, label %112
108: ; preds = %97
%109 = load i32, i32* %5
%110 = icmp ne i32 %109, 0
br i1 %110, label %111, label %112
111: ; preds = %108
store i32 1, i32* %11
br label %112
112: ; preds = %111, %108, %97
%113 = load i32, i32* %11
store i32 %113, i32* %3
br label %114
114: ; preds = %112, %94, %71, %64, %48
%115 = load i32, i32* %3
ret i32 %115
}

LLVM IR (-O2):
@.str = [5 x i8] c".LCK\00"
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @IncrementIndex(i8* %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = call i32 @strcpy(i8* %6, i8* %0)
%10 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %2, %11
%12 = phi i32 [ 0, %2 ], [ %28, %11 ]
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_EXCL
%15 = or i32 %14, %13
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IWGRP
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IROTH
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %6, i32 %17, i32 %26)
%28 = add nuw nsw i32 %12, 1
%29 = icmp slt i32 %27, 0
%30 = icmp ult i32 %12, 9
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %11, label %32
32: ; preds = %11
br i1 %29, label %65, label %33
33: ; preds = %32
%34 = call i32 (...) @getpid()
%35 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = call i32 @strlen(i8* %7)
%37 = call i32 @write(i32 %27, i8* %7, i32 %36)
%38 = call i32 @close(i32 %27)
%39 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %63, label %41
41: ; preds = %33
%42 = call i32 @fscanf(i32* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%43 = icmp eq i32 %42, 1
%44 = call i32 @fclose(i32* %39)
br i1 %43, label %45, label %63
45: ; preds = %41
%46 = load i32, i32* %5
%47 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp eq i32* %47, null
br i1 %48, label %63, label %49
49: ; preds = %45
%50 = icmp slt i32 %46, %1
%51 = icmp eq i32 %1, 0
%52 = or i1 %51, %50
%53 = add nsw i32 %46, 1
%54 = select i1 %52, i32 %53, i32 1
%55 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %54)
%56 = call i32 @fclose(i32* %47)
%57 = call i32 @unlink(i8* %6)
%58 = load i32, i32* %5
%59 = icmp sgt i32 %58, %1
%60 = icmp ne i32 %1, 0
%61 = and i1 %60, %59
%62 = select i1 %61, i32 1, i32 %58
br label %65
63: ; preds = %45, %41, %33
%64 = call i32 @unlink(i8* %6)
br label %65
65: ; preds = %49, %63, %32
%66 = phi i32 [ -1, %32 ], [ %62, %49 ], [ -1, %63 ]
ret i32 %66
}