#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*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int write(int, char *, int);
int main(void) {
  int fd;
  int len = strlen("hello world\n");
  char *p = "hello world\n";
  if ((fd = open("test.txt", O_RDWR | O_CREAT)) == -1) {
    perror("fail to open");
    exit(1);
  }
  while (1) {
    if (write(fd, p, len) == -1) {
      perror("fail to write");
      exit(1);
    }
    sleep(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
lea    0xe7b(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11c9 <main+0x69>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11ce <main+0x6e>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11fc <main+0x9c>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11ce <main+0x6e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe95(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x2e67(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe82(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    119e <main+0x3e>
lea    0xe7a(%rip),%rdi
jmp    11e2 <main+0x82>
mov    %eax,%ebp
lea    0xe59(%rip),%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11db <main+0x7b>
lea    0xe44(%rip),%r14
nopl   0x0(%rax)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x60>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"hello world\0A\00"
@.str.1 = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@.str.3 = [14 x i8] c"fail to write\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %5, i32* %3
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
br label %15
15: ; preds = %14, %24
%16 = load i32, i32* %2
%17 = load i8*, i8** %4
%18 = load i32, i32* %3
%19 = call i32 @write(i32 %16, i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = call i32 @sleep(i32 1)
br label %15
}

LLVM IR (-O2):
@.str = [13 x i8] c"hello world\0A\00"
@.str.1 = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@.str.3 = [14 x i8] c"fail to write\00"
define i32 @main() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%9 = icmp eq i32 %8, -1
br i1 %9, label %13, label %16
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %16, %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %7, %16
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %13, label %16
}