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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int,
                           char const *);
char *text_array_get(struct text_array *, int, int);
int text_array_save_range(struct text_array *t, const char *filename, int x,
                          int y, int w, int h) {
  int count = 0;
  FILE *file;
  int i, j;
  file = fopen(filename, "w");
  if (!file)
    return 0;
  for (j = y; y < (y + h); j++) {
    for (i = x; i < (x + w); i++) {
      const char *v = text_array_get(t, i, j);
      if (v) {
        fprintf(file, "%d %d %s\n", i, j, v);
        count++;
      }
    }
  }
  fclose(file);
  return count;
}


x86 assembly (clang, -O0):
<text_array_save_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe90(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1190 <text_array_save_range+0x50>
movl   $0x0,-0x4(%rbp)
jmp    1234 <text_array_save_range+0xf4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    -0x28(%rbp),%ecx
cmp    %ecx,%eax
jge    1225 <text_array_save_range+0xe5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    -0x24(%rbp),%ecx
cmp    %ecx,%eax
jge    1212 <text_array_save_range+0xd2>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%edx
call   1060 <text_array_get@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     11ff <text_array_save_range+0xbf>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    -0x48(%rbp),%r8
lea    0xe11(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1204 <text_array_save_range+0xc4>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ad <text_array_save_range+0x6d>
jmp    1217 <text_array_save_range+0xd7>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1196 <text_array_save_range+0x56>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<text_array_save_range>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r14d
mov    %r8,0x10(%rsp)
mov    %ecx,%ebp
mov    %rdx,0x8(%rsp)
mov    %rsi,%rax
mov    %rdi,%rbx
lea    0xe96(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11f8 <text_array_save_range+0xb8>
mov    %rax,%r12
test   %r14d,%r14d
jle    11f0 <text_array_save_range+0xb0>
mov    0x10(%rsp),%rax
mov    0x8(%rsp),%rcx
lea    (%rax,%rcx,1),%r15d
lea    0xe6a(%rip),%r13
jmp    11a3 <text_array_save_range+0x63>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmpl   $0x0,0x10(%rsp)
jle    11a0 <text_array_save_range+0x60>
mov    0x8(%rsp),%rax
mov    %eax,%r14d
jmp    11c9 <text_array_save_range+0x89>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r14d
cmp    %r15d,%r14d
jge    11a0 <text_array_save_range+0x60>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1060 <text_array_get@plt>
test   %rax,%rax
je     11c0 <text_array_save_range+0x80>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
mov    %ebp,%ecx
mov    %rax,%r8
call   1040 <fprintf@plt>
jmp    11c0 <text_array_save_range+0x80>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.text_array = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [10 x i8] c"%d %d %s\0A\00"
define i32 @text_array_save_range(%struct.text_array* %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca %struct.text_array*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
store %struct.text_array* %0, %struct.text_array** %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %14
%19 = load i8*, i8** %9
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %15
%21 = load i32*, i32** %15
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %6
store i32 0, i32* %7
br label %67
24: ; preds = %6
%25 = load i32, i32* %11
store i32 %25, i32* %17
br label %26
26: ; preds = %60, %24
%27 = load i32, i32* %11
%28 = load i32, i32* %11
%29 = load i32, i32* %13
%30 = add nsw i32 %28, %29
%31 = icmp slt i32 %27, %30
br i1 %31, label %32, label %63
32: ; preds = %26
%33 = load i32, i32* %10
store i32 %33, i32* %16
br label %34
34: ; preds = %56, %32
%35 = load i32, i32* %16
%36 = load i32, i32* %10
%37 = load i32, i32* %12
%38 = add nsw i32 %36, %37
%39 = icmp slt i32 %35, %38
br i1 %39, label %40, label %59
40: ; preds = %34
%41 = load %struct.text_array*, %struct.text_array** %8
%42 = load i32, i32* %16
%43 = load i32, i32* %17
%44 = call i8* @text_array_get(%struct.text_array* %41, i32 %42, i32 %43)
store i8* %44, i8** %18
%45 = load i8*, i8** %18
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %55
47: ; preds = %40
%48 = load i32*, i32** %15
%49 = load i32, i32* %16
%50 = load i32, i32* %17
%51 = load i8*, i8** %18
%52 = call i32 @fprintf(i32* %48, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %49, i32 %50, i8* %51)
%53 = load i32, i32* %14
%54 = add nsw i32 %53, 1
store i32 %54, i32* %14
br label %55
55: ; preds = %47, %40
br label %56
56: ; preds = %55
%57 = load i32, i32* %16
%58 = add nsw i32 %57, 1
store i32 %58, i32* %16
br label %34
59: ; preds = %34
br label %60
60: ; preds = %59
%61 = load i32, i32* %17
%62 = add nsw i32 %61, 1
store i32 %62, i32* %17
br label %26
63: ; preds = %26
%64 = load i32*, i32** %15
%65 = call i32 @fclose(i32* %64)
%66 = load i32, i32* %14
store i32 %66, i32* %7
br label %67
67: ; preds = %63, %23
%68 = load i32, i32* %7
ret i32 %68
}

LLVM IR (-O2):
%struct.text_array = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [10 x i8] c"%d %d %s\0A\00"
define i32 @text_array_save_range(%struct.text_array* %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %29, label %9
9: ; preds = %6
%10 = icmp sgt i32 %5, 0
br i1 %10, label %11, label %27
11: ; preds = %9
%12 = add nsw i32 %4, %2
%13 = icmp sgt i32 %4, 0
br label %14
14: ; preds = %25, %11
%15 = phi i32 [ %3, %11 ], [ %26, %25 ]
br i1 %13, label %16, label %25
16: ; preds = %14, %22
%17 = phi i32 [ %23, %22 ], [ %2, %14 ]
%18 = tail call i8* @text_array_get(%struct.text_array* %0, i32 %17, i32 %15)
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %15, i8* %18)
br label %22
22: ; preds = %20, %16
%23 = add nsw i32 %17, 1
%24 = icmp slt i32 %23, %12
br i1 %24, label %16, label %25
25: ; preds = %22, %14
%26 = add nsw i32 %15, 1
br label %14
27: ; preds = %9
%28 = tail call i32 @fclose(i32* %7)
br label %29
29: ; preds = %6, %27
ret i32 0
}