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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  char cmd_output[1024];
  char cmd_exp[1024];
  FILE *fp_exec, *fp_out, *fp_exp;
  if (argc < 3) {
    printf("Syntax: test_output test output expected");
    exit(-1);
  }
  if (!(fp_exec = popen(argv[1], "r"))) {
    printf("Failed to run %s\n", argv[1]);
    exit(-1);
  }
  if (!(fp_out = fopen(argv[2], "w"))) {
    printf("Failed to open %s for write\n", argv[2]);
    exit(-1);
  }
  while (NULL != fgets(cmd_output, sizeof(cmd_output - 1), fp_exec)) {
    fputs(cmd_output, fp_out);
  }
  pclose(fp_exec);
  fflush(fp_out);
  fclose(fp_out);
  if (argc == 3)
    return 0;
  if (!(fp_exp = fopen(argv[3], "r"))) {
    return (0);
  }
  if (!(fp_out = fopen(argv[2], "r"))) {
    printf("Failed to open %s for read\n", argv[2]);
    exit(-1);
  }
  while (fgets(cmd_exp, sizeof(cmd_exp) - 1, fp_exp)) {
    if (!fgets(cmd_output, sizeof(cmd_output) - 1, fp_out)) {
      printf("Can't read from output file\n");
      goto error;
    }
    if (strcmp(cmd_output, cmd_exp)) {
      printf("output and expected output are different\n");
      goto error;
    }
  }
  return 0;
