#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_NONBLOCK;
int O_WRONLY;
char *basename(char *);
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ usage(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  char *progname;
  char *filename;
  char *cmd;
  int cmdLen;
  int skip;
  int fd;
  int res;
  progname = basename(argv[0]);
  skip = strlen(progname) + 3;
  if (argc != 3 || strlen(argv[1]) <= skip) {
    usage(progname);
    return 1;
  }
  filename = argv[1] + skip;
  if (argv[2][0] != '\0') {
    cmd = argv[2];
  } else {
    cmd = progname;
  }
  cmdLen = strlen(cmd);
  cmd[cmdLen++] = '\n';
  fd = open(filename, O_WRONLY | O_NONBLOCK);
  if (fd < 0) {
    fprintf(stderr, "Can't open file '%s' (%s)\n", filename, strerror(errno));
    return 1;
  }
  res = write(fd, cmd, cmdLen);
  if (res < 0) {
    fprintf(stderr, "Can't write to file '%s' (%s)\n", filename,
            strerror(errno));
    return 1;
  } else if (res != cmdLen) {
    fprintf(stderr, "Couldn't write command to file '%s'\n", filename);
    return 1;
  }
  res = close(fd);
  if (res != 0) {
    fprintf(stderr, "Can't close file '%s' (%s)\n", filename, strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <basename@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x3,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11d5 <main+0x55>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
cmp    -0x30(%rbp),%eax
jg     11ea <main+0x6a>
mov    -0x18(%rbp),%rdi
call   1080 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x236>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1222 <main+0xa2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    122a <main+0xaa>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12ba <main+0x13a>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x236>
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    131c <main+0x19c>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd12(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x236>
mov    -0x38(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     134f <main+0x1cf>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xcfe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x236>
jmp    1354 <main+0x1d4>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     13af <main+0x22f>
mov    0x2c68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2c4c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xcc3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x236>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%ebp
mov    (%rsi),%rdi
call   1070 <basename@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1040 <strlen@plt>
cmp    $0x3,%ebp
jne    124e <main+0xce>
mov    %eax,%ebx
add    $0x3,%ebx
mov    0x8(%r15),%rdi
call   1040 <strlen@plt>
cmp    %ebx,%eax
jle    124e <main+0xce>
movslq %ebx,%r14
add    0x8(%r15),%r14
mov    0x10(%r15),%rax
cmpb   $0x0,(%rax)
cmovne %rax,%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
cltq
movb   $0xa,(%r12,%rax,1)
mov    0x2dd7(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1264 <main+0xe4>
mov    %eax,%r15d
add    $0x1,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1284 <main+0x104>
cmp    %ebp,%eax
jne    12b3 <main+0x133>
mov    %r15d,%edi
call   1050 <close@plt>
test   %eax,%eax
je     12cf <main+0x14f>
mov    0x2da3(%rip),%rax
mov    (%rax),%ebx
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe13(%rip),%rsi
jmp    12a2 <main+0x122>
mov    %r12,%rdi
call   1080 <usage@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd7e(%rip),%rsi
jmp    12a2 <main+0x122>
mov    0x2d4d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd79(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1256 <main+0xd6>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1256 <main+0xd6>
xor    %eax,%eax
jmp    125b <main+0xdb>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Can't open file '%s' (%s)\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"Can't write to file '%s' (%s)\0A\00"
@.str.2 = [37 x i8] c"Couldn't write command to file '%s'\0A\00"
@.str.3 = [28 x i8] c"Can't close file '%s' (%s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i8* @basename(i8* %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 3
store i32 %19, i32* %10
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 3
br i1 %21, label %29, label %22
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @strlen(i8* %25)
%27 = load i32, i32* %10
%28 = icmp sle i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %22, %2
%30 = load i8*, i8** %6
%31 = call i32 @usage(i8* %30)
store i32 1, i32* %3
br label %107
32: ; preds = %22
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8* %38, i8** %7
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %32
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
store i8* %49, i8** %8
br label %52
50: ; preds = %32
%51 = load i8*, i8** %6
store i8* %51, i8** %8
br label %52
52: ; preds = %50, %46
%53 = load i8*, i8** %8
%54 = call i32 @strlen(i8* %53)
store i32 %54, i32* %9
%55 = load i8*, i8** %8
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 10, i8* %59
%60 = load i8*, i8** %7
%61 = load i32, i32* @O_WRONLY
%62 = load i32, i32* @O_NONBLOCK
%63 = or i32 %61, %62
%64 = call i32 @open(i8* %60, i32 %63)
store i32 %64, i32* %11
%65 = load i32, i32* %11
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %52
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %7
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %69, i8* %71)
store i32 1, i32* %3
br label %107
73: ; preds = %52
%74 = load i32, i32* %11
%75 = load i8*, i8** %8
%76 = load i32, i32* %9
%77 = call i32 @write(i32 %74, i8* %75, i32 %76)
store i32 %77, i32* %12
%78 = load i32, i32* %12
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %86
80: ; preds = %73
%81 = load i32, i32* @stderr
%82 = load i8*, i8** %7
%83 = load i32, i32* @errno
%84 = call i8* @strerror(i32 %83)
%85 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %82, i8* %84)
store i32 1, i32* %3
br label %107
86: ; preds = %73
%87 = load i32, i32* %12
%88 = load i32, i32* %9
%89 = icmp ne i32 %87, %88
br i1 %89, label %90, label %94
90: ; preds = %86
%91 = load i32, i32* @stderr
%92 = load i8*, i8** %7
%93 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %91, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %92)
store i32 1, i32* %3
br label %107
94: ; preds = %86
br label %95
95: ; preds = %94
%96 = load i32, i32* %11
%97 = call i32 @close(i32 %96)
store i32 %97, i32* %12
%98 = load i32, i32* %12
%99 = icmp ne i32 %98, 0
br i1 %99, label %100, label %106
100: ; preds = %95
%101 = load i32, i32* @stderr
%102 = load i8*, i8** %7
%103 = load i32, i32* @errno
%104 = call i8* @strerror(i32 %103)
%105 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %101, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %102, i8* %104)
store i32 1, i32* %3
br label %107
106: ; preds = %95
store i32 0, i32* %3
br label %107
107: ; preds = %106, %100, %90, %80, %67, %29
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Can't open file '%s' (%s)\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"Can't write to file '%s' (%s)\0A\00"
@.str.2 = [37 x i8] c"Couldn't write command to file '%s'\0A\00"
@.str.3 = [28 x i8] c"Can't close file '%s' (%s)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i8* @basename(i8* %3)
%5 = tail call i32 @strlen(i8* %4)
%6 = add nsw i32 %5, 3
%7 = icmp eq i32 %0, 3
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @strlen(i8* %10)
%12 = icmp sgt i32 %11, %6
br i1 %12, label %15, label %13
13: ; preds = %8, %2
%14 = tail call i32 @usage(i8* %4)
br label %59
15: ; preds = %8
%16 = load i8*, i8** %9
%17 = sext i32 %6 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
%23 = select i1 %22, i8* %4, i8* %20
%24 = tail call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 1
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 10, i8* %27
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %29, %28
%31 = tail call i32 @open(i8* %18, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %15
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %36)
br label %59
38: ; preds = %15
%39 = tail call i32 @write(i32 %31, i8* %23, i32 %25)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %44)
br label %59
46: ; preds = %38
%47 = icmp eq i32 %39, %25
br i1 %47, label %51, label %48
48: ; preds = %46
%49 = load i32, i32* @stderr
%50 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %59
51: ; preds = %46
%52 = tail call i32 @close(i32 %31)
%53 = icmp eq i32 %52, 0
br i1 %53, label %59, label %54
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = tail call i8* @strerror(i32 %56)
%58 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %18, i8* %57)
br label %59
59: ; preds = %51, %54, %48, %41, %33, %13
%60 = phi i32 [ 1, %13 ], [ 1, %33 ], [ 1, %41 ], [ 1, %48 ], [ 1, %54 ], [ 0, %51 ]
ret i32 %60
}