#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*/ open_rdonly(char *);
int open_rdwr(char *);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, size_t);
int execute_read() {
  int fd = open_rdwr("syscalls.txt");
  char *value = "Sample output text\n";
  size_t sizeOfValue = strlen(value) * sizeof(char);
  write(fd, value, sizeOfValue);
  close(fd);
  char data[20];
  read(open_rdonly("syscalls.txt"), data, 20);
  return 0;
}


x86 assembly (clang, -O0):
<execute_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
call   1070 <open_rdwr@plt>
mov    %eax,-0x4(%rbp)
lea    0xe8f(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
lea    0xe4c(%rip),%rdi
call   1040 <open_rdonly@plt>
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xe91(%rip),%r14
mov    %r14,%rdi
call   1070 <open_rdwr@plt>
mov    %eax,%ebp
lea    0xe8d(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1040 <open_rdonly@plt>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1080 <read@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"syscalls.txt\00"
@.str.1 = [20 x i8] c"Sample output text\0A\00"
define i32 @execute_read() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [20 x i8]
%5 = call i32 @open_rdwr(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %5, i32* %1
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 1
store i64 %9, i64* %3
%10 = load i32, i32* %1
%11 = load i8*, i8** %2
%12 = load i64, i64* %3
%13 = call i32 @write(i32 %10, i8* %11, i64 %12)
%14 = load i32, i32* %1
%15 = call i32 @close(i32 %14)
%16 = call i32 @open_rdonly(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 20)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"syscalls.txt\00"
@.str.1 = [20 x i8] c"Sample output text\0A\00"
define i32 @execute_read() {
%1 = alloca [20 x i8]
%2 = tail call i32 @open_rdwr(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%4 = sext i32 %3 to i64
%5 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i64 %4)
%6 = tail call i32 @close(i32 %2)
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%8 = tail call i32 @open_rdonly(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @read(i32 %8, i8* %7, i32 20)
ret i32 0
}