#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 EIO;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char const *);
size_t fwrite(void const *, int, int, int /*<<< orphan*/ *);
int getopt(int, char **, char *);
int /*<<< orphan*/ print_usage();
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcmp(char const *, char *);
int strlen(char const *);
int main(int argc, char **argv) {
  int opt;
  FILE *fd;
  const char *mode;
  while (-1 != (opt = getopt(argc - 1, argv, "h"))) {
    switch (opt) {
    case 'h':
      print_usage();
      return 0;
    default:
      break;
    }
  }
  if (argc > 3) {
    size_t write_items_count;
    if (0 == strcmp((const char *)argv[argc - 2], ">>")) {
      mode = "a";
    } else if (0 == strcmp((const char *)argv[argc - 2], ">")) {
      mode = "w";
    } else {
      print_usage();
      return 0;
    }
    if (NULL == (fd = fopen((const char *)argv[argc - 1], mode))) {
      return -errno;
    }
    write_items_count =
        fwrite((const void *)argv[1], strlen((const char *)argv[1]), 1, fd);
    write_items_count += fwrite((const void *)"\n", 1, 1, fd);
    fclose(fd);
    return write_items_count > 0 ? 0 : -EIO;
  } else if (argc == 2) {
    printf("%s \n", argv[argc - 1]);
  } else {
    print_usage();
  }
  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)
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%rsi
lea    0xe59(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     11ec <main+0x6c>
mov    -0x14(%rbp),%eax
sub    $0x68,%eax
jne    11e2 <main+0x62>
jmp    11cf <main+0x4f>
mov    $0x0,%al
call   10a0 <print_usage@plt>
movl   $0x0,-0x4(%rbp)
jmp    1389 <main+0x209>
jmp    11e7 <main+0x67>
jmp    1196 <main+0x16>
cmpl   $0x3,-0x8(%rbp)
jle    134a <main+0x1ca>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdf4(%rip),%rsi
call   1060 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1231 <main+0xb1>
lea    0xddd(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1284 <main+0x104>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdba(%rip),%rsi
call   1060 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    126c <main+0xec>
lea    0xda4(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    127f <main+0xff>
mov    $0x0,%al
call   10a0 <print_usage@plt>
movl   $0x0,-0x4(%rbp)
jmp    1389 <main+0x209>
jmp    1284 <main+0x104>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    12c3 <main+0x143>
xor    %eax,%eax
mov    0x2d1f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x209>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rcx
lea    0xd0d(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
jbe    1331 <main+0x1b1>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    133f <main+0x1bf>
xor    %eax,%eax
mov    0x2ca6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x209>
cmpl   $0x2,-0x8(%rbp)
jne    1376 <main+0x1f6>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xc9d(%rip),%rdi
call   1050 <printf@plt>
jmp    137d <main+0x1fd>
mov    $0x0,%al
call   10a0 <print_usage@plt>
jmp    1382 <main+0x202>
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   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
lea    -0x1(%r14),%ebp
lea    0xe68(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <getopt@plt>
cmp    $0xffffffff,%eax
je     11cd <main+0x4d>
cmp    $0x68,%eax
jne    11a0 <main+0x20>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <print_usage@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x4,%r14d
jl     120a <main+0x8a>
add    $0xfffffffe,%r14d
mov    (%r15,%r14,8),%rdi
lea    0xe20(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1226 <main+0xa6>
mov    (%r15,%r14,8),%rdi
lea    0xe0c(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11b7 <main+0x37>
lea    0xdff(%rip),%rsi
jmp    122d <main+0xad>
cmp    $0x2,%r14d
jne    11b7 <main+0x37>
mov    %ebp,%eax
mov    (%r15,%rax,8),%rsi
lea    0xdec(%rip),%rdi
call   1050 <printf@plt>
xor    %ebx,%ebx
jmp    11c0 <main+0x40>
lea    0xdd8(%rip),%rsi
mov    %ebp,%eax
mov    (%r15,%rax,8),%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     129d <main+0x11d>
mov    %rax,%rbp
mov    0x8(%r15),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1090 <fwrite@plt>
mov    %rax,%r14
lea    0xda4(%rip),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1090 <fwrite@plt>
mov    %rax,%r15
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    0x2d54(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
xor    %eax,%eax
add    %r14,%r15
cmovne %eax,%ebx
jmp    11c0 <main+0x40>
mov    0x2d34(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11c0 <main+0x40>

LLVM IR (-O0):
@.str = [2 x i8] c"h\00"
@.str.1 = [3 x i8] c">>\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [2 x i8] c">\00"
@.str.4 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
@EIO = global i32 0
@.str.6 = [5 x i8] c"%s \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 i8*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %10
10: ; preds = %21, %2
%11 = load i32, i32* %4
%12 = sub nsw i32 %11, 1
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %6
%15 = icmp ne i32 -1, %14
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = load i32, i32* %6
switch i32 %17, label %20 [
i32 104, label %18
]
18: ; preds = %16
%19 = call i32 (...) @print_usage()
store i32 0, i32* %3
br label %100
20: ; preds = %16
br label %21
21: ; preds = %20
br label %10
22: ; preds = %10
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 3
br i1 %24, label %25, label %85
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %26, i64 %29
%31 = load i8*, i8** %30
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp eq i64 0, %32
br i1 %33, label %34, label %35
34: ; preds = %25
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %48
35: ; preds = %25
%36 = load i8**, i8*** %5
%37 = load i32, i32* %4
%38 = sub nsw i32 %37, 2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %36, i64 %39
%41 = load i8*, i8** %40
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp eq i64 0, %42
br i1 %43, label %44, label %45
44: ; preds = %35
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %8
br label %47
45: ; preds = %35
%46 = call i32 (...) @print_usage()
store i32 0, i32* %3
br label %100
47: ; preds = %44
br label %48
48: ; preds = %47, %34
%49 = load i8**, i8*** %5
%50 = load i32, i32* %4
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %49, i64 %52
%54 = load i8*, i8** %53
%55 = load i8*, i8** %8
%56 = call i32* @fopen(i8* %54, i8* %55)
store i32* %56, i32** %7
%57 = icmp eq i32* null, %56
br i1 %57, label %58, label %61
58: ; preds = %48
%59 = load i32, i32* @errno
%60 = sub nsw i32 0, %59
store i32 %60, i32* %3
br label %100
61: ; preds = %48
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = call i32 @strlen(i8* %67)
%69 = load i32*, i32** %7
%70 = call i64 @fwrite(i8* %64, i32 %68, i32 1, i32* %69)
store i64 %70, i64* %9
%71 = load i32*, i32** %7
%72 = call i64 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 1, i32* %71)
%73 = load i64, i64* %9
%74 = add i64 %73, %72
store i64 %74, i64* %9
%75 = load i32*, i32** %7
%76 = call i32 @fclose(i32* %75)
%77 = load i64, i64* %9
%78 = icmp ugt i64 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %61
br label %83
80: ; preds = %61
%81 = load i32, i32* @EIO
%82 = sub nsw i32 0, %81
br label %83
83: ; preds = %80, %79
%84 = phi i32 [ 0, %79 ], [ %82, %80 ]
store i32 %84, i32* %3
br label %100
85: ; preds = %22
%86 = load i32, i32* %4
%87 = icmp eq i32 %86, 2
br i1 %87, label %88, label %96
88: ; preds = %85
%89 = load i8**, i8*** %5
%90 = load i32, i32* %4
%91 = sub nsw i32 %90, 1
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8*, i8** %89, i64 %92
%94 = load i8*, i8** %93
%95 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %94)
br label %98
96: ; preds = %85
%97 = call i32 (...) @print_usage()
br label %98
98: ; preds = %96, %88
br label %99
99: ; preds = %98
store i32 0, i32* %3
br label %100
100: ; preds = %99, %83, %58, %45, %18
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
@.str = [2 x i8] c"h\00"
@.str.1 = [3 x i8] c">>\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [2 x i8] c">\00"
@.str.4 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
@EIO = global i32 0
@.str.6 = [5 x i8] c"%s \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = add nsw i32 %0, -1
br label %4
4: ; preds = %4, %2
%5 = tail call i32 @getopt(i32 %3, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %5, label %4 [
i32 -1, label %8
i32 104, label %6
]
6: ; preds = %4
%7 = tail call i32 (...) @print_usage()
br label %54
8: ; preds = %4
%9 = icmp sgt i32 %0, 3
br i1 %9, label %10, label %45
10: ; preds = %8
%11 = add nsw i32 %0, -2
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8*, i8** %1, i64 %12
%14 = load i8*, i8** %13
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %10
%18 = load i8*, i8** %13
%19 = tail call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = tail call i32 (...) @print_usage()
br label %54
23: ; preds = %17, %10
%24 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %10 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %17 ]
%25 = zext i32 %3 to i64
%26 = getelementptr inbounds i8*, i8** %1, i64 %25
%27 = load i8*, i8** %26
%28 = tail call i32* @fopen(i8* %27, i8* %24)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
br label %54
33: ; preds = %23
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = tail call i32 @strlen(i8* %35)
%37 = tail call i64 @fwrite(i8* %35, i32 %36, i32 1, i32* %28)
%38 = tail call i64 @fwrite(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 1, i32* %28)
%39 = tail call i32 @fclose(i32* %28)
%40 = sub i64 0, %38
%41 = icmp eq i64 %37, %40
%42 = load i32, i32* @EIO
%43 = sub nsw i32 0, %42
%44 = select i1 %41, i32 %43, i32 0
br label %54
45: ; preds = %8
%46 = icmp eq i32 %0, 2
br i1 %46, label %47, label %52
47: ; preds = %45
%48 = zext i32 %3 to i64
%49 = getelementptr inbounds i8*, i8** %1, i64 %48
%50 = load i8*, i8** %49
%51 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %50)
br label %54
52: ; preds = %45
%53 = tail call i32 (...) @print_usage()
br label %54
54: ; preds = %52, %47, %21, %30, %33, %6
%55 = phi i32 [ 0, %6 ], [ %32, %30 ], [ %44, %33 ], [ 0, %21 ], [ 0, %47 ], [ 0, %52 ]
ret i32 %55
}