#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 S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd, len;
  char str[100];
  fd = open("hello.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (fd) {
    write(fd, "Hello, Software Weekly", strlen("Hello, software weekly"));
    close(fd);
  }
  fd = open("hello.txt", O_RDWR);
  len = read(fd, str, 100);
  str[len] = '\0';
  printf("%s\n", str);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11dc <main+0x7c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x74(%rbp)
lea    0xe63(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    %eax,%edx
lea    0xe3b(%rip),%rsi
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    0x2e8a(%rip),%rax
mov    0x2e6b(%rip),%r14
mov    (%r14),%esi
or     (%rax),%esi
mov    0x2e47(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe6d(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
je     11c3 <main+0x63>
mov    %eax,%ebx
lea    0xe7a(%rip),%rdi
call   1040 <strlen@plt>
lea    0xe57(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%r14),%esi
lea    0xe33(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe43(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"hello.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [23 x i8] c"Hello, Software Weekly\00"
@.str.2 = [23 x i8] c"Hello, software weekly\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @write(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
br label %20
20: ; preds = %14, %0
%21 = load i32, i32* @O_RDWR
%22 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 100)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"hello.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [23 x i8] c"Hello, Software Weekly\00"
@.str.2 = [23 x i8] c"Hello, software weekly\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %0
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @write(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = tail call i32 @close(i32 %9)
br label %15
15: ; preds = %11, %0
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @read(i32 %17, i8* %2, i32 100)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%22 = call i32 @close(i32 %17)
ret i32 0
}