#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 close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int syscall(int, int, char *, int);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("test_sys_forcewrite"
           ": Not enough arguments, please supply someone elses file to "
           "destroy\n");
    return -1;
  }
  int fd;
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    perror("open");
    exit(1);
  }
  char *buf = "Mary had a little lamb, and two helpings of peas and carrots";
  printf("test_sys_forcewrite"
         ": string: %s\n",
         buf);
  int count = 64;
  printf("test_sys_forcewrite"
         ": Trying sys call %d on file: %s, bytes %d\n",
         291, argv[1], count);
  int result = syscall(291, fd, buf, count);
  if (result == -1) {
    printf("test_sys_forcewrite"
           ": failed for file: %s, result %d\n",
           argv[1], result);
  } else {
    printf("test_sys_forcewrite"
           ": Bytes written %d\n",
           result);
  }
  if ((result = close(fd)) < 0) {
    perror("close");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <main+0x135>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d2 <main+0x72>
lea    0xe95(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe84(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xeb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x40,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x24(%rbp),%ecx
lea    0xeb3(%rip),%rdi
mov    $0x123,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x123,%edi
call   1050 <syscall@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1253 <main+0xf3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%edx
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1264 <main+0x104>
mov    -0x28(%rbp),%esi
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    128e <main+0x12e>
lea    0xed7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    11f7 <main+0x87>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1235 <main+0xc5>
mov    %eax,%ebp
lea    0xea3(%rip),%rdi
lea    0xe5f(%rip),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%rdx
lea    0xea8(%rip),%rdi
mov    $0x123,%esi
mov    $0x40,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x123,%edi
mov    %ebp,%esi
mov    %r14,%rdx
mov    $0x40,%ecx
call   1060 <syscall@plt>
cmp    $0xffffffff,%eax
je     120a <main+0x9a>
lea    0xeeb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1221 <main+0xb1>
lea    0xf06(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1230 <main+0xc0>
mov    0x8(%rbx),%rsi
lea    0xe8d(%rip),%rdi
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
js     123e <main+0xce>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc4(%rip),%rdi
jmp    1245 <main+0xd5>
lea    0xeb9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [88 x i8] c"test_sys_forcewrite: Not enough arguments, please supply someone elses file to destroy\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [61 x i8] c"Mary had a little lamb, and two helpings of peas and carrots\00"
@.str.3 = [33 x i8] c"test_sys_forcewrite: string: %s\0A\00"
@.str.4 = [63 x i8] c"test_sys_forcewrite: Trying sys call %d on file: %s, bytes %d\0A\00"
@.str.5 = [53 x i8] c"test_sys_forcewrite: failed for file: %s, result %d\0A\00"
@.str.6 = [39 x i8] c"test_sys_forcewrite: Bytes written %d\0A\00"
@.str.7 = [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 i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
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_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
store i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i8** %7
%25 = load i8*, i8** %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %25)
store i32 64, i32* %8
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i32 291, i8* %29, i32 %30)
%32 = load i32, i32* %6
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i32 @syscall(i32 291, i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %24
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = load i32, i32* %9
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %41, i32 %42)
br label %47
44: ; preds = %24
%45 = load i32, i32* %9
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %44, %38
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
store i32 %49, i32* %9
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %47
store i32 0, i32* %3
br label %55
55: ; preds = %54, %12
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [61 x i8] c"Mary had a little lamb, and two helpings of peas and carrots\00"
@.str.3 = [33 x i8] c"test_sys_forcewrite: string: %s\0A\00"
@.str.4 = [63 x i8] c"test_sys_forcewrite: Trying sys call %d on file: %s, bytes %d\0A\00"
@.str.5 = [53 x i8] c"test_sys_forcewrite: failed for file: %s, result %d\0A\00"
@.str.6 = [39 x i8] c"test_sys_forcewrite: Bytes written %d\0A\00"
@.str.7 = [6 x i8] c"close\00"
@str = [87 x i8] c"test_sys_forcewrite: Not enough arguments, please supply someone elses file to destroy\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([87 x i8], [87 x i8]* @str, i64 0, i64 0))
br label %32
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %6
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0))
%17 = load i8*, i8** %7
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i32 291, i8* %17, i32 64)
%19 = tail call i32 @syscall(i32 291, i32 %10, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 64)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i8*, i8** %7
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %22, i32 -1)
br label %26
24: ; preds = %15
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %19)
br label %26
26: ; preds = %24, %21
%27 = tail call i32 @close(i32 %10)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %26, %4
%33 = phi i32 [ -1, %4 ], [ 0, %26 ]
ret i32 %33
}