#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 /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, long, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  int handle;
  char msg[] = "This is a test";
  char ch[50];
  int n;
  handle = open("hello.txt", O_CREAT | O_RDWR, 0777);
  write(handle, msg, strlen(msg));
  lseek(handle, 0L, SEEK_SET);
  while ((n = read(handle, ch, 50)) > 0) {
    ch[n] = '\0';
    printf("%s\n", ch);
    sleep(1);
  }
  close(handle);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x17(%rbp)
mov    0xe68(%rip),%eax
mov    %eax,-0xf(%rbp)
mov    0xe62(%rip),%ax
mov    %ax,-0xb(%rbp)
mov    0xe5a(%rip),%al
mov    %al,-0x9(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
lea    0xe3f(%rip),%rdi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x64(%rbp)
lea    -0x17(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x17(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x32,%edx
call   1080 <read@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jle    125c <main+0xdc>
movslq -0x54(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rsi
lea    0xdd1(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1217 <main+0x97>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
movabs $0x74736574206120,%rax
mov    %rax,0x7(%rsp)
movabs $0x2073692073696854,%rax
69 73 20
mov    %rax,(%rsp)
mov    0x2e4c(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe51(%rip),%rdi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x32,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    124f <main+0xcf>
lea    0xe0a(%rip),%r14
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x32,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1220 <main+0xa0>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.msg = [15 x i8] c"This is a test\00"
@.str = [10 x i8] c"hello.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [15 x i8]
%4 = alloca [50 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [15 x i8]* %3 to i8*
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9, i32 511)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%13 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = call i32 @write(i32 %11, i8* %12, i32 %14)
%16 = load i32, i32* %2
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek(i32 %16, i64 0, i32 %17)
br label %19
19: ; preds = %24, %0
%20 = load i32, i32* %2
%21 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 50)
store i32 %22, i32* %5
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @sleep(i32 1)
br label %19
31: ; preds = %19
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
ret i32 0
}

LLVM IR (-O2):
@__const.main.msg = [15 x i8] c"This is a test\00"
@.str = [10 x i8] c"hello.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [15 x i8]
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 511)
%9 = call i32 @strlen(i8* %3)
%10 = call i32 @write(i32 %8, i8* %3, i32 %9)
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @lseek(i32 %8, i64 0, i32 %11)
%13 = call i32 @read(i32 %8, i8* %4, i32 50)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %0, %15
%16 = phi i32 [ %21, %15 ], [ %13, %0 ]
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %17
store i8 0, i8* %18
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%20 = call i32 @sleep(i32 1)
%21 = call i32 @read(i32 %8, i8* %4, i32 50)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %15, label %23
23: ; preds = %15, %0
%24 = call i32 @close(i32 %8)
ret i32 0
}