#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 */
typedef int /*<<< orphan*/ time_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
char *asctime(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int daemon(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ localtime(int /*<<< orphan*/ *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  time_t t;
  int fd;
  if (daemon(0, 0) == -1)
    do {
      perror("daemon error");
      exit(EXIT_FAILURE);
    } while (0);
  ;
  while (1) {
    fd = open("/home/vagrant/learn-c/2017/10/18/daemon/daemon.log",
              O_WRONLY | O_CREAT | O_APPEND, 0644);
    if (fd == -1)
      do {
        perror("open error");
        exit(EXIT_FAILURE);
      } while (0);
    ;
    t = time(0);
    char *buf = asctime(localtime(&t));
    write(fd, buf, strlen(buf));
    close(fd);
    sleep(60);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   1090 <daemon@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x40>
jmp    11d6 <main+0x26>
lea    0xe23(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    11f5 <main+0x45>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
mov    0x2db2(%rip),%rax
or     (%rax),%esi
lea    0xdf6(%rip),%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    124d <main+0x9d>
jmp    1233 <main+0x83>
lea    0xe06(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,-0x8(%rbp)
lea    -0x8(%rbp),%rdi
call   1030 <localtime@plt>
mov    %eax,%edi
call   1060 <asctime@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0x3c,%edi
call   10d0 <sleep@plt>
jmp    11f5 <main+0x45>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
xor    %esi,%esi
call   1090 <daemon@plt>
cmp    $0xffffffff,%eax
jne    11d5 <main+0x25>
lea    0xe30(%rip),%rdi
jmp    128a <main+0xda>
mov    0x2dec(%rip),%r13
mov    0x2e15(%rip),%r12
mov    (%r12),%esi
or     0x0(%r13),%esi
mov    0x2dce(%rip),%r14
or     (%r14),%esi
lea    0xe11(%rip),%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xd3>
mov    %eax,%ebp
lea    0xdf9(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   1030 <localtime@plt>
mov    %eax,%edi
call   1060 <asctime@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x3c,%edi
call   10d0 <sleep@plt>
mov    (%r12),%esi
or     0x0(%r13),%esi
or     (%r14),%esi
mov    %r15,%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1220 <main+0x70>
lea    0xdb6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"daemon error\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [51 x i8] c"/home/vagrant/learn-c/2017/10/18/daemon/daemon.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [11 x i8] c"open error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i32 @daemon(i32 0, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %13
7: ; preds = %0
br label %8
8: ; preds = %7
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; No predecessors!
br label %13
13: ; preds = %12, %0
br label %14
14: ; preds = %13, %29
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_APPEND
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 420)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %14
br label %24
24: ; preds = %23
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; No predecessors!
br label %29
29: ; preds = %28, %14
%30 = call i32 @time(i32 0)
store i32 %30, i32* %2
%31 = call i32 @localtime(i32* %2)
%32 = call i8* @asctime(i32 %31)
store i8* %32, i8** %4
%33 = load i32, i32* %3
%34 = load i8*, i8** %4
%35 = load i8*, i8** %4
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
%38 = load i32, i32* %3
%39 = call i32 @close(i32 %38)
%40 = call i32 @sleep(i32 60)
br label %14
}

LLVM IR (-O2):
@.str = [13 x i8] c"daemon error\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [51 x i8] c"/home/vagrant/learn-c/2017/10/18/daemon/daemon.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [11 x i8] c"open error\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @daemon(i32 0, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %13, label %5
5: ; preds = %0
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_APPEND
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 420)
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %21
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %21, %5
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %5, %21
%22 = phi i32 [ %35, %21 ], [ %11, %5 ]
%23 = call i32 @time(i32 0)
store i32 %23, i32* %1
%24 = call i32 @localtime(i32* %1)
%25 = call i8* @asctime(i32 %24)
%26 = call i32 @strlen(i8* %25)
%27 = call i32 @write(i32 %22, i8* %25, i32 %26)
%28 = call i32 @close(i32 %22)
%29 = call i32 @sleep(i32 60)
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %31, %30
%33 = load i32, i32* @O_APPEND
%34 = or i32 %32, %33
%35 = call i32 @open(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 420)
%36 = icmp eq i32 %35, -1
br i1 %36, label %17, label %21
}