#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 */
scalar_t__ EACCES;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int STDOUT_FILENO;
int close(int);
int dup(int);
scalar_t__ dup2(int, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int writeToFile(char *path, char *message, int redirtype) {
  int desc, flags = 0;
  int current_out = dup(1);
  switch (redirtype) {
  case 1:
    flags = O_WRONLY | O_CREAT;
    break;
  case 2:
    flags = O_APPEND | O_WRONLY | O_CREAT;
    break;
  default:
    flags = O_WRONLY | O_CREAT;
    break;
  }
  if ((desc = open(path, flags)) == -1) {
    if (errno == EACCES) {
      fprintf(stderr, "minish: %s : Permission denied\n", path);
    } else {
      perror("minish: error opening the file");
    }
    return -1;
  }
  dup2(desc, STDOUT_FILENO);
  write(desc, message, strlen(message));
  if (dup2(current_out, 1) < 0) {
    fprintf(stderr, "couldn't reset output\n");
    return 1;
  }
  if (close(desc) == -1) {
    perror("close error");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writeToFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x1,%eax
je     11cc <writeToFile+0x4c>
jmp    11bb <writeToFile+0x3b>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     11e6 <writeToFile+0x66>
jmp    1209 <writeToFile+0x89>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    0x2e1c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    121e <writeToFile+0x9e>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    0x2dba(%rip),%rcx
or     (%rcx),%eax
mov    0x2df9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    121e <writeToFile+0x9e>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1288 <writeToFile+0x108>
mov    0x2d83(%rip),%rax
mov    (%rax),%rax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <writeToFile+0xf0>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    127c <writeToFile+0xfc>
lea    0xda9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <writeToFile+0x1a8>
mov    -0x20(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    12f8 <writeToFile+0x178>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1328 <writeToFile+0x1a8>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    1321 <writeToFile+0x1a1>
lea    0xd46(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <writeToFile+0x1a8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeToFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,%r14d
cmp    $0x2,%ebp
je     11b6 <writeToFile+0x36>
cmp    $0x1,%ebp
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
jmp    11cf <writeToFile+0x4f>
mov    0x2deb(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1232 <writeToFile+0xb2>
mov    %eax,%ebp
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%ebx
mov    %r14d,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     1269 <writeToFile+0xe9>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
jne    1280 <writeToFile+0x100>
lea    0xe26(%rip),%rdi
jmp    1294 <writeToFile+0x114>
mov    0x2d87(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
jne    128d <writeToFile+0x10d>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1280 <writeToFile+0x100>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8c(%rip),%rdi
call   10a0 <perror@plt>
jmp    1262 <writeToFile+0xe2>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@stderr = global i32 0
@.str = [32 x i8] c"minish: %s : Permission denied\0A\00"
@.str.1 = [31 x i8] c"minish: error opening the file\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [23 x i8] c"couldn't reset output\0A\00"
@.str.3 = [12 x i8] c"close error\00"
define i32 @writeToFile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%11 = call i32 @dup(i32 1)
store i32 %11, i32* %10
%12 = load i32, i32* %7
switch i32 %12, label %23 [
i32 1, label %13
i32 2, label %17
]
13: ; preds = %3
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
store i32 %16, i32* %9
br label %27
17: ; preds = %3
%18 = load i32, i32* @O_APPEND
%19 = load i32, i32* @O_WRONLY
%20 = or i32 %18, %19
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
store i32 %22, i32* %9
br label %27
23: ; preds = %3
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
store i32 %26, i32* %9
br label %27
27: ; preds = %23, %17, %13
%28 = load i8*, i8** %5
%29 = load i32, i32* %9
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %43
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = load i64, i64* @EACCES
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %5
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %38)
br label %42
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
store i32 -1, i32* %4
br label %65
43: ; preds = %27
%44 = load i32, i32* %8
%45 = load i32, i32* @STDOUT_FILENO
%46 = call i64 @dup2(i32 %44, i32 %45)
%47 = load i32, i32* %8
%48 = load i8*, i8** %6
%49 = load i8*, i8** %6
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 %47, i8* %48, i32 %50)
%52 = load i32, i32* %10
%53 = call i64 @dup2(i32 %52, i32 1)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %43
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
br label %65
58: ; preds = %43
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %65
64: ; preds = %58
store i32 0, i32* %4
br label %65
65: ; preds = %64, %62, %55, %42
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@stderr = global i32 0
@.str = [32 x i8] c"minish: %s : Permission denied\0A\00"
@.str.1 = [31 x i8] c"minish: error opening the file\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [23 x i8] c"couldn't reset output\0A\00"
@.str.3 = [12 x i8] c"close error\00"
define i32 @writeToFile(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @dup(i32 1)
switch i32 %2, label %11 [
i32 1, label %5
i32 2, label %7
]
5: ; preds = %3
%6 = load i32, i32* @O_CREAT
br label %13
7: ; preds = %3
%8 = load i32, i32* @O_APPEND
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %9, %8
br label %13
11: ; preds = %3
%12 = load i32, i32* @O_CREAT
br label %13
13: ; preds = %11, %7, %5
%14 = phi i32* [ @O_WRONLY, %11 ], [ @O_CREAT, %7 ], [ @O_WRONLY, %5 ]
%15 = phi i32 [ %12, %11 ], [ %10, %7 ], [ %6, %5 ]
%16 = load i32, i32* %14
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %0, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %29
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EACCES
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %44
27: ; preds = %20
%28 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %44
29: ; preds = %13
%30 = load i32, i32* @STDOUT_FILENO
%31 = tail call i64 @dup2(i32 %18, i32 %30)
%32 = tail call i32 @strlen(i8* %1)
%33 = tail call i32 @write(i32 %18, i8* %1, i32 %32)
%34 = tail call i64 @dup2(i32 %4, i32 1)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %44
39: ; preds = %29
%40 = tail call i32 @close(i32 %18)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %39, %24, %27, %42, %36
%45 = phi i32 [ 1, %36 ], [ -1, %42 ], [ -1, %27 ], [ -1, %24 ], [ 0, %39 ]
ret i32 %45
}