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

/* Variables and functions */
int errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, ...);
void write_linearray_to_file(const char *file_name, const char **lines,
                             char *err, size_t err_len) {
  FILE *fp;
  const char **l;
  fp = fopen(file_name, "w");
  if (!fp) {
    int ret = errno;
    snprintf(err, err_len, "error opening '%s' for writing: %d\n", file_name,
             ret);
    return;
  }
  for (l = lines; *l; ++l) {
    if (fprintf(fp, "%s\n", *l) < 0) {
      snprintf(err, err_len, "error writing to '%s'\n", file_name);
      fclose(fp);
      return;
    }
  }
  if (fclose(fp)) {
    int ret = errno;
    snprintf(err, err_len, "error closing '%s' for writing: %d\n", file_name,
             ret);
    return;
  }
}


x86 assembly (clang, -O0):
<write_linearray_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a6 <write_linearray_to_file+0x66>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x34(%rbp),%r8d
lea    0xe68(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1258 <write_linearray_to_file+0x118>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     121b <write_linearray_to_file+0xdb>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
lea    0xe58(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    1205 <write_linearray_to_file+0xc5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe3a(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1258 <write_linearray_to_file+0x118>
jmp    120a <write_linearray_to_file+0xca>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
jmp    11ae <write_linearray_to_file+0x6e>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1258 <write_linearray_to_file+0x118>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x38(%rbp),%r8d
lea    0xdf0(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_linearray_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11bc <write_linearray_to_file+0x7c>
mov    %rax,%r13
mov    (%rbx),%rdx
test   %rdx,%rdx
je     119c <write_linearray_to_file+0x5c>
add    $0x8,%rbx
lea    0xea8(%rip),%rbp
xchg   %ax,%ax
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <fprintf@plt>
test   %rax,%rax
js     11eb <write_linearray_to_file+0xab>
mov    (%rbx),%rdx
add    $0x8,%rbx
test   %rdx,%rdx
jne    1180 <write_linearray_to_file+0x40>
mov    %r13,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     1218 <write_linearray_to_file+0xd8>
mov    0x2e30(%rip),%rax
mov    (%rax),%r8d
lea    0xe87(%rip),%rdx
jmp    11cd <write_linearray_to_file+0x8d>
mov    0x2e1d(%rip),%rax
mov    (%rax),%r8d
lea    0xe35(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <snprintf@plt>
lea    0xe38(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@errno = global i32 0
@.str.1 = [36 x i8] c"error opening '%s' for writing: %d\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [23 x i8] c"error writing to '%s'\0A\00"
@.str.4 = [36 x i8] c"error closing '%s' for writing: %d\0A\00"
define void @write_linearray_to_file(i8* %0, i8** %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = icmp ne i32* %15, null
br i1 %16, label %24, label %17
17: ; preds = %4
%18 = load i32, i32* @errno
store i32 %18, i32* %11
%19 = load i8*, i8** %7
%20 = load i64, i64* %8
%21 = load i8*, i8** %5
%22 = load i32, i32* %11
%23 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %19, i64 %20, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %21, i32 %22)
br label %58
24: ; preds = %4
%25 = load i8**, i8*** %6
store i8** %25, i8*** %10
br label %26
26: ; preds = %44, %24
%27 = load i8**, i8*** %10
%28 = load i8*, i8** %27
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %47
30: ; preds = %26
%31 = load i32*, i32** %9
%32 = load i8**, i8*** %10
%33 = load i8*, i8** %32
%34 = call i64 @fprintf(i32* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load i8*, i8** %7
%38 = load i64, i64* %8
%39 = load i8*, i8** %5
%40 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %37, i64 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32*, i32** %9
%42 = call i64 @fclose(i32* %41)
br label %58
43: ; preds = %30
br label %44
44: ; preds = %43
%45 = load i8**, i8*** %10
%46 = getelementptr inbounds i8*, i8** %45, i32 1
store i8** %46, i8*** %10
br label %26
47: ; preds = %26
%48 = load i32*, i32** %9
%49 = call i64 @fclose(i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i32, i32* @errno
store i32 %52, i32* %12
%53 = load i8*, i8** %7
%54 = load i64, i64* %8
%55 = load i8*, i8** %5
%56 = load i32, i32* %12
%57 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %53, i64 %54, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %55, i32 %56)
br label %58
58: ; preds = %17, %36, %51, %47
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@errno = global i32 0
@.str.1 = [36 x i8] c"error opening '%s' for writing: %d\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [23 x i8] c"error writing to '%s'\0A\00"
@.str.4 = [36 x i8] c"error closing '%s' for writing: %d\0A\00"
define void @write_linearray_to_file(i8* %0, i8** nocapture %1, i8* %2, i64 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = load i8*, i8** %1
%9 = icmp eq i8* %8, null
br i1 %9, label %25, label %16
10: ; preds = %4
%11 = load i32, i32* @errno
%12 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %11)
br label %31
13: ; preds = %16
%14 = load i8*, i8** %21
%15 = icmp eq i8* %14, null
br i1 %15, label %25, label %16
16: ; preds = %7, %13
%17 = phi i8* [ %14, %13 ], [ %8, %7 ]
%18 = phi i8** [ %21, %13 ], [ %1, %7 ]
%19 = tail call i64 @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%20 = icmp slt i64 %19, 0
%21 = getelementptr inbounds i8*, i8** %18, i64 1
br i1 %20, label %22, label %13
22: ; preds = %16
%23 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%24 = tail call i64 @fclose(i32* %5)
br label %31
25: ; preds = %13, %7
%26 = tail call i64 @fclose(i32* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @errno
%30 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %29)
br label %31
31: ; preds = %25, %28, %22, %10
ret void
}