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

/* Variables and functions */
int /*<<< orphan*/ COLS;
int /*<<< orphan*/ LINES;
int /*<<< orphan*/ box(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/);
int /*<<< orphan*/ endwin();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ getmaxyx(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ mvwaddch(int /*<<< orphan*/ *, int, int, char);
int /*<<< orphan*/ mvwprintw(int /*<<< orphan*/ *, int, int, char *, int, int);
int /*<<< orphan*/ *newterm(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *);
int /*<<< orphan*/ *newwin(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ set_term(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ wgetch(int /*<<< orphan*/ *);
int /*<<< orphan*/ wrefresh(int /*<<< orphan*/ *);
int main() {
  WINDOW *win;
  SCREEN *scr;
  FILE *out, *in, *to_in;
  int pd[2];
  int i, j, maxy, maxx;
  if ((out = fopen("/tmp/curses_redir.out", "w")) == NULL) {
    fprintf(stderr, "error opening output file\n");
    exit(1);
  }
  if (pipe(pd) != 0) {
    fprintf(stderr, "pipe error: %s\n", strerror(errno));
    exit(1);
  }
  if ((in = fdopen(pd[0], "r")) == NULL) {
    fprintf(stderr, "error opening input file\n");
    exit(1);
  }
  if ((to_in = fdopen(pd[1], "w")) == NULL) {
    fprintf(stderr, "error opening to_in file *\n");
    exit(1);
  }
  if ((scr = newterm(NULL, out, in)) == NULL) {
    fprintf(stderr, "newterm failed\n");
    exit(1);
  }
  set_term(scr);
  win = newwin(LINES, COLS, 0, 0);
  if (win == NULL) {
    fprintf(stderr, "error creating win\n");
    exit(1);
  }
  box(win, 0, 0);
  getmaxyx(win, maxy, maxx);
  for (i = 1; i < maxy - 1; i++)
    for (j = 1; j < maxx - 1; j++) {
      mvwaddch(win, i, j, '+');
    }
  mvwprintw(win, 5, 5, "curses_redir dims: %d, %d", maxy, maxx);
  wrefresh(win);
  fprintf(to_in, "\n");
  fflush(to_in);
  wgetch(win);
  for (i = 1; i < maxy - 1; i++)
    for (j = 1; j < maxx - 1; j++) {
      mvwaddch(win, i, j, (j % 2 == 0) ? '=' : '*');
    }
  mvwprintw(win, 0, 0, "curses_redir dims: %d, %d", maxy, maxx);
  wrefresh(win);
  fprintf(to_in, "\n");
  fflush(to_in);
  wgetch(win);
  endwin();
  fclose(out);
  fclose(in);
  fclose(to_in);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xdca(%rip),%rdi
lea    0xdd9(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1272 <main+0x52>
mov    0x2d79(%rip),%rax
mov    (%rax),%rdi
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
lea    -0x38(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     12c0 <main+0xa0>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1140 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rdx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x38(%rbp),%edi
lea    0xd79(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12ff <main+0xdf>
mov    0x2cec(%rip),%rax
mov    (%rax),%rdi
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x34(%rbp),%edi
lea    0xd0d(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    133e <main+0x11e>
mov    0x2cad(%rip),%rax
mov    (%rax),%rdi
lea    0xd32(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <newterm@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    137f <main+0x15f>
mov    0x2c6c(%rip),%rax
mov    (%rax),%rdi
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1100 <set_term@plt>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
mov    0x2c48(%rip),%rax
mov    (%rax),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1050 <newwin@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    13d4 <main+0x1b4>
mov    0x2c17(%rip),%rax
mov    (%rax),%rdi
lea    0xcc8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <box@plt>
mov    -0x10(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x48(%rbp),%edx
call   1070 <getmaxyx@plt>
movl   $0x1,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x44(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1455 <main+0x235>
movl   $0x1,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    -0x48(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1442 <main+0x222>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%edx
mov    $0x2b,%ecx
call   1120 <mvwaddch@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    140f <main+0x1ef>
jmp    1447 <main+0x227>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    13f7 <main+0x1d7>
mov    -0x10(%rbp),%rdi
mov    -0x44(%rbp),%r8d
mov    -0x48(%rbp),%r9d
mov    $0x5,%edx
lea    0xc32(%rip),%rcx
mov    %edx,%esi
call   10f0 <mvwprintw@plt>
mov    -0x10(%rbp),%rdi
call   1040 <wrefresh@plt>
mov    -0x30(%rbp),%rdi
lea    0xbf1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <fflush@plt>
mov    -0x10(%rbp),%rdi
call   10e0 <wgetch@plt>
movl   $0x1,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x44(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1529 <main+0x309>
movl   $0x1,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    -0x48(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1516 <main+0x2f6>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x40(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,%r8d
mov    -0x54(%rbp),%edx
mov    $0x2a,%eax
mov    $0x3d,%ecx
cmp    $0x0,%r8d
cmove  %ecx,%eax
movsbl %al,%ecx
call   1120 <mvwaddch@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    14c0 <main+0x2a0>
jmp    151b <main+0x2fb>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    14a8 <main+0x288>
mov    -0x10(%rbp),%rdi
mov    -0x44(%rbp),%r8d
mov    -0x48(%rbp),%r9d
xor    %edx,%edx
lea    0xb61(%rip),%rcx
mov    %edx,%esi
call   10f0 <mvwprintw@plt>
mov    -0x10(%rbp),%rdi
call   1040 <wrefresh@plt>
mov    -0x30(%rbp),%rdi
lea    0xb20(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <fflush@plt>
mov    -0x10(%rbp),%rdi
call   10e0 <wgetch@plt>
mov    $0x0,%al
call   10d0 <endwin@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xdbc(%rip),%rdi
lea    0xdcb(%rip),%rsi
call   1120 <fopen@plt>
test   %rax,%rax
je     13a4 <main+0x174>
mov    %rax,%r14
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    13bf <main+0x18f>
mov    0x8(%rsp),%edi
lea    0xdc9(%rip),%rsi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     13f5 <main+0x1c5>
mov    %rax,%r15
mov    0xc(%rsp),%edi
lea    0xd80(%rip),%rsi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     140d <main+0x1dd>
mov    %rax,%r12
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <newterm@plt>
test   %rax,%rax
je     1425 <main+0x1f5>
mov    %rax,%rdi
call   1110 <set_term@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <newwin@plt>
test   %rax,%rax
je     143d <main+0x20d>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <box@plt>
mov    %rbx,%rdi
call   1080 <getmaxyx@plt>
lea    0xd98(%rip),%r13
mov    %rbx,%rdi
mov    $0x5,%esi
mov    $0x5,%edx
mov    %r13,%rcx
call   1100 <mvwprintw@plt>
mov    %rbx,%rdi
call   1040 <wrefresh@plt>
mov    $0xa,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    %r12,%rdi
call   10b0 <fflush@plt>
mov    %rbx,%rdi
call   10f0 <wgetch@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r13,%rcx
call   1100 <mvwprintw@plt>
mov    %rbx,%rdi
call   1040 <wrefresh@plt>
mov    $0xa,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    %r12,%rdi
call   10b0 <fflush@plt>
mov    %rbx,%rdi
call   10f0 <wgetch@plt>
xor    %eax,%eax
call   10e0 <endwin@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2c25(%rip),%rax
mov    (%rax),%rcx
lea    0xc63(%rip),%rdi
mov    $0x1a,%esi
jmp    1453 <main+0x223>
mov    0x2c0a(%rip),%rax
mov    (%rax),%rbx
mov    0x2bf8(%rip),%rax
mov    (%rax),%edi
call   1150 <strerror@plt>
lea    0xc55(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    0x2bd4(%rip),%rax
mov    (%rax),%rcx
lea    0xc3f(%rip),%rdi
mov    $0x19,%esi
jmp    1453 <main+0x223>
mov    0x2bbc(%rip),%rax
mov    (%rax),%rcx
lea    0xc41(%rip),%rdi
mov    $0x1b,%esi
jmp    1453 <main+0x223>
mov    0x2ba4(%rip),%rax
mov    (%rax),%rcx
lea    0xc45(%rip),%rdi
mov    $0xf,%esi
jmp    1453 <main+0x223>
mov    0x2b8c(%rip),%rax
mov    (%rax),%rcx
lea    0xc3d(%rip),%rdi
mov    $0x13,%esi
mov    $0x1,%edx
call   1140 <fwrite@plt>
mov    $0x1,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"/tmp/curses_redir.out\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [27 x i8] c"error opening output file\0A\00"
@.str.3 = [16 x i8] c"pipe error: %s\0A\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [26 x i8] c"error opening input file\0A\00"
@.str.6 = [28 x i8] c"error opening to_in file *\0A\00"
@.str.7 = [16 x i8] c"newterm failed\0A\00"
@LINES = global i32 0
@COLS = global i32 0
@.str.8 = [20 x i8] c"error creating win\0A\00"
@.str.9 = [26 x i8] c"curses_redir dims: %d, %d\00"
@.str.10 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
%12 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i32*, i32** @stderr
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%20 = call i64 @pipe(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32*, i32** @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32* @fdopen(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %31, i32** %5
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32*, i32** @stderr
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32* @fdopen(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %6
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32*, i32** @stderr
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %37
%47 = load i32*, i32** %4
%48 = load i32*, i32** %5
%49 = call i32* @newterm(i32* null, i32* %47, i32* %48)
store i32* %49, i32** %3
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = load i32*, i32** @stderr
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %46
%56 = load i32*, i32** %3
%57 = call i32 @set_term(i32* %56)
%58 = load i32, i32* @LINES
%59 = load i32, i32* @COLS
%60 = call i32* @newwin(i32 %58, i32 %59, i32 0, i32 0)
store i32* %60, i32** %2
%61 = load i32*, i32** %2
%62 = icmp eq i32* %61, null
br i1 %62, label %63, label %67
63: ; preds = %55
%64 = load i32*, i32** @stderr
%65 = call i32 (i32*, i8*, ...) @fprintf(i32* %64, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %55
%68 = load i32*, i32** %2
%69 = call i32 @box(i32* %68, i32 0, i32 0)
%70 = load i32*, i32** %2
%71 = load i32, i32* %10
%72 = load i32, i32* %11
%73 = call i32 @getmaxyx(i32* %70, i32 %71, i32 %72)
store i32 1, i32* %8
br label %74
74: ; preds = %94, %67
%75 = load i32, i32* %8
%76 = load i32, i32* %10
%77 = sub nsw i32 %76, 1
%78 = icmp slt i32 %75, %77
br i1 %78, label %79, label %97
79: ; preds = %74
store i32 1, i32* %9
br label %80
80: ; preds = %90, %79
%81 = load i32, i32* %9
%82 = load i32, i32* %11
%83 = sub nsw i32 %82, 1
%84 = icmp slt i32 %81, %83
br i1 %84, label %85, label %93
85: ; preds = %80
%86 = load i32*, i32** %2
%87 = load i32, i32* %8
%88 = load i32, i32* %9
%89 = call i32 @mvwaddch(i32* %86, i32 %87, i32 %88, i8 signext 43)
br label %90
90: ; preds = %85
%91 = load i32, i32* %9
%92 = add nsw i32 %91, 1
store i32 %92, i32* %9
br label %80
93: ; preds = %80
br label %94
94: ; preds = %93
%95 = load i32, i32* %8
%96 = add nsw i32 %95, 1
store i32 %96, i32* %8
br label %74
97: ; preds = %74
%98 = load i32*, i32** %2
%99 = load i32, i32* %10
%100 = load i32, i32* %11
%101 = call i32 @mvwprintw(i32* %98, i32 5, i32 5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 %99, i32 %100)
%102 = load i32*, i32** %2
%103 = call i32 @wrefresh(i32* %102)
%104 = load i32*, i32** %6
%105 = call i32 (i32*, i8*, ...) @fprintf(i32* %104, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%106 = load i32*, i32** %6
%107 = call i32 @fflush(i32* %106)
%108 = load i32*, i32** %2
%109 = call i32 @wgetch(i32* %108)
store i32 1, i32* %8
br label %110
110: ; preds = %136, %97
%111 = load i32, i32* %8
%112 = load i32, i32* %10
%113 = sub nsw i32 %112, 1
%114 = icmp slt i32 %111, %113
br i1 %114, label %115, label %139
115: ; preds = %110
store i32 1, i32* %9
br label %116
116: ; preds = %132, %115
%117 = load i32, i32* %9
%118 = load i32, i32* %11
%119 = sub nsw i32 %118, 1
%120 = icmp slt i32 %117, %119
br i1 %120, label %121, label %135
121: ; preds = %116
%122 = load i32*, i32** %2
%123 = load i32, i32* %8
%124 = load i32, i32* %9
%125 = load i32, i32* %9
%126 = srem i32 %125, 2
%127 = icmp eq i32 %126, 0
%128 = zext i1 %127 to i64
%129 = select i1 %127, i32 61, i32 42
%130 = trunc i32 %129 to i8
%131 = call i32 @mvwaddch(i32* %122, i32 %123, i32 %124, i8 signext %130)
br label %132
132: ; preds = %121
%133 = load i32, i32* %9
%134 = add nsw i32 %133, 1
store i32 %134, i32* %9
br label %116
135: ; preds = %116
br label %136
136: ; preds = %135
%137 = load i32, i32* %8
%138 = add nsw i32 %137, 1
store i32 %138, i32* %8
br label %110
139: ; preds = %110
%140 = load i32*, i32** %2
%141 = load i32, i32* %10
%142 = load i32, i32* %11
%143 = call i32 @mvwprintw(i32* %140, i32 0, i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 %141, i32 %142)
%144 = load i32*, i32** %2
%145 = call i32 @wrefresh(i32* %144)
%146 = load i32*, i32** %6
%147 = call i32 (i32*, i8*, ...) @fprintf(i32* %146, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%148 = load i32*, i32** %6
%149 = call i32 @fflush(i32* %148)
%150 = load i32*, i32** %2
%151 = call i32 @wgetch(i32* %150)
%152 = call i32 (...) @endwin()
%153 = load i32*, i32** %4
%154 = call i32 @fclose(i32* %153)
%155 = load i32*, i32** %5
%156 = call i32 @fclose(i32* %155)
%157 = load i32*, i32** %6
%158 = call i32 @fclose(i32* %157)
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"/tmp/curses_redir.out\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [27 x i8] c"error opening output file\0A\00"
@.str.3 = [16 x i8] c"pipe error: %s\0A\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [26 x i8] c"error opening input file\0A\00"
@.str.6 = [28 x i8] c"error opening to_in file *\0A\00"
@.str.7 = [16 x i8] c"newterm failed\0A\00"
@LINES = global i32 0
@COLS = global i32 0
@.str.8 = [20 x i8] c"error creating win\0A\00"
@.str.9 = [26 x i8] c"curses_redir dims: %d, %d\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 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 %5, label %9
5: ; preds = %0
%6 = load i32*, i32** @stderr
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 26, i64 1, i32* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %9
%14 = load i32*, i32** @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = load i32, i32* %10
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32*, i32** @stderr
%25 = call i64 @fwrite(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i64 25, i64 1, i32* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32*, i32** @stderr
%34 = call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 27, i64 1, i32* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = call i32* @newterm(i32* null, i32* %3, i32* %21)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32*, i32** @stderr
%41 = call i64 @fwrite(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i64 15, i64 1, i32* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = call i32 @set_term(i32* %37)
%45 = load i32, i32* @LINES
%46 = load i32, i32* @COLS
%47 = call i32* @newwin(i32 %45, i32 %46, i32 0, i32 0)
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %53
49: ; preds = %43
%50 = load i32*, i32** @stderr
%51 = call i64 @fwrite(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i64 19, i64 1, i32* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %43
%54 = call i32 @box(i32* %47, i32 0, i32 0)
%55 = call i32 @getmaxyx(i32* %47, i32 undef, i32 undef)
%56 = call i32 @mvwprintw(i32* %47, i32 5, i32 5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 undef, i32 undef)
%57 = call i32 @wrefresh(i32* %47)
%58 = call i32 @fputc(i32 10, i32* %30)
%59 = call i32 @fflush(i32* %30)
%60 = call i32 @wgetch(i32* %47)
%61 = call i32 @mvwprintw(i32* %47, i32 0, i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 undef, i32 undef)
%62 = call i32 @wrefresh(i32* %47)
%63 = call i32 @fputc(i32 10, i32* %30)
%64 = call i32 @fflush(i32* %30)
%65 = call i32 @wgetch(i32* %47)
%66 = call i32 (...) @endwin()
%67 = call i32 @fclose(i32* %3)
%68 = call i32 @fclose(i32* %21)
%69 = call i32 @fclose(i32* %30)
ret i32 0
}