#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*/ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int write_posns(const char *path, uint32_t *saved_posns, size_t size) {
  FILE *f = NULL;
  size_t nwritten = 0;
  int ii = 0;
  uint32_t pos = 0;
  f = fopen(path, "w+");
  if (NULL == f) {
    return 1;
  }
  for (ii = 0; ii < size; ++ii) {
    pos = htonl(saved_posns[ii]);
    nwritten = fwrite(&pos, sizeof(uint32_t), 1, f);
    if (nwritten < 1) {
      fclose(f);
      return 1;
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<write_posns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    119e <write_posns+0x5e>
movl   $0x1,-0x4(%rbp)
jmp    1224 <write_posns+0xe4>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1214 <write_posns+0xd4>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <htonl@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jae    1201 <write_posns+0xc1>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1224 <write_posns+0xe4>
jmp    1206 <write_posns+0xc6>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a5 <write_posns+0x65>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_posns>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xea8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11bc <write_posns+0x7c>
mov    %rax,%r13
test   %r14,%r14
je     11b0 <write_posns+0x70>
xor    %ebx,%ebx
lea    0x4(%rsp),%r12
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1060 <fwrite@plt>
test   %rax,%rax
je     11b2 <write_posns+0x72>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1180 <write_posns+0x40>
xor    %ebp,%ebp
mov    %r13,%rdi
call   1030 <fclose@plt>
jmp    11c1 <write_posns+0x81>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
define i32 @write_posns(i8* %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i32* null, i32** %8
store i64 0, i64* %9
store i32 0, i32* %10
store i32 0, i32* %11
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp eq i32* null, %14
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 1, i32* %4
br label %44
17: ; preds = %3
store i32 0, i32* %10
br label %18
18: ; preds = %38, %17
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = load i64, i64* %7
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %41
23: ; preds = %18
%24 = load i32*, i32** %6
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 @htonl(i32 %28)
store i32 %29, i32* %11
%30 = load i32*, i32** %8
%31 = call i64 @fwrite(i32* %11, i32 4, i32 1, i32* %30)
store i64 %31, i64* %9
%32 = load i64, i64* %9
%33 = icmp ult i64 %32, 1
br i1 %33, label %34, label %37
34: ; preds = %23
%35 = load i32*, i32** %8
%36 = call i32 @fclose(i32* %35)
store i32 1, i32* %4
br label %44
37: ; preds = %23
br label %38
38: ; preds = %37
%39 = load i32, i32* %10
%40 = add nsw i32 %39, 1
store i32 %40, i32* %10
br label %18
41: ; preds = %18
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %4
br label %44
44: ; preds = %41, %34, %16
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
define i32 @write_posns(i8* nocapture %0, i32* nocapture %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %23, label %8
8: ; preds = %3
%9 = icmp eq i64 %2, 0
br i1 %9, label %20, label %12
10: ; preds = %12
%11 = icmp eq i64 %19, %2
br i1 %11, label %20, label %12
12: ; preds = %8, %10
%13 = phi i64 [ %19, %10 ], [ 0, %8 ]
%14 = getelementptr inbounds i32, i32* %1, i64 %13
%15 = load i32, i32* %14
%16 = tail call i32 @htonl(i32 %15)
store i32 %16, i32* %4
%17 = call i64 @fwrite(i32* %4, i32 4, i32 1, i32* %6)
%18 = icmp eq i64 %17, 0
%19 = add nuw i64 %13, 1
br i1 %18, label %20, label %10
20: ; preds = %10, %12, %8
%21 = phi i32 [ 0, %8 ], [ 1, %12 ], [ 0, %10 ]
%22 = tail call i32 @fclose(i32* %6)
br label %23
23: ; preds = %20, %3
%24 = phi i32 [ 1, %3 ], [ %21, %20 ]
ret i32 %24
}