error:
  fclose(fp_exp);
  fclose(fp_out);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11db <main+0x3b>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3f(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x818(%rbp)
cmp    $0x0,%rax
jne    1220 <main+0x80>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe0e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x820(%rbp)
cmp    $0x0,%rax
jne    1265 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    126a <main+0xca>
lea    -0x410(%rbp),%rdi
mov    -0x818(%rbp),%rdx
mov    $0x8,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12a8 <main+0x108>
lea    -0x410(%rbp),%rdi
mov    -0x820(%rbp),%rsi
call   1060 <fputs@plt>
jmp    126a <main+0xca>
mov    -0x818(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x820(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x820(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x3,-0x8(%rbp)
jne    12e2 <main+0x142>
movl   $0x0,-0x4(%rbp)
jmp    1414 <main+0x274>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xd38(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x828(%rbp)
cmp    $0x0,%rax
jne    1313 <main+0x173>
movl   $0x0,-0x4(%rbp)
jmp    1414 <main+0x274>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd07(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x820(%rbp)
cmp    $0x0,%rax
jne    1358 <main+0x1b8>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    135d <main+0x1bd>
lea    -0x810(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    $0x3ff,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     13e9 <main+0x249>
lea    -0x410(%rbp),%rdi
mov    -0x820(%rbp),%rdx
mov    $0x3ff,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    13b4 <main+0x214>
lea    0xcd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13f5 <main+0x255>
lea    -0x410(%rbp),%rdi
lea    -0x810(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     13e4 <main+0x244>
lea    0xcbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13f5 <main+0x255>
jmp    135d <main+0x1bd>
movl   $0x0,-0x4(%rbp)
jmp    1414 <main+0x274>
mov    -0x828(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x820(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
cmp    $0x2,%edi
jle    1324 <main+0x174>
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x8(%rsi),%rdi
lea    0xe4e(%rip),%rsi
call   10b0 <popen@plt>
test   %rax,%rax
je     133c <main+0x18c>
mov    %rax,%r14
mov    0x10(%r15),%rdi
lea    0xe46(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1349 <main+0x199>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x8,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1240 <main+0x90>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1220 <main+0x70>
mov    %r14,%rdi
call   1060 <pclose@plt>
mov    %rbp,%rdi
call   10a0 <fflush@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %r14d,%r14d
cmp    $0x3,%r12d
je     130f <main+0x15f>
mov    0x18(%r15),%rdi
lea    0xdb9(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     130f <main+0x15f>
mov    %rax,%r13
mov    0x10(%r15),%rdi
lea    0xd9d(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1356 <main+0x1a6>
mov    %rax,%r12
lea    0x400(%rsp),%rbx
mov    %rsp,%rbp
lea    0xdc9(%rip),%r15
nop
mov    %rbx,%rdi
mov    $0x3ff,%esi
mov    %r13,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     130f <main+0x15f>
mov    %rbp,%rdi
mov    $0x3ff,%esi
mov    %r12,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12f1 <main+0x141>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12b0 <main+0x100>
lea    0xda3(%rip),%r15
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    0x8(%r15),%rsi
lea    0xce4(%rip),%rdi
jmp    1361 <main+0x1b1>
mov    0x10(%r15),%rsi
lea    0xceb(%rip),%rdi
jmp    1361 <main+0x1b1>
mov    0x10(%r15),%rsi
lea    0xcfb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Syntax: test_output test output expected\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"Failed to run %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [29 x i8] c"Failed to open %s for write\0A\00"
@.str.5 = [28 x i8] c"Failed to open %s for read\0A\00"
@.str.6 = [29 x i8] c"Can't read from output file\0A\00"
@.str.7 = [42 x i8] c"output and expected output are different\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @popen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %28, label %22
22: ; preds = %16
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %16
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %32, i32** %9
%33 = icmp ne i32* %32, null
br i1 %33, label %40, label %34
34: ; preds = %28
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %28
br label %41
41: ; preds = %46, %40
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%43 = load i32*, i32** %8
%44 = call i32* @fgets(i8* %42, i32 8, i32* %43)
%45 = icmp ne i32* null, %44
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = load i32*, i32** %9
%49 = call i32 @fputs(i8* %47, i32* %48)
br label %41
50: ; preds = %41
%51 = load i32*, i32** %8
%52 = call i32 @pclose(i32* %51)
%53 = load i32*, i32** %9
%54 = call i32 @fflush(i32* %53)
%55 = load i32*, i32** %9
%56 = call i32 @fclose(i32* %55)
%57 = load i32, i32* %4
%58 = icmp eq i32 %57, 3
br i1 %58, label %59, label %60
59: ; preds = %50
store i32 0, i32* %3
br label %106
60: ; preds = %50
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 3
%63 = load i8*, i8** %62
%64 = call i32* @fopen(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %64, i32** %10
%65 = icmp ne i32* %64, null
br i1 %65, label %67, label %66
66: ; preds = %60
store i32 0, i32* %3
br label %106
67: ; preds = %60
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 2
%70 = load i8*, i8** %69
%71 = call i32* @fopen(i8* %70, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %71, i32** %9
%72 = icmp ne i32* %71, null
br i1 %72, label %79, label %73
73: ; preds = %67
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 2
%76 = load i8*, i8** %75
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %76)
%78 = call i32 @exit(i32 -1)
unreachable
79: ; preds = %67
br label %80
80: ; preds = %99, %79
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%82 = load i32*, i32** %10
%83 = call i32* @fgets(i8* %81, i32 1023, i32* %82)
%84 = icmp ne i32* %83, null
br i1 %84, label %85, label %100
85: ; preds = %80
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%87 = load i32*, i32** %9
%88 = call i32* @fgets(i8* %86, i32 1023, i32* %87)
%89 = icmp ne i32* %88, null
br i1 %89, label %92, label %90
90: ; preds = %85
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %101
92: ; preds = %85
%93 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%94 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%95 = call i64 @strcmp(i8* %93, i8* %94)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %92
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0))
br label %101
99: ; preds = %92
br label %80
100: ; preds = %80
store i32 0, i32* %3
br label %106
101: ; preds = %97, %90
%102 = load i32*, i32** %10
%103 = call i32 @fclose(i32* %102)
%104 = load i32*, i32** %9
%105 = call i32 @fclose(i32* %104)
store i32 1, i32* %3
br label %106
106: ; preds = %101, %100, %66, %59
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@.str = [41 x i8] c"Syntax: test_output test output expected\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"Failed to run %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [29 x i8] c"Failed to open %s for write\0A\00"
@.str.5 = [28 x i8] c"Failed to open %s for read\0A\00"
@str = [28 x i8] c"Can't read from output file\00"
@str.8 = [41 x i8] c"output and expected output are different\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @popen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i8*, i8** %12
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %11
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = call i32* @fgets(i8* %5, i32 8, i32* %14)
%27 = icmp eq i32* %26, null
br i1 %27, label %36, label %32
28: ; preds = %20
%29 = load i8*, i8** %21
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %25, %32
%33 = call i32 @fputs(i8* %5, i32* %23)
%34 = call i32* @fgets(i8* %5, i32 8, i32* %14)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %32
36: ; preds = %32, %25
%37 = call i32 @pclose(i32* %14)
%38 = call i32 @fflush(i32* %23)
%39 = call i32 @fclose(i32* %23)
%40 = icmp eq i32 %0, 3
br i1 %40, label %68, label %41
41: ; preds = %36
%42 = getelementptr inbounds i8*, i8** %1, i64 3
%43 = load i8*, i8** %42
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%45 = icmp eq i32* %44, null
br i1 %45, label %68, label %46
46: ; preds = %41
%47 = load i8*, i8** %21
%48 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i8*, i8** %21
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %46, %60
%55 = call i32* @fgets(i8* %6, i32 1023, i32* %44)
%56 = icmp eq i32* %55, null
br i1 %56, label %68, label %57
57: ; preds = %54
%58 = call i32* @fgets(i8* %5, i32 1023, i32* %48)
%59 = icmp eq i32* %58, null
br i1 %59, label %63, label %60
60: ; preds = %57
%61 = call i64 @strcmp(i8* %5, i8* %6)
%62 = icmp eq i64 %61, 0
br i1 %62, label %54, label %63
63: ; preds = %60, %57
%64 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0), %57 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0), %60 ]
%65 = call i32 @puts(i8* dereferenceable(1) %64)
%66 = call i32 @fclose(i32* %44)
%67 = call i32 @fclose(i32* %48)
br label %68
68: ; preds = %54, %41, %36, %63
%69 = phi i32 [ 1, %63 ], [ 0, %36 ], [ 0, %41 ], [ 0, %54 ]
ret i32 %69
}