#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int read(int, int *, int);
int write(int, int *, int);
int adjust_instance_count(char *countfile, int inc) {
  int fd;
  int proccount = 0;
  int brw;
  fd = open(countfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  brw = read(fd, &proccount, sizeof(int));
  if ((brw == 0) || (brw == -1))
    proccount = 0;
  proccount += inc;
  if (proccount < 0)
    proccount = 0;
  lseek(fd, 0, SEEK_SET);
  brw = write(fd, &proccount, sizeof(int));
  close(fd);
  return proccount;
}


x86 assembly (clang, -O0):
<adjust_instance_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    0x2e7e(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11be <adjust_instance_count+0x6e>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c5 <adjust_instance_count+0x75>
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11df <adjust_instance_count+0x8f>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x10(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adjust_instance_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
movl   $0x0,0x4(%rsp)
mov    0x2e77(%rip),%rax
mov    0x2e88(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e45(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
add    $0x1,%eax
xor    %ecx,%ecx
mov    $0x0,%ebx
cmp    $0x2,%eax
jb     11b4 <adjust_instance_count+0x64>
mov    0x4(%rsp),%ebx
add    %r15d,%ebx
cmovs  %ecx,%ebx
mov    %ebx,0x4(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SEEK_SET = global i32 0
define i32 @adjust_instance_count(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %8, i32 %11, i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = call i32 @read(i32 %16, i32* %6, i32 4)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %2
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %20, %2
store i32 0, i32* %6
br label %24
24: ; preds = %23, %20
%25 = load i32, i32* %4
%26 = load i32, i32* %6
%27 = add nsw i32 %26, %25
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 0, i32* %6
br label %31
31: ; preds = %30, %24
%32 = load i32, i32* %5
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @lseek(i32 %32, i32 0, i32 %33)
%35 = load i32, i32* %5
%36 = call i32 @write(i32 %35, i32* %6, i32 4)
store i32 %36, i32* %7
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %6
ret i32 %39
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SEEK_SET = global i32 0
define i32 @adjust_instance_count(i8* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %0, i32 %7, i32 %10)
%12 = call i32 @read(i32 %11, i32* %3, i32 4)
%13 = add i32 %12, 1
%14 = icmp ult i32 %13, 2
%15 = load i32, i32* %3
%16 = select i1 %14, i32 0, i32 %15
%17 = add nsw i32 %16, %1
%18 = icmp sgt i32 %17, 0
%19 = select i1 %18, i32 %17, i32 0
store i32 %19, i32* %3
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @lseek(i32 %11, i32 0, i32 %20)
%22 = call i32 @write(i32 %11, i32* %3, i32 4)
%23 = tail call i32 @close(i32 %11)
ret i32 %19
}