#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*/ close(int);
int /*<<< orphan*/ fileDetails(char *);
int open(char *, char *);
int /*<<< orphan*/ printf(char *);
int read(char *, int, int, int);
int /*<<< orphan*/ touch(char *);
int write(char *, int, int, int);
void fileDemo() {
  char aux[] = {'w', '0'};
  printf("Creating file in cwd called: HelloWorld\n");
  touch("HelloWorld");
  int open1;
  printf("Opening in w mode and writing 'Hello world!'.\n");
  open1 = open("HelloWorld", "w");
  char *hello = "Hello world!";
  int status = write(hello, 14, 1, open1);
  close(open1);
  aux[0] = 'r';
  printf("Closing and opening same file in r mode\n");
  open1 = open("HelloWorld", "r");
  char buffer[50] = {0};
  status = read(buffer, 7, 2, open1);
  printf("Read: ");
  printf(buffer);
  printf("\n");
  close(open1);
  printf("File details:\n");
  fileDetails("HelloWorld");
  printf("You can now check the file is present, and see its details with the "
         "fileDetails <path> command.\n");
  return;
}


x86 assembly (clang, -O0):
<fileDemo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0xe71(%rip),%ax
mov    %ax,-0x2(%rbp)
lea    0xe68(%rip),%rdi
call   1050 <printf@plt>
lea    0xe85(%rip),%rdi
call   1030 <touch@plt>
lea    0xe84(%rip),%rdi
call   1050 <printf@plt>
lea    0xe6d(%rip),%rdi
lea    0xea0(%rip),%rsi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xe,%esi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movb   $0x72,-0x2(%rbp)
lea    0xe70(%rip),%rdi
call   1050 <printf@plt>
lea    0xe1b(%rip),%rdi
lea    0xe86(%rip),%rsi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x7,%esi
mov    $0x2,%edx
call   1080 <read@plt>
mov    %eax,-0x14(%rbp)
lea    0xe50(%rip),%rdi
call   1050 <printf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <printf@plt>
lea    0xdff(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    0xe2e(%rip),%rdi
call   1050 <printf@plt>
lea    0xda7(%rip),%rdi
call   1090 <fileDetails@plt>
lea    0xe25(%rip),%rdi
call   1050 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileDemo>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
lea    0xe79(%rip),%rdi
call   1050 <puts@plt>
lea    0xe8a(%rip),%r14
mov    %r14,%rdi
call   1030 <touch@plt>
lea    0xe86(%rip),%rdi
call   1050 <puts@plt>
lea    0xe3a(%rip),%rsi
mov    %r14,%rdi
call   10b0 <open@plt>
mov    %eax,%ebp
lea    0xe2b(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %eax,%ecx
call   1060 <write@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xe78(%rip),%rdi
call   1050 <puts@plt>
lea    0xe0d(%rip),%rsi
mov    %r14,%rdi
call   10b0 <open@plt>
mov    %eax,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movw   $0x0,0x30(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x7,%esi
mov    $0x2,%edx
mov    %eax,%ecx
call   1090 <read@plt>
lea    0xdcf(%rip),%rdi
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1070 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xe2f(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   10a0 <fileDetails@plt>
lea    0xe29(%rip),%rdi
call   1050 <puts@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.fileDemo.aux = [2 x i8] c"w0"
@.str = [41 x i8] c"Creating file in cwd called: HelloWorld\0A\00"
@.str.1 = [11 x i8] c"HelloWorld\00"
@.str.2 = [47 x i8] c"Opening in w mode and writing 'Hello world!'.\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [13 x i8] c"Hello world!\00"
@.str.5 = [41 x i8] c"Closing and opening same file in r mode\0A\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [7 x i8] c"Read: \00"
@.str.8 = [2 x i8] c"\0A\00"
@.str.9 = [15 x i8] c"File details:\0A\00"
@.str.10 = [97 x i8] c"You can now check the file is present, and see its details with the fileDetails <path> command.\0A\00"
define void @fileDemo() {
%1 = alloca [2 x i8]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [50 x i8]
%6 = bitcast [2 x i8]* %1 to i8*
%7 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @touch(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 %10, i32* %2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8** %3
%11 = load i8*, i8** %3
%12 = load i32, i32* %2
%13 = call i32 @write(i8* %11, i32 14, i32 1, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %2
%15 = call i32 @close(i32 %14)
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
store i8 114, i8* %16
%17 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
%18 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32 %18, i32* %2
%19 = bitcast [50 x i8]* %5 to i8*
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* %2
%22 = call i32 @read(i8* %20, i32 7, i32 2, i32 %21)
store i32 %22, i32* %4
%23 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%24 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%25 = call i32 @printf(i8* %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
%30 = call i32 @fileDetails(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @printf(i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str.10, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"HelloWorld\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [13 x i8] c"Hello world!\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [7 x i8] c"Read: \00"
@str = [40 x i8] c"Creating file in cwd called: HelloWorld\00"
@str.11 = [46 x i8] c"Opening in w mode and writing 'Hello world!'.\00"
@str.12 = [40 x i8] c"Closing and opening same file in r mode\00"
@str.13 = [14 x i8] c"File details:\00"
@str.14 = [96 x i8] c"You can now check the file is present, and see its details with the fileDetails <path> command.\00"
define void @fileDemo() {
%1 = alloca [50 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @touch(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.11, i64 0, i64 0))
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%6 = tail call i32 @write(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 14, i32 1, i32 %5)
%7 = tail call i32 @close(i32 %5)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.12, i64 0, i64 0))
%9 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%10 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%11 = call i32 @read(i8* %10, i32 7, i32 2, i32 %9)
%12 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%13 = call i32 @printf(i8* %10)
%14 = call i32 @putchar(i32 10)
%15 = call i32 @close(i32 %9)
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0))
%17 = call i32 @fileDetails(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([96 x i8], [96 x i8]* @str.14, i64 0, i64 0))
ret void
}