#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_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
void work() {
  char ch = 'A';
  for (;;) {
    int fd = open("deamon.dat", O_WRONLY | O_CREAT | O_APPEND, 0666);
    if (fd != 0) {
      write(fd, &ch, 1);
      close(fd);
    }
    if (ch++ > 'Z') {
      ch = 'A';
    }
    sleep(2);
  }
}


x86 assembly (clang, -O0):
<work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movb   $0x41,-0x1(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    0x2e9c(%rip),%rax
or     (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
lea    0xe92(%rip),%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     119e <work+0x5e>
mov    -0x8(%rbp),%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x1(%rbp),%al
mov    %al,%cl
add    $0x1,%cl
mov    %cl,-0x1(%rbp)
movsbl %al,%eax
cmp    $0x5a,%eax
jle    11b9 <work+0x79>
movb   $0x41,-0x1(%rbp)
mov    $0x2,%edi
call   1060 <sleep@plt>
jmp    114c <work+0xc>

x86 assembly (clang, -O2):
<work>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movb   $0x41,0x7(%rsp)
mov    $0x41,%bl
mov    0x2e9f(%rip),%r13
mov    0x2e68(%rip),%r14
lea    0xe99(%rip),%r12
mov    $0x41,%r15d
jmp    118b <work+0x4b>
nop
lea    0x1(%rbx),%eax
cmp    $0x5b,%bl
movzbl %al,%ebx
cmovge %r15d,%ebx
mov    %bl,0x7(%rsp)
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    0x0(%r13),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
or     (%r14),%esi
mov    %r12,%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
test   %eax,%eax
je     1170 <work+0x30>
mov    %eax,%ebp
mov    %eax,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1170 <work+0x30>

LLVM IR (-O0):
@.str = [11 x i8] c"deamon.dat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
define void @work() {
%1 = alloca i8
%2 = alloca i32
store i8 65, i8* %1
br label %3
3: ; preds = %23, %0
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8, i32 438)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load i32, i32* %2
%14 = call i32 @write(i32 %13, i8* %1, i32 1)
%15 = load i32, i32* %2
%16 = call i32 @close(i32 %15)
br label %17
17: ; preds = %12, %3
%18 = load i8, i8* %1
%19 = add i8 %18, 1
store i8 %19, i8* %1
%20 = sext i8 %18 to i32
%21 = icmp sgt i32 %20, 90
br i1 %21, label %22, label %23
22: ; preds = %17
store i8 65, i8* %1
br label %23
23: ; preds = %22, %17
%24 = call i32 @sleep(i32 2)
br label %3
}

LLVM IR (-O2):
@.str = [11 x i8] c"deamon.dat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
define void @work() {
%1 = alloca i8
store i8 65, i8* %1
br label %2
2: ; preds = %14, %0
%3 = phi i8 [ %17, %14 ], [ 65, %0 ]
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8, i32 438)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i32 @write(i32 %9, i8* %1, i32 1)
%13 = tail call i32 @close(i32 %9)
br label %14
14: ; preds = %11, %2
%15 = add i8 %3, 1
%16 = icmp sgt i8 %3, 90
%17 = select i1 %16, i8 65, i8 %15
store i8 %17, i8* %1
%18 = tail call i32 @sleep(i32 2)
br label %2
}