#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 O_RDWR;
int O_SYNC;
char *basename(char *);
int close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd = 1;
  if (argc != 2 && argc != 3) {
    fprintf(stderr, "usage: %s STRING [ostream]\n", basename(argv[0]));
    exit(1);
  }
  if (argc == 3)
    if ((fd = open(argv[2], O_RDWR | O_SYNC)) == -1)
      err(errno, "%s", argv[2]);
  if (write(fd, argv[1], strlen(argv[1])) == -1)
    err(errno, "write error");
  if (fd != 1)
    if (close(fd) == -1)
      err(errno, "close error");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e5 <main+0x65>
cmpl   $0x3,-0x8(%rbp)
je     11e5 <main+0x65>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <basename@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jne    123e <main+0xbe>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1239 <main+0xb9>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
jmp    123e <main+0xbe>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    128b <main+0x10b>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
cmpl   $0x1,-0x14(%rbp)
je     12c2 <main+0x142>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12bd <main+0x13d>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
jmp    12c2 <main+0x142>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    124c <main+0xcc>
mov    $0x1,%ebp
cmp    $0x3,%edi
jne    11e0 <main+0x60>
mov    0x10(%rbx),%rdi
mov    0x2e3f(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11e0 <main+0x60>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xe48(%rip),%rsi
xor    %eax,%eax
call   1080 <err@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1204 <main+0x84>
cmp    $0x1,%ebp
jne    1220 <main+0xa0>
jmp    1243 <main+0xc3>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
xor    %eax,%eax
call   1080 <err@plt>
cmp    $0x1,%ebp
je     1243 <main+0xc3>
mov    %ebp,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1243 <main+0xc3>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
xor    %eax,%eax
call   1080 <err@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d7d(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rdi
call   1070 <basename@plt>
lea    0xd9c(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"usage: %s STRING [ostream]\0A\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@errno = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [12 x i8] c"write error\00"
@.str.3 = [12 x i8] c"close error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %20
9: ; preds = %2
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i8* @basename(i8* %16)
%18 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %9, %2
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 3
br i1 %22, label %23, label %39
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_SYNC
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %26, i32 %29)
store i32 %30, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %23
%33 = load i32, i32* @errno
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 (i32, i8*, ...) @err(i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %32, %23
br label %39
39: ; preds = %38, %20
%40 = load i32, i32* %6
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @write(i32 %40, i8* %43, i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %39
%51 = load i32, i32* @errno
%52 = call i32 (i32, i8*, ...) @err(i32 %51, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %50, %39
%54 = load i32, i32* %6
%55 = icmp ne i32 %54, 1
br i1 %55, label %56, label %64
56: ; preds = %53
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = load i32, i32* @errno
%62 = call i32 (i32, i8*, ...) @err(i32 %61, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %63
63: ; preds = %60, %56
br label %64
64: ; preds = %63, %53
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"usage: %s STRING [ostream]\0A\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@errno = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [12 x i8] c"write error\00"
@.str.3 = [12 x i8] c"close error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = and i32 %0, -2
%4 = icmp eq i32 %3, 2
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i8* @basename(i8* %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = icmp eq i32 %0, 3
br i1 %12, label %13, label %25
13: ; preds = %11
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_SYNC
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %15, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = load i32, i32* @errno
%23 = load i8*, i8** %14
%24 = tail call i32 (i32, i8*, ...) @err(i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %13, %21, %11
%26 = phi i32 [ -1, %21 ], [ %19, %13 ], [ 1, %11 ]
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = tail call i32 @strlen(i8* %28)
%30 = tail call i32 @write(i32 %26, i8* %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* @errno
%34 = tail call i32 (i32, i8*, ...) @err(i32 %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %25
%36 = icmp eq i32 %26, 1
br i1 %36, label %43, label %37
37: ; preds = %35
%38 = tail call i32 @close(i32 %26)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = tail call i32 (i32, i8*, ...) @err(i32 %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %37, %40, %35
ret i32 0
}