#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
char mvinch(short, short);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ sound_bell();
void save_screen(void) {
  FILE *fp;
  short i, j;
  char buf[80 + 2];
  char found_non_blank;
  if ((fp = fopen("rogue.screen", "w")) != NULL) {
    for (i = 0; i < 24; i++) {
      found_non_blank = 0;
      for (j = (80 - 1); j >= 0; j--) {
        buf[j] = mvinch(i, j);
        if (!found_non_blank) {
          if ((buf[j] != ' ') || (j == 0)) {
            buf[j + ((j == 0) ? 0 : 1)] = 0;
            found_non_blank = 1;
          }
        }
      }
      fputs(buf, fp);
      putc('\n', fp);
    }
    fclose(fp);
  } else {
    sound_bell();
  }
}


x86 assembly (clang, -O0):
<save_screen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe91(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     126f <save_screen+0x10f>
movw   $0x0,-0xa(%rbp)
movswl -0xa(%rbp),%eax
cmp    $0x18,%eax
jge    1261 <save_screen+0x101>
movb   $0x0,-0x61(%rbp)
movw   $0x4f,-0xc(%rbp)
movswl -0xc(%rbp),%eax
cmp    $0x0,%eax
jl     1235 <save_screen+0xd5>
mov    -0xa(%rbp),%ax
movswl %ax,%edi
movswl -0xc(%rbp),%esi
call   1080 <mvinch@plt>
mov    %al,%cl
movswq -0xc(%rbp),%rax
mov    %cl,-0x60(%rbp,%rax,1)
cmpb   $0x0,-0x61(%rbp)
jne    121f <save_screen+0xbf>
movswq -0xc(%rbp),%rax
movsbl -0x60(%rbp,%rax,1),%eax
cmp    $0x20,%eax
jne    11f8 <save_screen+0x98>
movswl -0xc(%rbp),%eax
cmp    $0x0,%eax
jne    121a <save_screen+0xba>
movswl -0xc(%rbp),%eax
movswl -0xc(%rbp),%esi
mov    $0x1,%ecx
xor    %edx,%edx
cmp    $0x0,%esi
cmove  %edx,%ecx
add    %ecx,%eax
cltq
movb   $0x0,-0x60(%rbp,%rax,1)
movb   $0x1,-0x61(%rbp)
jmp    121f <save_screen+0xbf>
jmp    1224 <save_screen+0xc4>
mov    -0xc(%rbp),%ax
add    $0xffff,%ax
mov    %ax,-0xc(%rbp)
jmp    11a6 <save_screen+0x46>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x8(%rbp),%rsi
mov    $0xa,%edi
call   1050 <putc@plt>
mov    -0xa(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0xa(%rbp)
jmp    118f <save_screen+0x2f>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1276 <save_screen+0x116>
mov    $0x0,%al
call   1060 <sound_bell@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_screen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
lea    0xe8d(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     121f <save_screen+0xbf>
mov    %rax,%r15
xor    %ebp,%ebp
mov    %rsp,%r14
jmp    11c1 <save_screen+0x61>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <fputs@plt>
mov    $0xa,%edi
mov    %r15,%rsi
call   1050 <putc@plt>
add    $0x1,%ebp
cmp    $0x18,%bp
je     120b <save_screen+0xab>
mov    $0x4f,%ebx
xor    %ecx,%ecx
jmp    11e6 <save_screen+0x86>
cmp    $0x1,%rbx
mov    %ebx,%eax
sbb    $0xffffffff,%eax
movb   $0x0,(%rsp,%rax,1)
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbx
jae    11a0 <save_screen+0x40>
mov    %ecx,%r12d
movswl %bp,%edi
movswl %bx,%esi
call   1080 <mvinch@plt>
mov    %al,(%rsp,%rbx,1)
mov    $0x1,%cl
test   %r12b,%r12b
jne    11e0 <save_screen+0x80>
cmp    $0x20,%al
jne    11ca <save_screen+0x6a>
test   %rbx,%rbx
je     11ca <save_screen+0x6a>
xor    %ecx,%ecx
jmp    11e0 <save_screen+0x80>
mov    %r15,%rdi
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <sound_bell@plt>

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

LLVM IR (-O2):
@.str = [13 x i8] c"rogue.screen\00"
@.str.1 = [2 x i8] c"w\00"
define void @save_screen() {
%1 = alloca [82 x i8]
%2 = getelementptr inbounds [82 x i8], [82 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %35, label %5
5: ; preds = %0, %28
%6 = phi i16 [ %31, %28 ], [ 0, %0 ]
br label %7
7: ; preds = %5, %24
%8 = phi i64 [ 79, %5 ], [ %26, %24 ]
%9 = phi i8 [ 0, %5 ], [ %25, %24 ]
%10 = trunc i64 %8 to i16
%11 = tail call signext i8 @mvinch(i16 signext %6, i16 signext %10)
%12 = getelementptr inbounds [82 x i8], [82 x i8]* %1, i64 0, i64 %8
store i8 %11, i8* %12
%13 = icmp eq i8 %9, 0
br i1 %13, label %14, label %24
14: ; preds = %7
%15 = icmp ne i8 %11, 32
%16 = icmp eq i64 %8, 0
%17 = or i1 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = xor i1 %16, true
%20 = zext i1 %19 to i64
%21 = add nuw nsw i64 %8, %20
%22 = and i64 %21, 4294967295
%23 = getelementptr inbounds [82 x i8], [82 x i8]* %1, i64 0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %7, %14, %18
%25 = phi i8 [ 1, %7 ], [ 1, %18 ], [ 0, %14 ]
%26 = add nsw i64 %8, -1
%27 = icmp eq i64 %8, 0
br i1 %27, label %28, label %7
28: ; preds = %24
%29 = call i32 @fputs(i8* %2, i32* %3)
%30 = tail call i32 @putc(i8 signext 10, i32* %3)
%31 = add nuw nsw i16 %6, 1
%32 = icmp eq i16 %31, 24
br i1 %32, label %33, label %5
33: ; preds = %28
%34 = tail call i32 @fclose(i32* %3)
br label %37
35: ; preds = %0
%36 = tail call i32 (...) @sound_bell()
br label %37
37: ; preds = %35, %33
ret void
}