#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*/ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ my_lock(int);
int /*<<< orphan*/ my_unlock(int);
int open(char *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  pid_t pid;
  char buf[1024];
  int i, n, cnt;
  pid = getpid();
  fd = open("seqno", O_CREAT | O_RDWR,
            (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
  for (i = 0; i < 3000000; i++) {
    my_lock(fd);
    lseek(fd, 0, SEEK_SET);
    n = read(fd, buf, sizeof(buf));
    buf[n] = '\0';
    sscanf(buf, "%d", &cnt);
    cnt++;
    snprintf(buf, sizeof(buf), "%d", cnt);
    lseek(fd, 0, SEEK_SET);
    write(fd, buf, strlen(buf));
    my_unlock(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    0x2deb(%rip),%rax
or     (%rax),%edx
mov    0x2dca(%rip),%rax
or     (%rax),%edx
mov    0x2de1(%rip),%rax
or     (%rax),%edx
mov    0x2db0(%rip),%rax
or     (%rax),%edx
mov    0x2d8f(%rip),%rax
or     (%rax),%edx
lea    0xdee(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x424(%rbp)
cmpl   $0x2dc6c0,-0x424(%rbp)
jge    1325 <main+0x185>
mov    -0x14(%rbp),%edi
call   1080 <my_lock@plt>
mov    -0x14(%rbp),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
mov    %eax,-0x428(%rbp)
movslq -0x428(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
lea    0xd80(%rip),%rsi
lea    -0x42c(%rbp),%rdx
call   10b0 <sscanf@plt>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x42c(%rbp),%ecx
mov    $0x400,%esi
lea    0xd4c(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x14(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <lseek@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x420(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1030 <my_unlock@plt>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    1224 <main+0x84>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    0x2e39(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd5(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2dbb(%rip),%rax
or     (%rax),%edx
mov    0x2d9a(%rip),%rax
or     (%rax),%edx
lea    0xdf9(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%ebx
mov    $0x2dc6c0,%ebp
mov    0x2da6(%rip),%r13
lea    0x10(%rsp),%r14
lea    0xde0(%rip),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1080 <my_lock@plt>
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1090 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   10b0 <sscanf@plt>
mov    0xc(%rsp),%ecx
add    $0x1,%ecx
mov    %ecx,0xc(%rsp)
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1070 <snprintf@plt>
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1090 <lseek@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1030 <my_unlock@plt>
add    $0xffffffff,%ebp
jne    1230 <main+0x90>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"seqno\00"
@O_CREAT = global i32 0
@O_RDWR = 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
@S_IWOTH = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [3 x i8] c"%d\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 [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (...) @getpid()
store i32 %12, i32* %7
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_RDWR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IWGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IWOTH
%26 = or i32 %24, %25
%27 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %26)
store i32 %27, i32* %6
store i32 0, i32* %9
br label %28
28: ; preds = %60, %2
%29 = load i32, i32* %9
%30 = icmp slt i32 %29, 3000000
br i1 %30, label %31, label %63
31: ; preds = %28
%32 = load i32, i32* %6
%33 = call i32 @my_lock(i32 %32)
%34 = load i32, i32* %6
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %34, i32 0, i32 %35)
%37 = load i32, i32* %6
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 1024)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %41
store i8 0, i8* %42
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = call i32 @sscanf(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %11)
%45 = load i32, i32* %11
%46 = add nsw i32 %45, 1
store i32 %46, i32* %11
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%48 = load i32, i32* %11
%49 = call i32 @snprintf(i8* %47, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %6
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @lseek(i32 %50, i32 0, i32 %51)
%53 = load i32, i32* %6
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, 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 @my_unlock(i32 %58)
br label %60
60: ; preds = %31
%61 = load i32, i32* %9
%62 = add nsw i32 %61, 1
store i32 %62, i32* %9
br label %28
63: ; preds = %28
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"seqno\00"
@O_CREAT = global i32 0
@O_RDWR = 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
@S_IWOTH = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 (...) @getpid()
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IWOTH
%21 = or i32 %19, %20
%22 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %21)
br label %23
23: ; preds = %2, %23
%24 = phi i32 [ 0, %2 ], [ %40, %23 ]
%25 = call i32 @my_lock(i32 %22)
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %22, i32 0, i32 %26)
%28 = call i32 @read(i32 %22, i8* %5, i32 1024)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
%34 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %22, i32 0, i32 %35)
%37 = call i32 @strlen(i8* %5)
%38 = call i32 @write(i32 %22, i8* %5, i32 %37)
%39 = call i32 @my_unlock(i32 %22)
%40 = add nuw nsw i32 %24, 1
%41 = icmp eq i32 %40, 3000000
br i1 %41, label %42, label %23
42: ; preds = %23
ret i32 0
}