#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 ssize_t;
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 /*<<< orphan*/ *stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcmp(char *, char *);
char *trim(char *);
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "usage: %s <file_name>.txt\n", argv[0]);
    return -1;
  }
  FILE *out_fp = fopen(argv[1], "w");
  char *line = NULL;
  size_t len = 0;
  ssize_t nread;
  while ((nread = getline(&line, &len, stdin)) != -1) {
    if (!strcmp(line, "quit\n") || !strcmp(line, "q\n")) {
      fclose(out_fp);
      return 0;
    }
    char *trimmed = trim(line);
    fprintf(stdout, "%s", trimmed);
    fprintf(out_fp, "%s", trimmed);
  }
  if (line != NULL) {
    free(line);
  }
  fclose(out_fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b9 <main+0x49>
mov    0x2e41(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe58(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <main+0x13e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe53(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     128a <main+0x11a>
mov    -0x20(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1235 <main+0xc5>
mov    -0x20(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    124a <main+0xda>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ae <main+0x13e>
mov    -0x20(%rbp),%rdi
call   1050 <trim@plt>
mov    %rax,-0x38(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rdx
lea    0xdba(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xda6(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11e1 <main+0x71>
cmpq   $0x0,-0x20(%rbp)
je     129e <main+0x12e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    127f <main+0xff>
mov    0x8(%rsi),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     1264 <main+0xe4>
lea    0xe39(%rip),%r14
lea    0xe38(%rip),%r15
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1273 <main+0xf3>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1273 <main+0xf3>
mov    %rbp,%rdi
call   1050 <trim@plt>
mov    %rax,%rbp
mov    0x2d9b(%rip),%rax
mov    (%rax),%rsi
mov    %rbp,%rdi
call   1060 <fputs@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r13,%rsi
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
jne    1200 <main+0x80>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1273 <main+0xf3>
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    129d <main+0x11d>
mov    0x2d52(%rip),%rax
mov    (%rax),%rdi
mov    (%rsi),%rdx
lea    0xd6d(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32* null
@.str = [27 x i8] c"usage: %s <file_name>.txt\0A\00"
@.str.1 = [2 x i8] c"w\00"
@stdin = global i32 0
@.str.2 = [6 x i8] c"quit\0A\00"
@.str.3 = [3 x i8] c"q\0A\00"
@stdout = global i32* null
@.str.4 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32*, i32** @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %57
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
store i8* null, i8** %7
store i64 0, i64* %8
br label %24
24: ; preds = %39, %19
%25 = load i32, i32* @stdin
%26 = call i32 @getline(i8** %7, i64* %8, i32 %25)
store i32 %26, i32* %9
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %48
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = call i32 @strcmp(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i8*, i8** %7
%34 = call i32 @strcmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %32, %28
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
store i32 0, i32* %3
br label %57
39: ; preds = %32
%40 = load i8*, i8** %7
%41 = call i8* @trim(i8* %40)
store i8* %41, i8** %10
%42 = load i32*, i32** @stdout
%43 = load i8*, i8** %10
%44 = call i32 @fprintf(i32* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = load i32*, i32** %6
%46 = load i8*, i8** %10
%47 = call i32 @fprintf(i32* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %46)
br label %24
48: ; preds = %24
%49 = load i8*, i8** %7
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %7
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %51, %48
%55 = load i32*, i32** %6
%56 = call i32 @fclose(i32* %55)
store i32 0, i32* %3
br label %57
57: ; preds = %54, %36, %13
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [27 x i8] c"usage: %s <file_name>.txt\0A\00"
@.str.1 = [2 x i8] c"w\00"
@stdin = global i32 0
@.str.2 = [6 x i8] c"quit\0A\00"
@.str.3 = [3 x i8] c"q\0A\00"
@stdout = global i32* null
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32*, i32** @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %41
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%15 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%16 = load i32, i32* @stdin
%17 = call i32 @getline(i8** %3, i64* %4, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %34, label %19
19: ; preds = %10, %26
%20 = load i8*, i8** %3
%21 = call i32 @strcmp(i8* dereferenceable(1) %20, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32 %21, 0
br i1 %22, label %39, label %23
23: ; preds = %19
%24 = call i32 @strcmp(i8* dereferenceable(1) %20, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i32 %24, 0
br i1 %25, label %39, label %26
26: ; preds = %23
%27 = call i8* @trim(i8* %20)
%28 = load i32*, i32** @stdout
%29 = call i32 @fputs(i8* %27, i32* %28)
%30 = call i32 @fputs(i8* %27, i32* %13)
%31 = load i32, i32* @stdin
%32 = call i32 @getline(i8** %3, i64* %4, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %19
34: ; preds = %26, %10
%35 = load i8*, i8** %3
%36 = icmp eq i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 @free(i8* %35)
br label %39
39: ; preds = %19, %23, %34, %37
%40 = call i32 @fclose(i32* %13)
br label %41
41: ; preds = %39, %6
%42 = phi i32 [ -1, %6 ], [ 0, %39 ]
ret i32 %42
}