#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 /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
int syscall(int, int, char *, int);
int /*<<< orphan*/ usage();
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    usage();
  }
  char *filename = argv[1];
  char *normal = "Normal Write.\n";
  int normalSize = (strlen(normal) + 1) * sizeof(char);
  char *forced = "Forced Write.\n";
  int forcedSize = (strlen(forced) + 1) * sizeof(char);
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("Could not open the file.\n");
    return -1;
  }
  if (write(fd, normal, normalSize) != normalSize) {
    printf("Could not write to the file normally.\n");
  }
  if (syscall(291, fd, forced, forcedSize) != forcedSize) {
    printf("Could not force write to the file.\n");
    return -1;
  }
  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    1187 <main+0x27>
mov    $0x0,%al
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    0xe66(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,-0x24(%rbp)
lea    0xe55(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    120a <main+0xaa>
lea    0xe25(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <main+0x10a>
mov    -0x38(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     122e <main+0xce>
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x34(%rbp),%ecx
mov    $0x123,%edi
call   1060 <syscall@plt>
cmp    -0x34(%rbp),%eax
je     1263 <main+0x103>
lea    0xe0d(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <main+0x10a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     1173 <main+0x13>
xor    %eax,%eax
call   1070 <usage@plt>
mov    0x8(%rbx),%rbx
lea    0xe82(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
lea    0xe83(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11fa <main+0x9a>
mov    %eax,%ebx
add    $0x1,%ebp
add    $0x1,%r14d
lea    0xe47(%rip),%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11d2 <main+0x72>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
lea    0xe36(%rip),%rdx
mov    $0x123,%edi
mov    %ebx,%esi
mov    %r14d,%ecx
call   1060 <syscall@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %r14d,%ecx
je     120b <main+0xab>
lea    0xe26(%rip),%rdi
jmp    1201 <main+0xa1>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Normal Write.\0A\00"
@.str.1 = [15 x i8] c"Forced Write.\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [26 x i8] c"Could not open the file.\0A\00"
@.str.3 = [39 x i8] c"Could not write to the file normally.\0A\00"
@.str.4 = [36 x i8] c"Could not force write to the file.\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 i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (...) @usage()
br label %16
16: ; preds = %14, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %6
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %7
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 1
%25 = trunc i64 %24 to i32
store i32 %25, i32* %8
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %9
%26 = load i8*, i8** %9
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 1
%31 = trunc i64 %30 to i32
store i32 %31, i32* %10
%32 = load i8*, i8** %6
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %16
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
39: ; preds = %16
%40 = load i32, i32* %11
%41 = load i8*, i8** %7
%42 = load i32, i32* %8
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
%44 = load i32, i32* %8
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %39
%49 = load i32, i32* %11
%50 = load i8*, i8** %9
%51 = load i32, i32* %10
%52 = call i32 @syscall(i32 291, i32 %49, i8* %50, i32 %51)
%53 = load i32, i32* %10
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
57: ; preds = %48
store i32 0, i32* %3
br label %58
58: ; preds = %57, %55, %37
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [15 x i8] c"Normal Write.\0A\00"
@.str.1 = [15 x i8] c"Forced Write.\0A\00"
@O_RDONLY = global i32 0
@str = [35 x i8] c"Could not force write to the file.\00"
@str.5 = [38 x i8] c"Could not write to the file normally.\00"
@str.6 = [25 x i8] c"Could not open the file.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (...) @usage()
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = add nsw i32 %9, 1
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = add nsw i32 %11, 1
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %8, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %6
%17 = tail call i32 @write(i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10)
%18 = icmp eq i32 %17, %10
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = tail call i32 @syscall(i32 291, i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%23 = icmp eq i32 %22, %12
br i1 %23, label %27, label %24
24: ; preds = %21, %6
%25 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0), %6 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0), %21 ]
%26 = tail call i32 @puts(i8* dereferenceable(1) %25)
br label %27
27: ; preds = %24, %21
%28 = phi i32 [ 0, %21 ], [ -1, %24 ]
ret i32 %28
}