#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ daemon_init();
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  char *msg = "I'm daemon process...\n";
  int fd = -1;
  daemon_init();
  fd = open("/tmp/test_daemon.log", O_RDWR | O_CREAT | O_APPEND, 0666);
  if (fd < 0) {
    printf("open /tmp/test_daemon.log fail.\n");
    exit(1);
  }
  while (1) {
    write(fd, msg, strlen(msg));
    sleep(3);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    $0x0,%al
call   1060 <daemon_init@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
lea    0xe5d(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e7 <main+0x77>
lea    0xe54(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11ec <main+0x7c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
jmp    11ec <main+0x7c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <daemon_init@plt>
mov    0x2e5f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
lea    0xe7b(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11e0 <main+0x70>
mov    %eax,%ebp
lea    0xe4d(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
jmp    11c0 <main+0x50>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"I'm daemon process...\0A\00"
@.str.1 = [21 x i8] c"/tmp/test_daemon.log\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [33 x i8] c"open /tmp/test_daemon.log fail.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %2
store i32 -1, i32* %3
%4 = call i32 (...) @daemon_init()
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 438)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
br label %17
17: ; preds = %16, %17
%18 = load i32, i32* %3
%19 = load i8*, i8** %2
%20 = load i8*, i8** %2
%21 = call i32 @strlen(i8* %20)
%22 = call i32 @write(i32 %18, i8* %19, i32 %21)
%23 = call i32 @sleep(i32 3)
br label %17
}

LLVM IR (-O2):
@.str = [23 x i8] c"I'm daemon process...\0A\00"
@.str.1 = [21 x i8] c"/tmp/test_daemon.log\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@str = [32 x i8] c"open /tmp/test_daemon.log fail.\00"
define i32 @main() {
%1 = tail call i32 (...) @daemon_init()
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_APPEND
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 438)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0, %12
%13 = tail call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i32 @sleep(i32 3)
br label %12
}