#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 */
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int creat(char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ scanf(char *, char *);
int write(int, int /*<<< orphan*/, int);
int main(int argc, char *argv[]) {
  int fd, ret, ret1, ret2;
  char ch1;
  char buff[256];
  DIR *dir;
  struct dirent *entry;
  if ((dir = opendir("/home/suraj/Desktop")) == NULL) {
    printf("Unable to open specified directory\n");
    return -1;
  }
  fd = creat("demo.txt", 0x777);
  if (fd == -1) {
    printf("Unable to create file : already exist\n");
  } else {
    printf(
        "\"demo.txt\" File is successfully created with file descriptor %d \n",
        fd);
    while ((entry = readdir(dir)) != NULL) {
      ret1 = write(fd, entry->d_name, sizeof(buff));
      memset(buff, 0, sizeof(buff));
    }
    close(fd);
  }
  printf("Want to read contents from this file ? ( y / n ) : ");
  scanf("%c", &ch1);
  if (ch1 == 'y') {
    fd = open("demo.txt", O_RDONLY);
    while ((ret2 = read(fd, buff, sizeof(buff))) != 0) {
      printf("%s\n", buff);
    }
  }
  close(fd);
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x138(%rbp)
cmp    $0x0,%rax
jne    1200 <main+0x50>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <main+0x185>
lea    0xe31(%rip),%rdi
mov    $0x777,%esi
call   10d0 <creat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1231 <main+0x81>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1298 <main+0xe8>
mov    -0x14(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x138(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x140(%rbp)
cmp    $0x0,%rax
je     1290 <main+0xe0>
mov    -0x14(%rbp),%edi
mov    -0x140(%rbp),%rax
mov    (%rax),%esi
mov    $0x100,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
jmp    1242 <main+0x92>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe31(%rip),%rdi
lea    -0x21(%rbp),%rsi
call   10b0 <scanf@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x79,%eax
jne    131a <main+0x16a>
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
lea    0xd65(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
call   1090 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1315 <main+0x165>
lea    -0x130(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12db <main+0x12b>
jmp    131a <main+0x16a>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x138(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
lea    0xe2c(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1253 <main+0x93>
mov    %rax,%r15
lea    0xe2c(%rip),%rdi
mov    $0x777,%esi
call   10e0 <creat@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     126a <main+0xaa>
lea    0xe1d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     124a <main+0x8a>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    (%rax),%esi
mov    %ebp,%edi
mov    $0x100,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
jne    1220 <main+0x60>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1276 <main+0xb6>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
jmp    12fa <main+0x13a>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
lea    0xde2(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe05(%rip),%rdi
lea    0xf(%rsp),%rsi
call   10c0 <scanf@plt>
cmpb   $0x79,0xf(%rsp)
jne    12eb <main+0x12b>
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
lea    0xd65(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x100,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     12eb <main+0x12b>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   10a0 <read@plt>
test   %eax,%eax
jne    12d0 <main+0x110>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r15,%rdi
call   1090 <closedir@plt>
mov    %r14d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [20 x i8] c"/home/suraj/Desktop\00"
@.str.1 = [36 x i8] c"Unable to open specified directory\0A\00"
@.str.2 = [9 x i8] c"demo.txt\00"
@.str.3 = [39 x i8] c"Unable to create file : already exist\0A\00"
@.str.4 = [66 x i8] c"\22demo.txt\22 File is successfully created with file descriptor %d \0A\00"
@.str.5 = [52 x i8] c"Want to read contents from this file ? ( y / n ) : \00"
@.str.6 = [3 x i8] c"%c\00"
@O_RDONLY = global i32 0
@.str.7 = [4 x i8] c"%s\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 i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca [256 x i8]
%12 = alloca i32*
%13 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32* @opendir(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %12
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
18: ; preds = %2
%19 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 1911)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %42
24: ; preds = %18
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %31, %24
%28 = load i32*, i32** %12
%29 = call %struct.dirent* @readdir(i32* %28)
store %struct.dirent* %29, %struct.dirent** %13
%30 = icmp ne %struct.dirent* %29, null
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = load i32, i32* %6
%33 = load %struct.dirent*, %struct.dirent** %13
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @write(i32 %32, i32 %35, i32 256)
store i32 %36, i32* %8
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 256)
br label %27
39: ; preds = %27
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39, %22
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %10)
%45 = load i8, i8* %10
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 121
br i1 %47, label %48, label %60
48: ; preds = %42
%49 = load i32, i32* @O_RDONLY
%50 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %49)
store i32 %50, i32* %6
br label %51
51: ; preds = %56, %48
%52 = load i32, i32* %6
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 256)
store i32 %54, i32* %9
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %57)
br label %51
59: ; preds = %51
br label %60
60: ; preds = %59, %42
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
%63 = load i32*, i32** %12
%64 = call i32 @closedir(i32* %63)
store i32 0, i32* %3
br label %65
65: ; preds = %60, %16
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [20 x i8] c"/home/suraj/Desktop\00"
@.str.2 = [9 x i8] c"demo.txt\00"
@.str.4 = [66 x i8] c"\22demo.txt\22 File is successfully created with file descriptor %d \0A\00"
@.str.5 = [52 x i8] c"Want to read contents from this file ? ( y / n ) : \00"
@.str.6 = [3 x i8] c"%c\00"
@O_RDONLY = global i32 0
@str = [38 x i8] c"Unable to create file : already exist\00"
@str.8 = [35 x i8] c"Unable to open specified directory\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.8, i64 0, i64 0))
br label %47
10: ; preds = %2
%11 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 1911)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %29
15: ; preds = %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i32 %11)
%17 = call %struct.dirent* @readdir(i32* %6)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %27, label %19
19: ; preds = %15, %19
%20 = phi %struct.dirent* [ %25, %19 ], [ %17, %15 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @write(i32 %11, i32 %22, i32 256)
%24 = call i32 @memset(i8* %5, i32 0, i32 256)
%25 = call %struct.dirent* @readdir(i32* %6)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %19
27: ; preds = %19, %15
%28 = call i32 @close(i32 %11)
br label %29
29: ; preds = %27, %13
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0))
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %3)
%32 = load i8, i8* %3
%33 = icmp eq i8 %32, 121
br i1 %33, label %34, label %43
34: ; preds = %29
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = call i32 @read(i32 %36, i8* %5, i32 256)
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %34, %39
%40 = call i32 @puts(i8* %5)
%41 = call i32 @read(i32 %36, i8* %5, i32 256)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %39
43: ; preds = %39, %34, %29
%44 = phi i32 [ %11, %29 ], [ %36, %34 ], [ %36, %39 ]
%45 = call i32 @close(i32 %44)
%46 = call i32 @closedir(i32* %6)
br label %47
47: ; preds = %43, %8
%48 = phi i32 [ -1, %8 ], [ 0, %43 ]
ret i32 %48
}