#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 st_size;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int errno;
int fstat(int, struct stat *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int const strerror(int);
int /*<<< orphan*/ v_printf(char *, int const, ...);
int /*<<< orphan*/ write(int, char *, int const);
int process(const int write_size, const int write_iters) {
  int failed = 0;
  int fd;
  struct stat sbuf;
  char ch = 'A';
  char buf[write_size];
  char filename[4096];
  int ret;
  v_printf("s%d i%d\n", write_size, write_iters);
  for (int idx = 0; idx < write_size; idx++) {
    buf[idx] = ch;
    ++ch;
    if (ch > 'z')
      ch = 'A';
  }
  strcpy(filename, "files/stat-stat");
  fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0);
  for (int kdx = 0; kdx < write_iters; kdx++) {
    write(fd, buf, write_size);
  }
  ret = fstat(fd, &sbuf);
  if (ret < 0) {
    v_printf("ERROR on stat or fstat: %d: %s\n", errno, strerror(errno));
    ++failed;
  } else {
    int expected_size;
    expected_size = write_size * write_iters;
    if (sbuf.st_size == expected_size) {
      v_printf("stat-stat After write, fstat got expected size of %d\n",
               sbuf.st_size);
    } else {
      v_printf("stat-stat After write, fstat ERROR expected %d size is %d\n",
               expected_size, sbuf.st_size);
      ++failed;
    }
  }
  close(fd);
  return failed;
}


