#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*/ buff;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
long syscall(int, int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Please include the filename to which to write.\n");
    return 1;
  }
  int fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("Error opening file");
    return 2;
  }
  char buff[] = "Hello, how are you?";
  long success = syscall(291, fd, buff, sizeof(buff) - 1);
  if (success < 0) {
    perror("Failure in forcewrite");
    return 3;
  }
  printf("Success!\n");
  close(fd);
  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)
cmpl   $0x2,-0x8(%rbp)
jge    1188 <main+0x38>
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123a <main+0xea>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c3 <main+0x73>
lea    0xe7e(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    123a <main+0xea>
mov    0xea6(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xea3(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xea1(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    -0x30(%rbp),%rdx
mov    $0x123,%edi
mov    $0x13,%ecx
call   1050 <syscall@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    121f <main+0xcf>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    123a <main+0xea>
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
cmp    $0x1,%edi
jg     116d <main+0x1d>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11c6 <main+0x76>
mov    0x8(%rsi),%rdi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11cc <main+0x7c>
mov    %eax,%ebx
movaps 0xee4(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x3f756f,0x10(%rsp)
mov    %rsp,%rdx
mov    $0x123,%edi
mov    %eax,%esi
mov    $0x13,%ecx
call   1050 <syscall@plt>
test   %rax,%rax
js     11df <main+0x8f>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
jmp    11c6 <main+0x76>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%eax
jmp    11c6 <main+0x76>

LLVM IR (-O0):
@.str = [48 x i8] c"Please include the filename to which to write.\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [19 x i8] c"Error opening file\00"
@__const.main.buff = [20 x i8] c"Hello, how are you?\00"
@.str.2 = [22 x i8] c"Failure in forcewrite\00"
@.str.3 = [10 x i8] c"Success!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [20 x i8]
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %36
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %36
23: ; preds = %13
%24 = bitcast [20 x i8]* %7 to i8*
%25 = load i32, i32* %6
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%27 = call i64 @syscall(i32 291, i32 %25, i8* %26, i32 19)
store i64 %27, i64* %8
%28 = load i64, i64* %8
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %36
32: ; preds = %23
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 0, i32* %3
br label %36
36: ; preds = %32, %30, %21, %11
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [19 x i8] c"Error opening file\00"
@__const.main.buff = [20 x i8] c"Hello, how are you?\00"
@.str.2 = [22 x i8] c"Failure in forcewrite\00"
@str = [9 x i8] c"Success!\00"
@str.4 = [47 x i8] c"Please include the filename to which to write.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [20 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.4, i64 0, i64 0))
br label %26
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %26
15: ; preds = %7
%16 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%17 = call i64 @syscall(i32 291, i32 %11, i8* %16, i32 19)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %24
21: ; preds = %15
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%23 = call i32 @close(i32 %11)
br label %24
24: ; preds = %21, %19
%25 = phi i32 [ 3, %19 ], [ 0, %21 ]
br label %26
26: ; preds = %13, %24, %5
%27 = phi i32 [ 1, %5 ], [ 2, %13 ], [ %25, %24 ]
ret i32 %27
}