#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_RDWR;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd, pos;
  char *buffer = "hello world!\n";
  char input[80];
  if (argc != 2) {
    printf("Use: %s <filename>\n", argv[0]);
    return -1;
  }
  unlink(argv[1]);
  fd = open(argv[1], O_WRONLY | O_CREAT, 0700);
  if (fd < 0) {
    perror(argv[1]);
    return -1;
  }
  write(fd, buffer, strlen(buffer));
  close(fd);
  fd = open(argv[1], O_RDWR | O_TRUNC, 0700);
  if (fd < 0) {
    perror(argv[1]);
    return -1;
  }
  pos = read(fd, input, 80);
  if (pos < 0) {
    perror("read");
    return -1;
  }
  if (pos != 0) {
    printf("FAILURE (%d) (%s)\n", pos, input);
  } else {
    printf("SUCCESS\n");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cf <main+0x4f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <main+0x194>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
mov    $0x1c0,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1226 <main+0xa6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <main+0x194>
mov    -0x14(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    0x2d85(%rip),%rax
or     (%rax),%esi
mov    $0x1c0,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    129d <main+0x11d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <main+0x194>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d3 <main+0x153>
lea    0xd60(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <main+0x194>
cmpl   $0x0,-0x18(%rbp)
je     12f7 <main+0x177>
mov    -0x18(%rbp),%esi
lea    -0x70(%rbp),%rdx
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1305 <main+0x185>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1249 <main+0xb9>
mov    0x8(%rbx),%rdi
call   1030 <unlink@plt>
mov    0x8(%rbx),%rdi
mov    0x2e08(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1c0,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     127f <main+0xef>
mov    %eax,%ebp
lea    0xe22(%rip),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x8(%rbx),%rdi
mov    0x2dd4(%rip),%rax
mov    0x2de5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1c0,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     127f <main+0xef>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x50,%edx
call   1090 <read@plt>
test   %eax,%eax
js     1285 <main+0xf5>
je     1261 <main+0xd1>
lea    0xdec(%rip),%rdi
mov    %rsp,%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    126d <main+0xdd>
mov    (%rbx),%rsi
lea    0xdbb(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%eax
jmp    1276 <main+0xe6>
lea    0xdd2(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%rbx),%rdi
jmp    128c <main+0xfc>
lea    0xd96(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1276 <main+0xe6>

LLVM IR (-O0):
@.str = [14 x i8] c"hello world!\0A\00"
@.str.1 = [20 x i8] c"Use: %s <filename>\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [19 x i8] c"FAILURE (%d) (%s)\0A\00"
@.str.4 = [9 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 i32
%8 = alloca i8*
%9 = alloca [80 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %78
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @unlink(i8* %20)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %24, i32 %27, i32 448)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %17
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @perror(i8* %34)
store i32 -1, i32* %3
br label %78
36: ; preds = %17
%37 = load i32, i32* %6
%38 = load i8*, i8** %8
%39 = load i8*, i8** %8
%40 = call i32 @strlen(i8* %39)
%41 = call i32 @write(i32 %37, i8* %38, i32 %40)
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = load i32, i32* @O_RDWR
%48 = load i32, i32* @O_TRUNC
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %46, i32 %49, i32 448)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %36
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 @perror(i8* %56)
store i32 -1, i32* %3
br label %78
58: ; preds = %36
%59 = load i32, i32* %6
%60 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 80)
store i32 %61, i32* %7
%62 = load i32, i32* %7
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %58
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %78
66: ; preds = %58
%67 = load i32, i32* %7
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %66
%70 = load i32, i32* %7
%71 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %70, i8* %71)
br label %75
73: ; preds = %66
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %75
75: ; preds = %73, %69
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
store i32 0, i32* %3
br label %78
78: ; preds = %75, %64, %53, %31, %12
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@.str = [14 x i8] c"hello world!\0A\00"
@.str.1 = [20 x i8] c"Use: %s <filename>\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [19 x i8] c"FAILURE (%d) (%s)\0A\00"
@str = [8 x i8] c"SUCCESS\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %48
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @unlink(i8* %11)
%13 = load i8*, i8** %10
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %13, i32 %16, i32 448)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = load i8*, i8** %10
%21 = tail call i32 @perror(i8* %20)
br label %48
22: ; preds = %9
%23 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = tail call i32 @close(i32 %17)
%26 = load i8*, i8** %10
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %28, %27
%30 = tail call i32 @open(i8* %26, i32 %29, i32 448)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = load i8*, i8** %10
%34 = tail call i32 @perror(i8* %33)
br label %48
35: ; preds = %22
%36 = call i32 @read(i32 %30, i8* %4, i32 80)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %48
40: ; preds = %35
%41 = icmp eq i32 %36, 0
br i1 %41, label %44, label %42
42: ; preds = %40
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %36, i8* %4)
br label %46
44: ; preds = %40
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %46
46: ; preds = %44, %42
%47 = call i32 @close(i32 %30)
br label %48
48: ; preds = %46, %38, %32, %19, %6
%49 = phi i32 [ -1, %6 ], [ -1, %19 ], [ -1, %32 ], [ -1, %38 ], [ 0, %46 ]
ret i32 %49
}