x86 assembly (clang, -O0):
<process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movb   $0x41,-0x19(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x1048(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1070 <v_printf@plt>
movl   $0x0,-0x1038(%rbp)
mov    -0x1038(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1228 <process+0xb8>
mov    -0x1048(%rbp),%rax
mov    -0x19(%rbp),%dl
movslq -0x1038(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x19(%rbp),%al
add    $0x1,%al
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x7a,%eax
jle    120f <process+0x9f>
movb   $0x41,-0x19(%rbp)
jmp    1214 <process+0xa4>
mov    -0x1038(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1038(%rbp)
jmp    11d3 <process+0x63>
lea    -0x1030(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1030(%rbp),%rdi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
mov    0x2da6(%rip),%rax
or     (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x103c(%rbp)
mov    -0x103c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12a6 <process+0x136>
mov    -0x1048(%rbp),%rsi
mov    -0x10(%rbp),%edi
mov    -0x4(%rbp),%edx
call   1040 <write@plt>
mov    -0x103c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x103c(%rbp)
jmp    1271 <process+0x101>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    1306 <process+0x196>
mov    0x2cfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x104c(%rbp)
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x104c(%rbp),%esi
mov    %eax,%edx
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1070 <v_printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    135d <process+0x1ed>
mov    -0x4(%rbp),%eax
imul   -0x8(%rbp),%eax
mov    %eax,-0x1040(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1040(%rbp),%eax
jne    1338 <process+0x1c8>
mov    -0x18(%rbp),%esi
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1070 <v_printf@plt>
jmp    1358 <process+0x1e8>
mov    -0x1040(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1070 <v_printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    135d <process+0x1ed>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %esi,%r13d
mov    %edi,%r15d
mov    %rsp,-0x38(%rbp)
mov    %edi,%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe56(%rip),%rdi
mov    %r15d,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1070 <v_printf@plt>
test   %r15d,%r15d
jle    1208 <process+0x98>
lea    -0x1(%r12),%rax
mov    %r12d,%r8d
and    $0x3,%r8d
cmp    $0x3,%rax
jae    12bc <process+0x14c>
mov    $0x41,%dl
xor    %ecx,%ecx
test   %r8,%r8
je     1208 <process+0x98>
add    %rbx,%rcx
xor    %esi,%esi
mov    $0x41,%edi
cs nopw 0x0(%rax,%rax,1)
nop
mov    %dl,(%rcx,%rsi,1)
add    $0x1,%dl
cmp    $0x7b,%dl
movzbl %dl,%edx
cmovge %edi,%edx
add    $0x1,%rsi
cmp    %rsi,%r8
jne    11f0 <process+0x80>
lea    0xdfa(%rip),%rsi
lea    -0x1040(%rbp),%r12
mov    %r12,%rdi
call   1030 <strcpy@plt>
mov    0x2db3(%rip),%rax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db9(%rip),%rax
or     (%rax),%esi
mov    %r12,%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,%r12d
test   %r13d,%r13d
jle    1264 <process+0xf4>
mov    %r13d,%r14d
xchg   %ax,%ax
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
add    $0xffffffff,%r14d
jne    1250 <process+0xe0>
lea    -0x30(%rbp),%rsi
mov    %r12d,%edi
call   1090 <fstat@plt>
test   %eax,%eax
js     129c <process+0x12c>
imul   %r15d,%r13d
mov    -0x30(%rbp),%edx
cmp    %r13d,%edx
jne    1334 <process+0x1c4>
lea    0xdae(%rip),%rdi
xor    %ebx,%ebx
mov    %r13d,%esi
xor    %eax,%eax
call   1070 <v_printf@plt>
jmp    134a <process+0x1da>
mov    0x2d25(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xd66(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
jmp    133e <process+0x1ce>
and    $0xfffffffc,%r12d
mov    $0x41,%al
xor    %ecx,%ecx
jmp    12df <process+0x16f>
cs nopw 0x0(%rax,%rax,1)
add    $0x4,%rcx
mov    %edx,%eax
cmp    %rcx,%r12
je     11d6 <process+0x66>
mov    %al,(%rbx,%rcx,1)
add    $0x1,%al
mov    $0x41,%edx
mov    $0x41,%esi
cmp    $0x7b,%al
jge    12f5 <process+0x185>
movzbl %al,%esi
mov    %sil,0x1(%rbx,%rcx,1)
add    $0x1,%sil
mov    $0x41,%eax
cmp    $0x7b,%sil
jge    130d <process+0x19d>
movzbl %sil,%eax
mov    %al,0x2(%rbx,%rcx,1)
add    $0x1,%al
mov    $0x41,%esi
cmp    $0x7b,%al
jge    131f <process+0x1af>
movzbl %al,%esi
mov    %sil,0x3(%rbx,%rcx,1)
add    $0x1,%sil
cmp    $0x7b,%sil
jge    12d0 <process+0x160>
movzbl %sil,%edx
jmp    12d0 <process+0x160>
lea    0xd34(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1070 <v_printf@plt>
mov    $0x1,%ebx
mov    %r12d,%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"s%d i%d\0A\00"
@.str.1 = [16 x i8] c"files/stat-stat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [32 x i8] c"ERROR on stat or fstat: %d: %s\0A\00"
@errno = global i32 0
@.str.3 = [54 x i8] c"stat-stat After write, fstat got expected size of %d\0A\00"
@.str.4 = [59 x i8] c"stat-stat After write, fstat ERROR expected %d size is %d\0A\00"
define i32 @process(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i64
%11 = alloca [4096 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i8 65, i8* %8
%16 = load i32, i32* %3
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %9
%19 = alloca i8, i64 %17
store i64 %17, i64* %10
%20 = load i32, i32* %3
%21 = load i32, i32* %4
%22 = call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21)
store i32 0, i32* %13
br label %23
23: ; preds = %39, %2
%24 = load i32, i32* %13
%25 = load i32, i32* %3
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %42
27: ; preds = %23
%28 = load i8, i8* %8
%29 = load i32, i32* %13
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %19, i64 %30
store i8 %28, i8* %31
%32 = load i8, i8* %8
%33 = add i8 %32, 1
store i8 %33, i8* %8
%34 = load i8, i8* %8
%35 = sext i8 %34 to i32
%36 = icmp sgt i32 %35, 122
br i1 %36, label %37, label %38
37: ; preds = %27
store i8 65, i8* %8
br label %38
38: ; preds = %37, %27
br label %39
39: ; preds = %38
%40 = load i32, i32* %13
%41 = add nsw i32 %40, 1
store i32 %41, i32* %13
br label %23
42: ; preds = %23
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%44 = call i32 @strcpy(i8* %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%46 = load i32, i32* @O_RDWR
%47 = load i32, i32* @O_CREAT
%48 = or i32 %46, %47
%49 = load i32, i32* @O_TRUNC
%50 = or i32 %48, %49
%51 = call i32 @open(i8* %45, i32 %50, i32 0)
store i32 %51, i32* %6
store i32 0, i32* %14
br label %52
52: ; preds = %60, %42
%53 = load i32, i32* %14
%54 = load i32, i32* %4
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %63
56: ; preds = %52
%57 = load i32, i32* %6
%58 = load i32, i32* %3
%59 = call i32 @write(i32 %57, i8* %19, i32 %58)
br label %60
60: ; preds = %56
%61 = load i32, i32* %14
%62 = add nsw i32 %61, 1
store i32 %62, i32* %14
br label %52
63: ; preds = %52
%64 = load i32, i32* %6
%65 = call i32 @fstat(i32 %64, %struct.stat* %7)
store i32 %65, i32* %12
%66 = load i32, i32* %12
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %63
%69 = load i32, i32* @errno
%70 = load i32, i32* @errno
%71 = call i32 @strerror(i32 %70)
%72 = call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %69, i32 %71)
%73 = load i32, i32* %5
%74 = add nsw i32 %73, 1
store i32 %74, i32* %5
br label %95
75: ; preds = %63
%76 = load i32, i32* %3
%77 = load i32, i32* %4
%78 = mul nsw i32 %76, %77
store i32 %78, i32* %15
%79 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%80 = load i32, i32* %79
%81 = load i32, i32* %15
%82 = icmp eq i32 %80, %81
br i1 %82, label %83, label %87
83: ; preds = %75
%84 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%85 = load i32, i32* %84
%86 = call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i32 %85)
br label %94
87: ; preds = %75
%88 = load i32, i32* %15
%89 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%90 = load i32, i32* %89
%91 = call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i32 %88, i32 %90)
%92 = load i32, i32* %5
%93 = add nsw i32 %92, 1
store i32 %93, i32* %5
br label %94
94: ; preds = %87, %83
br label %95
95: ; preds = %94, %68
%96 = load i32, i32* %6
%97 = call i32 @close(i32 %96)
%98 = load i32, i32* %5
%99 = load i8*, i8** %9
ret i32 %98
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"s%d i%d\0A\00"
@.str.1 = [16 x i8] c"files/stat-stat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [32 x i8] c"ERROR on stat or fstat: %d: %s\0A\00"
@errno = global i32 0
@.str.3 = [54 x i8] c"stat-stat After write, fstat got expected size of %d\0A\00"
@.str.4 = [59 x i8] c"stat-stat After write, fstat ERROR expected %d size is %d\0A\00"
define i32 @process(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca [4096 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = zext i32 %0 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%10 = tail call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%11 = icmp sgt i32 %0, 0
br i1 %11, label %12, label %33
12: ; preds = %2
%13 = add nsw i64 %6, -1
%14 = and i64 %6, 3
%15 = icmp ult i64 %13, 3
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = and i64 %6, 4294967292
br label %42
18: ; preds = %42, %12
%19 = phi i64 [ 0, %12 ], [ %65, %42 ]
%20 = phi i8 [ 65, %12 ], [ %64, %42 ]
%21 = icmp eq i64 %14, 0
br i1 %21, label %33, label %22
22: ; preds = %18, %22
%23 = phi i64 [ %30, %22 ], [ %19, %18 ]
%24 = phi i8 [ %29, %22 ], [ %20, %18 ]
%25 = phi i64 [ %31, %22 ], [ 0, %18 ]
%26 = getelementptr inbounds i8, i8* %8, i64 %23
store i8 %24, i8* %26
%27 = add i8 %24, 1
%28 = icmp sgt i8 %27, 122
%29 = select i1 %28, i8 65, i8 %27
%30 = add nuw nsw i64 %23, 1
%31 = add i64 %25, 1
%32 = icmp eq i64 %31, %14
br i1 %32, label %33, label %22
33: ; preds = %18, %22, %2
%34 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @O_RDWR
%36 = load i32, i32* @O_CREAT
%37 = or i32 %36, %35
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %9, i32 %39, i32 0)
%41 = icmp sgt i32 %1, 0
br i1 %41, label %71, label %68
42: ; preds = %42, %16
%43 = phi i64 [ 0, %16 ], [ %65, %42 ]
%44 = phi i8 [ 65, %16 ], [ %64, %42 ]
%45 = phi i64 [ 0, %16 ], [ %66, %42 ]
%46 = getelementptr inbounds i8, i8* %8, i64 %43
store i8 %44, i8* %46
%47 = add i8 %44, 1
%48 = icmp sgt i8 %47, 122
%49 = select i1 %48, i8 65, i8 %47
%50 = or i64 %43, 1
%51 = getelementptr inbounds i8, i8* %8, i64 %50
store i8 %49, i8* %51
%52 = add i8 %49, 1
%53 = icmp sgt i8 %52, 122
%54 = select i1 %53, i8 65, i8 %52
%55 = or i64 %43, 2
%56 = getelementptr inbounds i8, i8* %8, i64 %55
store i8 %54, i8* %56
%57 = add i8 %54, 1
%58 = icmp sgt i8 %57, 122
%59 = select i1 %58, i8 65, i8 %57
%60 = or i64 %43, 3
%61 = getelementptr inbounds i8, i8* %8, i64 %60
store i8 %59, i8* %61
%62 = add i8 %59, 1
%63 = icmp sgt i8 %62, 122
%64 = select i1 %63, i8 65, i8 %62
%65 = add nuw nsw i64 %43, 4
%66 = add i64 %45, 4
%67 = icmp eq i64 %66, %17
br i1 %67, label %18, label %42
68: ; preds = %71, %33
%69 = call i32 @fstat(i32 %40, %struct.stat* %3)
%70 = icmp slt i32 %69, 0
br i1 %70, label %76, label %80
71: ; preds = %33, %71
%72 = phi i32 [ %74, %71 ], [ 0, %33 ]
%73 = call i32 @write(i32 %40, i8* %8, i32 %0)
%74 = add nuw nsw i32 %72, 1
%75 = icmp eq i32 %74, %1
br i1 %75, label %68, label %71
76: ; preds = %68
%77 = load i32, i32* @errno
%78 = call i32 @strerror(i32 %77)
%79 = call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %77, i32 %78)
br label %89
80: ; preds = %68
%81 = mul nsw i32 %1, %0
%82 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%83 = load i32, i32* %82
%84 = icmp eq i32 %83, %81
br i1 %84, label %85, label %87
85: ; preds = %80
%86 = call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i32 %81)
br label %89
87: ; preds = %80
%88 = call i32 (i8*, i32, ...) @v_printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i32 %81, i32 %83)
br label %89
89: ; preds = %85, %87, %76
%90 = phi i32 [ 1, %76 ], [ 0, %85 ], [ 1, %87 ]
%91 = call i32 @close(i32 %40)
ret i32 %90
}