#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_CREAT;
int O_WRONLY;
int /*<<< orphan*/ POSHrunCommand(char *);
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int close(int);
int dup2(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int openedFD, destFD;
  if (argc != 4) {
    fprintf(stderr, "Usage: write file fd command\n");
    exit(1);
  }
  openedFD = open(argv[1], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (openedFD == -1) {
    perror("open");
    exit(1);
  }
  destFD = atoi(argv[2]);
  if (dup2(openedFD, destFD) == -1) {
    perror("dup2");
    exit(1);
  }
  POSHrunCommand(argv[3]);
  if (close(openedFD) == -1) {
    perror("close");
    exit(1);
  }
  return 0;
}


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)
cmpl   $0x4,-0x8(%rbp)
je     11bf <main+0x3f>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2df7(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1213 <main+0x93>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    124d <main+0xcd>
lea    0xde5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1060 <POSHrunCommand@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1281 <main+0x101>
lea    0xdb6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    11f4 <main+0x74>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e22(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e20(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1213 <main+0x93>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     121c <main+0x9c>
mov    0x18(%rbx),%rdi
call   1060 <POSHrunCommand@plt>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1225 <main+0xa5>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe04(%rip),%rdi
jmp    122c <main+0xac>
lea    0xe00(%rip),%rdi
jmp    122c <main+0xac>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: write file fd command\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"dup2\00"
@.str.3 = [6 x i8] c"close\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 4
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %17, i32 %20, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %14
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %7
%35 = load i32, i32* %6
%36 = load i32, i32* %7
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %30
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 3
%45 = load i8*, i8** %44
%46 = call i32 @POSHrunCommand(i8* %45)
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %42
ret i32 0
}

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