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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ stderr;
int write_ints(const char *file_name, int val, size_t count) {
  int status = 0;
  FILE *fd;
  struct stat filestat;
  size_t len;
  fd = fopen(file_name, "wb");
  if (fd < 0) {
    char cwd[1000 + 1];
    if (getcwd(cwd, 1000) == NULL) {
      return -1;
    }
    fprintf(stderr, "Could not open file [%s] \n", file_name);
    fprintf(stderr, "Currently in dir    [%s] \n", cwd);
    return -1;
  }
  for (int iter = 0; iter < count; iter++)
    fwrite(&iter, sizeof(iter), 1, fd);
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_ints>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    11f4 <write_ints+0xa4>
lea    -0x430(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    11b3 <write_ints+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <write_ints+0xfd>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    -0x430(%rbp),%rdx
lea    0xe3b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <write_ints+0xfd>
movl   $0x0,-0x434(%rbp)
movslq -0x434(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    123d <write_ints+0xed>
mov    -0x30(%rbp),%rcx
lea    -0x434(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    11fe <write_ints+0xae>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ints>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
lea    0xebd(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%rbx
movl   $0x0,0xc(%rsp)
test   %r14,%r14
je     1187 <write_ints+0x57>
lea    0xc(%rsp),%r15
nopl   (%rax)
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
movslq 0xc(%rsp),%rax
add    $0x1,%rax
mov    %eax,0xc(%rsp)
cmp    %r14,%rax
jb     1160 <write_ints+0x30>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Could not open file [%s] \0A\00"
@.str.2 = [27 x i8] c"Currently in dir [%s] \0A\00"
define i32 @write_ints(i8* %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %struct.stat
%11 = alloca i64
%12 = alloca [1001 x i8]
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i32 0, i32* %8
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp ult i32* %16, null
br i1 %17, label %18, label %30
18: ; preds = %3
%19 = getelementptr inbounds [1001 x i8], [1001 x i8]* %12, i64 0, i64 0
%20 = call i32* @getcwd(i8* %19, i32 1000)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %4
br label %45
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %5
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [1001 x i8], [1001 x i8]* %12, i64 0, i64 0
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 -1, i32* %4
br label %45
30: ; preds = %3
store i32 0, i32* %13
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %13
%33 = sext i32 %32 to i64
%34 = load i64, i64* %7
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i32*, i32** %9
%38 = call i32 @fwrite(i32* %13, i32 4, i32 1, i32* %37)
br label %39
39: ; preds = %36
%40 = load i32, i32* %13
%41 = add nsw i32 %40, 1
store i32 %41, i32* %13
br label %31
42: ; preds = %31
%43 = load i32*, i32** %9
%44 = call i32 @fclose(i32* %43)
store i32 0, i32* %4
br label %45
45: ; preds = %42, %23, %22
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
define i32 @write_ints(i8* nocapture %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = icmp eq i64 %2, 0
br i1 %7, label %8, label %10
8: ; preds = %10, %3
%9 = tail call i32 @fclose(i32* %5)
ret i32 0
10: ; preds = %3, %10
%11 = call i32 @fwrite(i32* %4, i32 4, i32 1, i32* %5)
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 1
store i32 %13, i32* %4
%14 = sext i32 %13 to i64
%15 = icmp ult i64 %14, %2
br i1 %15, label %10, label %8
}