#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 /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int lock();
int open(char *, int, ...);
int /*<<< orphan*/ read(int, unsigned int *, int);
int /*<<< orphan*/ unlock(int);
int /*<<< orphan*/ write(int, unsigned int *, int);
unsigned get_lock_count_and_add() {
  int lock_id = lock();
  unsigned count = 0;
  int fd = open("lock_count", O_RDONLY);
  if (fd != -1)
    read(fd, &count, sizeof(unsigned));
  close(fd);
  count++;
  fd = open("lock_count", O_WRONLY | O_CREAT, 0666);
  if (fd != -1)
    write(fd, &count, sizeof(unsigned));
  close(fd);
  unlock(lock_id);
  return count - 1;
}


x86 assembly (clang, -O0):
<get_lock_count_and_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <lock@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     11ae <get_lock_count_and_add+0x4e>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
lea    0xe28(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     1202 <get_lock_count_and_add+0xa2>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <write@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
call   1030 <unlock@plt>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_lock_count_and_add>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <lock@plt>
mov    %eax,%r14d
movl   $0x0,0xc(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11af <get_lock_count_and_add+0x4f>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1070 <read@plt>
mov    0xc(%rsp),%ebx
mov    %ebp,%edi
call   1060 <close@plt>
lea    0x1(%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x2e04(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe2a(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11fa <get_lock_count_and_add+0x9a>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1030 <unlock@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"lock_count\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @get_lock_count_and_add() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @lock()
store i32 %4, i32* %1
store i32 0, i32* %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* %3
%11 = call i32 @read(i32 %10, i32* %2, i32 4)
br label %12
12: ; preds = %9, %0
%13 = load i32, i32* %3
%14 = call i32 @close(i32 %13)
%15 = load i32, i32* %2
%16 = add i32 %15, 1
store i32 %16, i32* %2
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %19, i32 438)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = load i32, i32* %3
%25 = call i32 @write(i32 %24, i32* %2, i32 4)
br label %26
26: ; preds = %23, %12
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %1
%30 = call i32 @unlock(i32 %29)
%31 = load i32, i32* %2
%32 = sub i32 %31, 1
ret i32 %32
}

LLVM IR (-O2):
@.str = [11 x i8] c"lock_count\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @get_lock_count_and_add() {
%1 = alloca i32
%2 = tail call i32 (...) @lock()
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @read(i32 %5, i32* %1, i32 4)
%9 = load i32, i32* %1
br label %10
10: ; preds = %7, %0
%11 = phi i32 [ %9, %7 ], [ 0, %0 ]
%12 = tail call i32 @close(i32 %5)
%13 = add i32 %11, 1
store i32 %13, i32* %1
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %16, i32 438)
%18 = icmp eq i32 %17, -1
br i1 %18, label %21, label %19
19: ; preds = %10
%20 = call i32 @write(i32 %17, i32* %1, i32 4)
br label %21
21: ; preds = %19, %10
%22 = tail call i32 @close(i32 %17)
%23 = tail call i32 @unlock(i32 %2)
ret i32 %11
}