#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*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/);
int getopt(int, char **, char *);
scalar_t__ optind;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  char *file_mode = "w";
  int ch;
  while ((ch = getopt(argc, argv, "a")) != -1) {
    switch (ch) {
    case 'a':
      file_mode = "a";
      break;
    case 'i':
      printf("-i flag is not handled yet\n");
      break;
    }
  }
  argc -= optind;
  argv += optind;
  char *buffer = NULL;
  int read;
  size_t len;
  read = getline(&buffer, &len, stdin);
  if (read != -1) {
    puts(buffer);
    int i;
    FILE *file;
    for (i = 0; i < argc; i++) {
      file = fopen(argv[i], file_mode);
      fprintf(file, "%s\n", buffer);
      fclose(file);
    }
  } else {
    printf("No line read\n");
    return 1;
  }
  printf("Size read: %d\nLen: %zu\n", read, len);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe53(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     1208 <main+0x88>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x61,%eax
je     11e5 <main+0x65>
jmp    11d4 <main+0x54>
mov    -0x4c(%rbp),%eax
sub    $0x69,%eax
je     11f5 <main+0x75>
jmp    1203 <main+0x83>
lea    0xe16(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1203 <main+0x83>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11a1 <main+0x21>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     12c2 <main+0x142>
mov    -0x28(%rbp),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12bd <main+0x13d>
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xd7f(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    126d <main+0xed>
jmp    12dc <main+0x15c>
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <main+0x181>
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rdx
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe66(%rip),%r14
lea    0xe61(%rip),%r15
lea    0xe85(%rip),%r12
jmp    11b8 <main+0x38>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <getopt@plt>
cmp    $0x69,%eax
je     11b0 <main+0x30>
cmp    $0xffffffff,%eax
je     11d9 <main+0x59>
cmp    $0x61,%eax
jne    11b8 <main+0x38>
mov    %r15,%r14
jmp    11b8 <main+0x38>
mov    0x2e00(%rip),%rax
mov    (%rax),%r12
movq   $0x0,(%rsp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     1284 <main+0x104>
mov    %eax,0xc(%rsp)
sub    %r12d,%ebp
mov    (%rsp),%rdi
call   1040 <puts@plt>
test   %ebp,%ebp
jle    1260 <main+0xe0>
lea    (%rbx,%r12,8),%r13
mov    %ebp,%ebx
xor    %r15d,%r15d
lea    0xdda(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13,%r15,8),%rdi
mov    %r14,%rsi
call   1080 <fopen@plt>
mov    %rax,%rbp
mov    (%rsp),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
add    $0x1,%r15
cmp    %r15,%rbx
jne    1230 <main+0xb0>
mov    0x10(%rsp),%rdx
lea    0xd9c(%rip),%rdi
xor    %ebx,%ebx
mov    0xc(%rsp),%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    1295 <main+0x115>
lea    0xd95(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [28 x i8] c"-i flag is not handled yet\0A\00"
@optind = global i64 0
@stdin = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [14 x i8] c"No line read\0A\00"
@.str.5 = [24 x i8] c"Size read: %d\0ALen: %zu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6
br label %13
13: ; preds = %23, %2
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @getopt(i32 %14, i8** %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %16, i32* %7
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i32, i32* %7
switch i32 %19, label %23 [
i32 97, label %20
i32 105, label %21
]
20: ; preds = %18
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %23
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %18, %21, %20
br label %13
24: ; preds = %13
%25 = load i64, i64* @optind
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = sub nsw i64 %27, %25
%29 = trunc i64 %28 to i32
store i32 %29, i32* %4
%30 = load i64, i64* @optind
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 %30
store i8** %32, i8*** %5
store i8* null, i8** %8
%33 = load i32, i32* @stdin
%34 = call i32 @getline(i8** %8, i64* %10, i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %61
37: ; preds = %24
%38 = load i8*, i8** %8
%39 = call i32 @puts(i8* %38)
store i32 0, i32* %11
br label %40
40: ; preds = %57, %37
%41 = load i32, i32* %11
%42 = load i32, i32* %4
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %60
44: ; preds = %40
%45 = load i8**, i8*** %5
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %45, i64 %47
%49 = load i8*, i8** %48
%50 = load i8*, i8** %6
%51 = call i32* @fopen(i8* %49, i8* %50)
store i32* %51, i32** %12
%52 = load i32*, i32** %12
%53 = load i8*, i8** %8
%54 = call i32 @fprintf(i32* %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i32*, i32** %12
%56 = call i32 @fclose(i32* %55)
br label %57
57: ; preds = %44
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
br label %40
60: ; preds = %40
br label %63
61: ; preds = %24
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %69
63: ; preds = %60
%64 = load i32, i32* %9
%65 = load i64, i64* %10
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %64, i64 %65)
%67 = load i8*, i8** %8
%68 = call i32 @free(i8* %67)
store i32 0, i32* %3
br label %69
69: ; preds = %63, %61
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@optind = global i64 0
@stdin = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.5 = [24 x i8] c"Size read: %d\0ALen: %zu\0A\00"
@str = [13 x i8] c"No line read\00"
@str.6 = [27 x i8] c"-i flag is not handled yet\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i64
br label %5
5: ; preds = %7, %2
%6 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %7 ]
br label %7
7: ; preds = %9, %5
%8 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
switch i32 %8, label %9 [
i32 -1, label %12
i32 97, label %5
i32 105, label %10
]
9: ; preds = %7, %10
br label %7
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
br label %9
12: ; preds = %7
%13 = load i64, i64* @optind
%14 = trunc i64 %13 to i32
%15 = sub i32 %0, %14
%16 = getelementptr inbounds i8*, i8** %1, i64 %13
%17 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%18 = bitcast i64* %4 to i8*
%19 = load i32, i32* @stdin
%20 = call i32 @getline(i8** %3, i64* %4, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %43, label %22
22: ; preds = %12
%23 = load i8*, i8** %3
%24 = call i32 @puts(i8* dereferenceable(1) %23)
%25 = icmp sgt i32 %15, 0
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = zext i32 %15 to i64
br label %28
28: ; preds = %26, %28
%29 = phi i64 [ 0, %26 ], [ %36, %28 ]
%30 = getelementptr inbounds i8*, i8** %16, i64 %29
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* %6)
%33 = load i8*, i8** %3
%34 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = call i32 @fclose(i32* %32)
%36 = add nuw nsw i64 %29, 1
%37 = icmp eq i64 %36, %27
br i1 %37, label %38, label %28
38: ; preds = %28, %22
%39 = load i64, i64* %4
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %20, i64 %39)
%41 = load i8*, i8** %3
%42 = call i32 @free(i8* %41)
br label %45
43: ; preds = %12
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %38, %43
%46 = phi i32 [ 0, %38 ], [ 1, %43 ]
ret i32 %46
}