#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 */

/* Variables and functions */
int O_CREAT;
int O_SYNC;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(unsigned int *);
unsigned int *malloc(size_t);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, unsigned int *, size_t);
int primes_file(size_t num, const char *filename) {
  int err = 0;
  int fid = open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_SYNC,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
  if (fid < 0) {
    perror("Failed to open output file");
    return -1;
  }
  unsigned int *prime_nums = malloc(num * sizeof(unsigned int));
  if (prime_nums == NULL) {
    fprintf(stderr, "ERROR: Failed to allocate memory.\n");
    err = -1;
    goto fail;
  }
  unsigned int n = 2;
  size_t k = 0;
  size_t i = 0;
  size_t w = 0;
  for (k = 0, n = 2; k < num; ++n) {
    for (i = 0; i < k && n % prime_nums[i] != 0; ++i)
      ;
    if (i == k) {
      prime_nums[k] = n;
      ++k;
    }
    if (k - w == 128) {
      write(fid, &(prime_nums[w]), 128 * sizeof(unsigned int));
      w += 128;
    }
  }
  write(fid, &(prime_nums[w]), (k - w) * sizeof(unsigned int));
fail:
  if (close(fid)) {
    perror("Failed to close input file");
  }
  free(prime_nums);
  return err;
}


x86 assembly (clang, -O0):
<primes_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    0x2e09(%rip),%rax
or     (%rax),%edx
mov    0x2de8(%rip),%rax
or     (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
mov    0x2dce(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1206 <primes_file+0x96>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <primes_file+0x232>
mov    -0x10(%rbp),%rdi
shl    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1243 <primes_file+0xd3>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1375 <primes_file+0x205>
movl   $0x2,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x2,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1352 <primes_file+0x1e2>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
cmp    -0x38(%rbp),%rcx
mov    %al,-0x49(%rbp)
jae    12b3 <primes_file+0x143>
mov    -0x2c(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    -0x40(%rbp),%rsi
xor    %edx,%edx
divl   (%rcx,%rsi,4)
cmp    $0x0,%edx
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    12c3 <primes_file+0x153>
jmp    12d9 <primes_file+0x169>
jmp    12c8 <primes_file+0x158>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1287 <primes_file+0x117>
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    1301 <primes_file+0x191>
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
sub    -0x48(%rbp),%rax
cmp    $0x80,%rax
jne    133f <primes_file+0x1cf>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    -0x48(%rbp),%rax
add    $0x80,%rax
mov    %rax,-0x48(%rbp)
jmp    1344 <primes_file+0x1d4>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1271 <primes_file+0x101>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x38(%rbp),%rdx
sub    -0x48(%rbp),%rdx
shl    $0x2,%rdx
call   1040 <write@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     1393 <primes_file+0x223>
lea    0xcb0(%rip),%rdi
call   1090 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<primes_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e76(%rip),%rcx
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e5c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfa(%rip),%rcx
mov    0x2e1b(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2df8(%rip),%rcx
or     (%rcx),%edx
mov    0x2e0f(%rip),%rcx
or     (%rcx),%edx
mov    %rdi,%r12
mov    0x2ddb(%rip),%rcx
or     (%rcx),%edx
mov    %rsi,%rdi
mov    %eax,%esi
call   1080 <open@plt>
test   %eax,%eax
js     1300 <primes_file+0x190>
mov    %eax,%r15d
lea    0x0(,%r12,4),%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     129a <primes_file+0x12a>
test   %r12,%r12
je     12b6 <primes_file+0x146>
mov    $0x2,%ebx
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    122c <primes_file+0xbc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%ebx
cmp    %r12,%rbp
jae    12bb <primes_file+0x14b>
xor    %ecx,%ecx
test   %rbp,%rbp
je     1260 <primes_file+0xf0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%eax
xor    %edx,%edx
divl   0x0(%r13,%rcx,4)
test   %edx,%edx
je     1260 <primes_file+0xf0>
add    $0x1,%rcx
cmp    %rcx,%rbp
jne    1240 <primes_file+0xd0>
jmp    1265 <primes_file+0xf5>
nopl   0x0(%rax,%rax,1)
cmp    %rbp,%rcx
jne    126e <primes_file+0xfe>
mov    %ebx,0x0(%r13,%rbp,4)
add    $0x1,%rbp
mov    %rbp,%rax
sub    %r14,%rax
cmp    $0x80,%rax
jne    1220 <primes_file+0xb0>
lea    0x0(,%r14,4),%rsi
add    %r13,%rsi
mov    $0x200,%edx
mov    %r15d,%edi
call   1040 <write@plt>
sub    $0xffffffffffffff80,%r14
jmp    1220 <primes_file+0xb0>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    12da <primes_file+0x16a>
xor    %ebp,%ebp
xor    %r14d,%r14d
lea    0x0(,%r14,4),%rsi
add    %r13,%rsi
sub    %r14,%rbp
shl    $0x2,%rbp
mov    %r15d,%edi
mov    %rbp,%rdx
call   1040 <write@plt>
xor    %ebx,%ebx
mov    %r15d,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    1313 <primes_file+0x1a3>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf9(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12ef <primes_file+0x17f>
lea    0xd24(%rip),%rdi
call   1090 <perror@plt>
jmp    12e7 <primes_file+0x177>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_SYNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str = [27 x i8] c"Failed to open output file\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"ERROR: Failed to allocate memory.\0A\00"
@.str.2 = [27 x i8] c"Failed to close input file\00"
define i32 @primes_file(i64 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @O_SYNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IWGRP
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IROTH
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %13, i32 %20, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %2
%34 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %116
35: ; preds = %2
%36 = load i64, i64* %4
%37 = mul i64 %36, 4
%38 = call i32* @malloc(i64 %37)
store i32* %38, i32** %8
%39 = load i32*, i32** %8
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %106
44: ; preds = %35
store i32 2, i32* %9
store i64 0, i64* %10
store i64 0, i64* %11
store i64 0, i64* %12
store i64 0, i64* %10
store i32 2, i32* %9
br label %45
45: ; preds = %93, %44
%46 = load i64, i64* %10
%47 = load i64, i64* %4
%48 = icmp ult i64 %46, %47
br i1 %48, label %49, label %96
49: ; preds = %45
store i64 0, i64* %11
br label %50
50: ; preds = %65, %49
%51 = load i64, i64* %11
%52 = load i64, i64* %10
%53 = icmp ult i64 %51, %52
br i1 %53, label %54, label %62
54: ; preds = %50
%55 = load i32, i32* %9
%56 = load i32*, i32** %8
%57 = load i64, i64* %11
%58 = getelementptr inbounds i32, i32* %56, i64 %57
%59 = load i32, i32* %58
%60 = urem i32 %55, %59
%61 = icmp ne i32 %60, 0
br label %62
62: ; preds = %54, %50
%63 = phi i1 [ false, %50 ], [ %61, %54 ]
br i1 %63, label %64, label %68
64: ; preds = %62
br label %65
65: ; preds = %64
%66 = load i64, i64* %11
%67 = add i64 %66, 1
store i64 %67, i64* %11
br label %50
68: ; preds = %62
%69 = load i64, i64* %11
%70 = load i64, i64* %10
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %79
72: ; preds = %68
%73 = load i32, i32* %9
%74 = load i32*, i32** %8
%75 = load i64, i64* %10
%76 = getelementptr inbounds i32, i32* %74, i64 %75
store i32 %73, i32* %76
%77 = load i64, i64* %10
%78 = add i64 %77, 1
store i64 %78, i64* %10
br label %79
79: ; preds = %72, %68
%80 = load i64, i64* %10
%81 = load i64, i64* %12
%82 = sub i64 %80, %81
%83 = icmp eq i64 %82, 128
br i1 %83, label %84, label %92
84: ; preds = %79
%85 = load i32, i32* %7
%86 = load i32*, i32** %8
%87 = load i64, i64* %12
%88 = getelementptr inbounds i32, i32* %86, i64 %87
%89 = call i32 @write(i32 %85, i32* %88, i64 512)
%90 = load i64, i64* %12
%91 = add i64 %90, 128
store i64 %91, i64* %12
br label %92
92: ; preds = %84, %79
br label %93
93: ; preds = %92
%94 = load i32, i32* %9
%95 = add i32 %94, 1
store i32 %95, i32* %9
br label %45
96: ; preds = %45
%97 = load i32, i32* %7
%98 = load i32*, i32** %8
%99 = load i64, i64* %12
%100 = getelementptr inbounds i32, i32* %98, i64 %99
%101 = load i64, i64* %10
%102 = load i64, i64* %12
%103 = sub i64 %101, %102
%104 = mul i64 %103, 4
%105 = call i32 @write(i32 %97, i32* %100, i64 %104)
br label %106
106: ; preds = %96, %41
%107 = load i32, i32* %7
%108 = call i64 @close(i32 %107)
%109 = icmp ne i64 %108, 0
br i1 %109, label %110, label %112
110: ; preds = %106
%111 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %112
112: ; preds = %110, %106
%113 = load i32*, i32** %8
%114 = call i32 @free(i32* %113)
%115 = load i32, i32* %6
store i32 %115, i32* %3
br label %116
116: ; preds = %112, %33
%117 = load i32, i32* %3
ret i32 %117
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_SYNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@.str = [27 x i8] c"Failed to open output file\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"ERROR: Failed to allocate memory.\0A\00"
@.str.2 = [27 x i8] c"Failed to close input file\00"
define i32 @primes_file(i64 %0, i8* nocapture %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IWGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = tail call i32 @open(i8* %1, i32 %9, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %79
23: ; preds = %2
%24 = shl i64 %0, 2
%25 = tail call i32* @malloc(i64 %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = icmp eq i64 %0, 0
br i1 %28, label %64, label %32
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %71
32: ; preds = %27, %60
%33 = phi i64 [ %61, %60 ], [ 0, %27 ]
%34 = phi i64 [ %53, %60 ], [ 0, %27 ]
%35 = phi i32 [ %62, %60 ], [ 2, %27 ]
%36 = icmp eq i64 %34, 0
br i1 %36, label %46, label %37
37: ; preds = %32, %43
%38 = phi i64 [ %44, %43 ], [ 0, %32 ]
%39 = getelementptr inbounds i32, i32* %25, i64 %38
%40 = load i32, i32* %39
%41 = urem i32 %35, %40
%42 = icmp eq i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %37
%44 = add nuw i64 %38, 1
%45 = icmp eq i64 %44, %34
br i1 %45, label %49, label %37
46: ; preds = %37, %32
%47 = phi i64 [ 0, %32 ], [ %38, %37 ]
%48 = icmp eq i64 %47, %34
br i1 %48, label %49, label %52
49: ; preds = %43, %46
%50 = getelementptr inbounds i32, i32* %25, i64 %34
store i32 %35, i32* %50
%51 = add i64 %34, 1
br label %52
52: ; preds = %49, %46
%53 = phi i64 [ %51, %49 ], [ %34, %46 ]
%54 = sub i64 %53, %33
%55 = icmp eq i64 %54, 128
br i1 %55, label %56, label %60
56: ; preds = %52
%57 = getelementptr inbounds i32, i32* %25, i64 %33
%58 = tail call i32 @write(i32 %19, i32* %57, i64 512)
%59 = add i64 %33, 128
br label %60
60: ; preds = %52, %56
%61 = phi i64 [ %59, %56 ], [ %33, %52 ]
%62 = add i32 %35, 1
%63 = icmp ult i64 %53, %0
br i1 %63, label %32, label %64
64: ; preds = %60, %27
%65 = phi i64 [ 0, %27 ], [ %53, %60 ]
%66 = phi i64 [ 0, %27 ], [ %61, %60 ]
%67 = getelementptr inbounds i32, i32* %25, i64 %66
%68 = sub i64 %65, %66
%69 = shl i64 %68, 2
%70 = tail call i32 @write(i32 %19, i32* %67, i64 %69)
br label %71
71: ; preds = %64, %29
%72 = phi i32 [ -1, %29 ], [ 0, %64 ]
%73 = tail call i64 @close(i32 %19)
%74 = icmp eq i64 %73, 0
br i1 %74, label %77, label %75
75: ; preds = %71
%76 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %77
77: ; preds = %75, %71
%78 = tail call i32 @free(i32* %25)
br label %79
79: ; preds = %77, %21
%80 = phi i32 [ -1, %21 ], [ %72, %77 ]
ret i32 %80
}