#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 /*<<< orphan*/ F_LOCK;
int /*<<< orphan*/ F_ULOCK;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
void func_add(void) {
  FILE *fp;
  int fd;
  char linebuf[1024];
  fp = fopen("/tmp/out", "r+");
  if (fp == NULL) {
    perror("fopen()");
    exit(1);
  }
  fd = fileno(fp);
  lockf(fd, F_LOCK, 0);
  fgets(linebuf, 1024, fp);
  sleep(1);
  fseek(fp, 0, SEEK_SET);
  fprintf(fp, "%d\n", atoi(linebuf) + 1);
  fflush(fp);
  lockf(fd, F_ULOCK, 0);
  fclose(fp);
  return;
}


x86 assembly (clang, -O0):
<func_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xe2e(%rip),%rdi
lea    0xe30(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1203 <func_add+0x43>
lea    0xe18(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
call   10c0 <atoi@plt>
mov    -0x418(%rbp),%rdi
mov    %eax,%edx
add    $0x1,%edx
lea    0xd95(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0xc(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<func_add>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xe2e(%rip),%rdi
lea    0xe30(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     127e <func_add+0xbe>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %eax,%ebp
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r14,%rdi
call   10c0 <atoi@plt>
lea    0x1(%rax),%edx
lea    0xdcc(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1080 <fflush@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd87(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/tmp/out\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [8 x i8] c"fopen()\00"
@F_LOCK = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@F_ULOCK = global i32 0
define void @func_add() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32*, i32** %1
%12 = call i32 @fileno(i32* %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = load i32, i32* @F_LOCK
%15 = call i32 @lockf(i32 %13, i32 %14, i32 0)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %1
%18 = call i32 @fgets(i8* %16, i32 1024, i32* %17)
%19 = call i32 @sleep(i32 1)
%20 = load i32*, i32** %1
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i32*, i32** %1
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%25 = call i32 @atoi(i8* %24)
%26 = add nsw i32 %25, 1
%27 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %1
%29 = call i32 @fflush(i32* %28)
%30 = load i32, i32* %2
%31 = load i32, i32* @F_ULOCK
%32 = call i32 @lockf(i32 %30, i32 %31, i32 0)
%33 = load i32*, i32** %1
%34 = call i32 @fclose(i32* %33)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/tmp/out\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [8 x i8] c"fopen()\00"
@F_LOCK = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@F_ULOCK = global i32 0
define void @func_add() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 @fileno(i32* %3)
%10 = load i32, i32* @F_LOCK
%11 = tail call i32 @lockf(i32 %9, i32 %10, i32 0)
%12 = call i32 @fgets(i8* %2, i32 1024, i32* %3)
%13 = call i32 @sleep(i32 1)
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @fseek(i32* %3, i32 0, i32 %14)
%16 = call i32 @atoi(i8* %2)
%17 = add nsw i32 %16, 1
%18 = call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = call i32 @fflush(i32* %3)
%20 = load i32, i32* @F_ULOCK
%21 = call i32 @lockf(i32 %9, i32 %20, i32 0)
%22 = call i32 @fclose(i32* %3)
ret void
}