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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  FILE *fpout;
  char *mode;
  char buf[64 * 1024];
  if (argc != 3 && argc != 2) {
    fprintf(stderr, "Usage: c_sample filename [fopen-mode]\n");
    exit(1);
  }
  if (argc != 3)
    mode = "a+";
  fpout = fopen(argv[1], mode);
  while (1) {
    if (fgets(buf, sizeof(buf), stdin) == NULL)
      break;
    fputs(buf, fpout);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     119c <main+0x4c>
cmpl   $0x2,-0x8(%rbp)
je     119c <main+0x4c>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
je     11b1 <main+0x61>
lea    0xe7a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x10020(%rbp),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    $0x10000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11ef <main+0x9f>
jmp    1204 <main+0xb4>
lea    -0x10020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <fputs@plt>
jmp    11c6 <main+0x76>
mov    -0x4(%rbp),%eax
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10000,%rsp
and    $0xfffffffe,%edi
cmp    $0x2,%edi
jne    11cf <main+0x7f>
mov    0x8(%rsi),%rdi
lea    0xeb8(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    0x2e6a(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rdi
mov    $0x10000,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     11c0 <main+0x70>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <fputs@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x10000,%esi
call   1040 <fgets@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
xor    %eax,%eax
add    $0x10000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"Usage: c_sample filename [fopen-mode]\0A\00"
@.str.1 = [3 x i8] c"a+\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [65536 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11, %2
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 3
br i1 %20, label %21, label %22
21: ; preds = %18
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %22
22: ; preds = %21, %18
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i8*, i8** %7
%27 = call i32* @fopen(i8* %25, i8* %26)
store i32* %27, i32** %6
br label %28
28: ; preds = %22, %34
%29 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* @stdin
%31 = call i32* @fgets(i8* %29, i32 65536, i32 %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
br label %38
34: ; preds = %28
%35 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%36 = load i32*, i32** %6
%37 = call i32 @fputs(i8* %35, i32* %36)
br label %28
38: ; preds = %33
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"Usage: c_sample filename [fopen-mode]\0A\00"
@.str.1 = [3 x i8] c"a+\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 2
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([39 x i8], [39 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* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @stdin
%16 = call i32* @fgets(i8* %4, i32 65536, i32 %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %23, label %18
18: ; preds = %11, %18
%19 = call i32 @fputs(i8* %4, i32* %14)
%20 = load i32, i32* @stdin
%21 = call i32* @fgets(i8* %4, i32 65536, i32 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %18
23: ; preds = %18, %11
ret i32 0
}