#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
scalar_t__ getchar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *stdout;
int main(int argc, char **argv) {
  char c;
  if (argc < 2) {
    printf("Error: argument required\n");
    exit(1);
  }
  FILE *file = fopen(argv[1], "a");
  if (file == NULL) {
    printf("Error: cannot open file\n");
    exit(1);
  }
  while ((c = (char)getchar()) && (!feof(stdin))) {
    fputc(c, stdout);
    fputc(c, file);
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a6 <main+0x36>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <main+0x6f>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11e4 <main+0x74>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     1218 <main+0xa8>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1228 <main+0xb8>
jmp    1251 <main+0xe1>
mov    -0x11(%rbp),%al
mov    0x2da6(%rip),%rcx
mov    (%rcx),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
mov    -0x11(%rbp),%al
mov    -0x20(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
jmp    11e4 <main+0x74>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    1208 <main+0x98>
mov    0x8(%rsi),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1211 <main+0xa1>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %rax,%rbx
test   %bl,%bl
je     11f2 <main+0x82>
mov    0x2e3a(%rip),%r15
mov    0x2e23(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%edi
call   1070 <feof@plt>
test   %eax,%eax
jne    11f2 <main+0x82>
mov    (%r12),%rsi
movsbl %bl,%ebx
mov    %ebx,%edi
call   1050 <fputc@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %rax,%rbx
test   %bl,%bl
jne    11c0 <main+0x50>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0b(%rip),%rdi
jmp    1218 <main+0xa8>
lea    0xdea(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Error: argument required\0A\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [25 x i8] c"Error: cannot open file\0A\00"
@stdin = global i32 0
@stdout = global i32* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %7
%18 = load i32*, i32** %7
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %13
br label %24
24: ; preds = %36, %23
%25 = call i64 (...) @getchar()
%26 = trunc i64 %25 to i8
store i8 %26, i8* %6
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i32, i32* @stdin
%31 = call i32 @feof(i32 %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br label %34
34: ; preds = %29, %24
%35 = phi i1 [ false, %24 ], [ %33, %29 ]
br i1 %35, label %36, label %43
36: ; preds = %34
%37 = load i8, i8* %6
%38 = load i32*, i32** @stdout
%39 = call i32 @fputc(i8 signext %37, i32* %38)
%40 = load i8, i8* %6
%41 = load i32*, i32** %7
%42 = call i32 @fputc(i8 signext %40, i32* %41)
br label %24
43: ; preds = %34
%44 = load i32*, i32** %7
%45 = call i32 @fclose(i32* %44)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"a\00"
@stdin = global i32 0
@stdout = global i32* null
@str = [24 x i8] c"Error: cannot open file\00"
@str.3 = [25 x i8] c"Error: argument required\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.3, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = tail call i64 (...) @getchar()
%14 = trunc i64 %13 to i8
%15 = icmp eq i8 %14, 0
br i1 %15, label %31, label %19
16: ; preds = %7
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %12, %24
%20 = phi i8 [ %29, %24 ], [ %14, %12 ]
%21 = load i32, i32* @stdin
%22 = tail call i32 @feof(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = load i32*, i32** @stdout
%26 = tail call i32 @fputc(i8 signext %20, i32* %25)
%27 = tail call i32 @fputc(i8 signext %20, i32* %10)
%28 = tail call i64 (...) @getchar()
%29 = trunc i64 %28 to i8
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %19
31: ; preds = %19, %24, %12
%32 = tail call i32 @fclose(i32* %10)
ret i32 0
}