#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(unsigned char *, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(unsigned char *);
int /*<<< orphan*/ write(int, unsigned char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  unsigned char buf[256];
  if (argc < 2) {
    printf("\nFilename is missing\n");
    exit(1);
  }
  fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0660);
  if (fd < 0) {
    printf("Could not open file\n");
    exit(2);
  }
  printf("\n\nSimple text editor\n");
  printf("Enter few lines, When finished press control-D to quit\n");
  while (fgets(buf, 256, stdin)) {
    write(fd, buf, strlen(buf));
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a9 <main+0x39>
lea    0xe66(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
mov    $0x1b0,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f9 <main+0x89>
lea    0xe2c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1271 <main+0x101>
mov    -0x14(%rbp),%eax
mov    %eax,-0x12c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x12c(%rbp),%edi
mov    -0x128(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1211 <main+0xa1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
cmp    $0x1,%edi
jle    122e <main+0xbe>
mov    0x2e4d(%rip),%rcx
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x8(%rsi),%rdi
mov    0x2e4f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1b0,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1244 <main+0xd4>
mov    %eax,%ebp
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df8(%rip),%r14
mov    (%r14),%edx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     1219 <main+0xa9>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    11f0 <main+0x80>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"\0AFilename is missing\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [21 x i8] c"Could not open file\0A\00"
@.str.2 = [22 x i8] c"\0A\0ASimple text editor\0A\00"
@.str.3 = [56 x i8] c"Enter few lines, When finished press control-D to quit\0A\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
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_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21, i32 432)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %13
%29 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %36, %28
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%33 = load i32, i32* @stdin
%34 = call i64 @fgets(i8* %32, i32 256, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i32, i32* %6
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = call i32 @write(i32 %37, i8* %38, i32 %40)
br label %31
42: ; preds = %31
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stdin = global i32 0
@str = [21 x i8] c"\0A\0ASimple text editor\00"
@str.4 = [55 x i8] c"Enter few lines, When finished press control-D to quit\00"
@str.5 = [20 x i8] c"Could not open file\00"
@str.6 = [21 x i8] c"\0AFilename is missing\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %11, i32 %16, i32 432)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %9
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.4, i64 0, i64 0))
%25 = load i32, i32* @stdin
%26 = call i64 @fgets(i8* %4, i32 256, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %22, %28
%29 = call i32 @strlen(i8* %4)
%30 = call i32 @write(i32 %17, i8* %4, i32 %29)
%31 = load i32, i32* @stdin
%32 = call i64 @fgets(i8* %4, i32 256, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %28
34: ; preds = %28, %22
%35 = call i32 @close(i32 %17)
ret i32 0
}