#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, int *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, int *, int);
void do_inc_file(void) {
  int fd, fintt;
  int *fint = &fintt;
  struct stat stbuf;
  int do_init = 0;
  if (stat("test_inc", &stbuf))
    do_init = 1;
  fd = open("test_inc", O_RDWR | O_CREAT, 0666);
  if (fd < 0) {
    perror("open");
    return;
  }
  read(fd, fint, sizeof(int));
  if (do_init)
    *fint = 0;
  {};
  *fint = *fint + 1;
  {};
  lseek(fd, 0, SEEK_SET);
  write(fd, fint, sizeof(int));
  close(fd);
  fflush(stdout);
}


x86 assembly (clang, -O0):
<do_inc_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe62(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11b8 <do_inc_file+0x38>
movl   $0x1,-0x1c(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
lea    0xe2f(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11f9 <do_inc_file+0x79>
lea    0xe1a(%rip),%rdi
call   10a0 <perror@plt>
jmp    1267 <do_inc_file+0xe7>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmpl   $0x0,-0x1c(%rbp)
je     121e <do_inc_file+0x9e>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_inc_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe70(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
mov    %rax,%r14
mov    0x2e39(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
test   %eax,%eax
js     122f <do_inc_file+0xaf>
mov    %eax,%ebx
lea    0x4(%rsp),%r15
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0x4(%rsp),%eax
add    $0x1,%eax
test   %r14,%r14
mov    $0x1,%ecx
cmove  %eax,%ecx
mov    %ecx,0x4(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdd3(%rip),%rdi
call   10a0 <perror@plt>
jmp    1225 <do_inc_file+0xa5>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test_inc\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@stdout = global i32 0
define void @do_inc_file() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.stat
%5 = alloca i32
store i32* %2, i32** %3
store i32 0, i32* %5
%6 = call i64 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i32 1, i32* %5
br label %9
9: ; preds = %8, %0
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12, i32 438)
store i32 %13, i32* %1
%14 = load i32, i32* %1
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %41
18: ; preds = %9
%19 = load i32, i32* %1
%20 = load i32*, i32** %3
%21 = call i32 @read(i32 %19, i32* %20, i32 4)
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32*, i32** %3
store i32 0, i32* %25
br label %26
26: ; preds = %24, %18
%27 = load i32*, i32** %3
%28 = load i32, i32* %27
%29 = add nsw i32 %28, 1
%30 = load i32*, i32** %3
store i32 %29, i32* %30
%31 = load i32, i32* %1
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @lseek(i32 %31, i32 0, i32 %32)
%34 = load i32, i32* %1
%35 = load i32*, i32** %3
%36 = call i32 @write(i32 %34, i32* %35, i32 4)
%37 = load i32, i32* %1
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
br label %41
41: ; preds = %26, %16
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test_inc\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@stdout = global i32 0
define void @do_inc_file() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i64 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8, i32 438)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %25
13: ; preds = %0
%14 = icmp eq i64 %5, 0
%15 = call i32 @read(i32 %9, i32* %1, i32 4)
%16 = load i32, i32* %1
%17 = add i32 %16, 1
%18 = select i1 %14, i32 %17, i32 1
store i32 %18, i32* %1
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @lseek(i32 %9, i32 0, i32 %19)
%21 = call i32 @write(i32 %9, i32* %1, i32 4)
%22 = call i32 @close(i32 %9)
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
br label %25
25: ; preds = %13, %11
ret void
}