#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*/ buffer;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, n, length;
  char buffer[100];
  if (argc != 2) {
    printf("Number of Arguements provided != 2 \n");
    printf("Arguements Required: \n");
    printf("Filename \n");
    exit(-1);
  }
  fd = open(argv[1], O_APPEND | O_WRONLY | O_CREAT, 0644);
  if (fd < 0) {
    perror(argv[1]);
    exit(-1);
  }
  printf("Use Ctrl-D in a New Line to Exit! \n");
  printf("\n");
  while ((n = read(0, buffer, sizeof(buffer))) > 0) {
    length = strlen(buffer);
    write(fd, buffer, length);
  }
  close(fd);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d1 <main+0x51>
lea    0xe56(%rip),%rdi
call   1050 <printf@plt>
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
lea    0xe7a(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1222 <main+0xa2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
lea    0xe34(%rip),%rdi
call   1050 <printf@plt>
lea    -0x80(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1276 <main+0xf6>
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    123a <main+0xba>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
cmp    $0x2,%edi
jne    1253 <main+0xc3>
mov    %rsi,%r14
mov    0x2e1d(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rdi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1281 <main+0xf1>
mov    %eax,%ebp
lea    0xe22(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1237 <main+0xa7>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1210 <main+0x80>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc9(%rip),%rdi
call   1040 <puts@plt>
lea    0xde1(%rip),%rdi
call   1040 <puts@plt>
lea    0xdeb(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x8(%r14),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Number of Arguements provided != 2 \0A\00"
@.str.1 = [23 x i8] c"Arguements Required: \0A\00"
@.str.2 = [11 x i8] c"Filename \0A\00"
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [36 x i8] c"Use Ctrl-D in a New Line to Exit! \0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_APPEND
%22 = load i32, i32* @O_WRONLY
%23 = or i32 %21, %22
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %20, i32 %25, i32 420)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %17
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @perror(i8* %32)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %17
%36 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %42, %35
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%40 = call i32 @read(i32 0, i8* %39, i32 100)
store i32 %40, i32* %7
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %38
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
store i32 %44, i32* %8
%45 = load i32, i32* %6
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%47 = load i32, i32* %8
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
br label %38
49: ; preds = %38
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@str = [35 x i8] c"Use Ctrl-D in a New Line to Exit! \00"
@str.5 = [36 x i8] c"Number of Arguements provided != 2 \00"
@str.6 = [22 x i8] c"Arguements Required: \00"
@str.7 = [10 x i8] c"Filename \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.5, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, 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 = load i32, i32* @O_APPEND
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %15, %14
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = tail call i32 @open(i8* %13, i32 %18, i32 420)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = load i8*, i8** %12
%23 = tail call i32 @perror(i8* %22)
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %11
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 @putchar(i32 10)
%28 = call i32 @read(i32 0, i8* %4, i32 100)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %25, %30
%31 = call i32 @strlen(i8* %4)
%32 = call i32 @write(i32 %19, i8* %4, i32 %31)
%33 = call i32 @read(i32 0, i8* %4, i32 100)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %30, label %35
35: ; preds = %30, %25
%36 = call i32 @close(i32 %19)
%37 = call i32 @putchar(i32 10)
ret i32 0
}