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

/* Variables and functions */
int EXIT_SUCCESS;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int, int);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char buf = 0;
  if (argc == 1) {
    while (read(0, &buf, 1)) {
      write(1, &buf, 1);
    }
  } else {
    if (argc > 2) {
      fprintf(stderr, "Usage : %s [filename]\n", argv[0]);
    }
    int fd =
        open(argv[1], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
    while (read(0, &buf, 1)) {
      write(fd, &buf, 1);
    }
    close(fd);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11ad <main+0x5d>
jmp    1179 <main+0x29>
xor    %edi,%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     11a8 <main+0x58>
mov    $0x1,%edx
lea    -0x11(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
jmp    1179 <main+0x29>
jmp    1251 <main+0x101>
cmpl   $0x2,-0x8(%rbp)
jle    11d3 <main+0x83>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    0x2dd3(%rip),%rax
or     (%rax),%edx
mov    0x2db2(%rip),%rax
or     (%rax),%edx
mov    0x2dc9(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     1249 <main+0xf9>
mov    -0x18(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1219 <main+0xc9>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
movb   $0x0,0x7(%rsp)
cmp    $0x1,%edi
jne    11ab <main+0x5b>
lea    0x7(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
je     125a <main+0x10a>
lea    0x7(%rsp),%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jne    1180 <main+0x30>
jmp    125a <main+0x10a>
mov    %rsi,%rbx
cmp    $0x3,%edi
jl     11cb <main+0x7b>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e26(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    0x2de5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    0x8(%rbx),%rdi
mov    0x2dd5(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,%ebp
lea    0x7(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1253 <main+0x103>
lea    0x7(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jne    1230 <main+0xe0>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Usage : %s [filename]\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@EXIT_SUCCESS = global i32 0
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
store i8 0, i8* %6
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %17
10: ; preds = %2
br label %11
11: ; preds = %14, %10
%12 = call i64 @read(i32 0, i8* %6, i32 1)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @write(i32 1, i8* %6, i32 1)
br label %11
16: ; preds = %11
br label %50
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 2
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %20, %17
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_CREAT
%31 = load i32, i32* @O_RDWR
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IRGRP
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IWGRP
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %29, i32 %32, i32 %39)
store i32 %40, i32* %7
br label %41
41: ; preds = %44, %26
%42 = call i64 @read(i32 0, i8* %6, i32 1)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* %7
%46 = call i32 @write(i32 %45, i8* %6, i32 1)
br label %41
47: ; preds = %41
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %16
%51 = load i32, i32* @EXIT_SUCCESS
ret i32 %51
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Usage : %s [filename]\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
store i8 0, i8* %3
%4 = icmp eq i32 %0, 1
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = call i64 @read(i32 0, i8* %3, i32 1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %40, label %8
8: ; preds = %5, %8
%9 = call i32 @write(i32 1, i8* %3, i32 1)
%10 = call i64 @read(i32 0, i8* %3, i32 1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %40, label %8
12: ; preds = %2
%13 = icmp sgt i32 %0, 2
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %1
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %14, %12
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = load i32, i32* @S_IRGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IWGRP
%30 = or i32 %28, %29
%31 = tail call i32 @open(i8* %20, i32 %23, i32 %30)
%32 = call i64 @read(i32 0, i8* %3, i32 1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %18, %34
%35 = call i32 @write(i32 %31, i8* %3, i32 1)
%36 = call i64 @read(i32 0, i8* %3, i32 1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %34
38: ; preds = %34, %18
%39 = tail call i32 @close(i32 %31)
br label %40
40: ; preds = %8, %5, %38
%41 = load i32, i32* @EXIT_SUCCESS
ret i32 %41
}