#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 size_t u_int32_t;
typedef int /*<<< orphan*/ off_t;
typedef int /*<<< orphan*/ cur;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
size_t const GT0Offset(size_t);
scalar_t__ fwrite(void *, int, int, int /*<<< orphan*/ *);
size_t numGTs(int /*<<< orphan*/ const);
int writeGrainDirectory(const size_t offset, const off_t outsize, FILE *of) {
  size_t returner = 0;
  size_t i;
  size_t stop = numGTs(outsize);
  size_t start = offset + GT0Offset(stop);
  u_int32_t cur;
  for (i = 0; i < stop; i++) {
    cur = start + (i * 4);
    returner += fwrite((void *)&cur, sizeof(cur), 1, of);
  }
  return returner * sizeof(cur);
}


x86 assembly (clang, -O0):
<writeGrainDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <numGTs@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <GT0Offset@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    11cf <writeGrainDirectory+0x9f>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x8,%esi
mov    $0x1,%edx
call   1050 <fwrite@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    117e <writeGrainDirectory+0x4e>
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeGrainDirectory>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rdi,%r12
mov    %esi,%edi
call   1040 <numGTs@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <GT0Offset@plt>
test   %r15,%r15
je     119d <writeGrainDirectory+0x6d>
mov    %rax,%rbx
add    %r12,%rbx
xor    %r12d,%r12d
lea    0x8(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %rbx,0x8(%rsp)
mov    %r13,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
add    %rax,%r12
add    $0x4,%rbx
add    $0xffffffffffffffff,%r15
jne    1170 <writeGrainDirectory+0x40>
shl    $0x3,%r12d
jmp    11a0 <writeGrainDirectory+0x70>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @writeGrainDirectory(i64 %0, i32 %1, i32* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i64 %0, i64* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i64 0, i64* %7
%12 = load i32, i32* %5
%13 = call i64 @numGTs(i32 %12)
store i64 %13, i64* %9
%14 = load i64, i64* %4
%15 = load i64, i64* %9
%16 = call i64 @GT0Offset(i64 %15)
%17 = add i64 %14, %16
store i64 %17, i64* %10
store i64 0, i64* %8
br label %18
18: ; preds = %32, %3
%19 = load i64, i64* %8
%20 = load i64, i64* %9
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i64, i64* %10
%24 = load i64, i64* %8
%25 = mul i64 %24, 4
%26 = add i64 %23, %25
store i64 %26, i64* %11
%27 = bitcast i64* %11 to i8*
%28 = load i32*, i32** %6
%29 = call i64 @fwrite(i8* %27, i32 8, i32 1, i32* %28)
%30 = load i64, i64* %7
%31 = add i64 %30, %29
store i64 %31, i64* %7
br label %32
32: ; preds = %22
%33 = load i64, i64* %8
%34 = add i64 %33, 1
store i64 %34, i64* %8
br label %18
35: ; preds = %18
%36 = load i64, i64* %7
%37 = mul i64 %36, 8
%38 = trunc i64 %37 to i32
ret i32 %38
}

LLVM IR (-O2):
define i32 @writeGrainDirectory(i64 %0, i32 %1, i32* nocapture %2) {
%4 = alloca i64
%5 = tail call i64 @numGTs(i32 %1)
%6 = tail call i64 @GT0Offset(i64 %5)
%7 = add i64 %6, %0
%8 = bitcast i64* %4 to i8*
%9 = icmp eq i64 %5, 0
br i1 %9, label %22, label %10
10: ; preds = %3, %10
%11 = phi i64 [ %16, %10 ], [ 0, %3 ]
%12 = phi i64 [ %17, %10 ], [ 0, %3 ]
%13 = shl i64 %12, 2
%14 = add i64 %7, %13
store i64 %14, i64* %4
%15 = call i64 @fwrite(i8* %8, i32 8, i32 1, i32* %2)
%16 = add i64 %15, %11
%17 = add nuw i64 %12, 1
%18 = icmp eq i64 %17, %5
br i1 %18, label %19, label %10
19: ; preds = %10
%20 = trunc i64 %16 to i32
%21 = shl i32 %20, 3
br label %22
22: ; preds = %19, %3
%23 = phi i32 [ 0, %3 ], [ %21, %19 ]
ret i32 %23
}