#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*/ P();
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ V();
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 /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, 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);
  }
  P();
  fgets(linebuf, 1024, fp);
  sleep(1);
  fseek(fp, 0, SEEK_SET);
  fprintf(fp, "%d\n", atoi(linebuf) + 1);
  fflush(fp);
  V();
  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    $0x0,%al
call   1030 <P@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dac(%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    0xdad(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <fflush@plt>
mov    $0x0,%al
call   1040 <V@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<func_add>:
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xe2f(%rip),%rdi
lea    0xe31(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1258 <func_add+0x98>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <P@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    0x2dd0(%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    0xde6(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1080 <fflush@plt>
xor    %eax,%eax
call   1040 <V@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdad(%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"
@SEEK_SET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
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 = call i32 (...) @P()
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %1
%14 = call i32 @fgets(i8* %12, i32 1024, i32* %13)
%15 = call i32 @sleep(i32 1)
%16 = load i32*, i32** %1
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %1
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = call i32 @atoi(i8* %20)
%22 = add nsw i32 %21, 1
%23 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32*, i32** %1
%25 = call i32 @fflush(i32* %24)
%26 = call i32 (...) @V()
%27 = load i32*, i32** %1
%28 = call i32 @fclose(i32* %27)
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"
@SEEK_SET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
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 (...) @P()
%10 = call i32 @fgets(i8* %2, i32 1024, i32* %3)
%11 = call i32 @sleep(i32 1)
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @fseek(i32* %3, i32 0, i32 %12)
%14 = call i32 @atoi(i8* %2)
%15 = add nsw i32 %14, 1
%16 = call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 @fflush(i32* %3)
%18 = call i32 (...) @V()
%19 = call i32 @fclose(i32* %3)
ret void
}