#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 */
scalar_t__ EEXIST;
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int creat(char *, int);
char *ctime(int /*<<< orphan*/ *);
scalar_t__ errno;
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(int argc, char **argv) {
  int fd = open(argv[1], O_WRONLY | O_CREAT | O_EXCL, 0666);
  if (fd == -1) {
    perror("open");
    if (errno == EEXIST) {
      fd = creat(argv[1], 0666);
    } else {
      return -1;
    }
  }
  char wbuf[4096] = {0};
  time_t t;
  int i;
  for (i = 0; i < 5; ++i) {
    time(&t);
    sleep(1);
    snprintf(wbuf, 4096 - 1, "%s", ctime(&t));
    write(fd, wbuf, strlen(wbuf) + 1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    0x2de6(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1254 <main+0xa4>
lea    0xdf6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    1243 <main+0x93>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1b6,%esi
call   10c0 <creat@plt>
mov    %eax,-0x14(%rbp)
jmp    124f <main+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <main+0x178>
jmp    1254 <main+0xa4>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
movl   $0x0,-0x1028(%rbp)
cmpl   $0x5,-0x1028(%rbp)
jge    1319 <main+0x169>
lea    -0x1024(%rbp),%rdi
call   1090 <time@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1040(%rbp)
lea    -0x1024(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x1040(%rbp),%rdi
mov    %rax,%rcx
mov    $0xfff,%esi
lea    0xd41(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
jmp    1271 <main+0xc1>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%rbp
mov    0x2def(%rip),%rax
mov    0x2e28(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbp),%rdi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1382 <main+0x1d2>
lea    0x10(%rsp),%rbp
xor    %r14d,%r14d
mov    $0x1000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1090 <time@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %r15,%rdi
call   1040 <ctime@plt>
lea    0xdd2(%rip),%r12
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
mov    %r15,%rdi
call   1090 <time@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %r15,%rdi
call   1040 <ctime@plt>
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
mov    %r15,%rdi
call   1090 <time@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %r15,%rdi
call   1040 <ctime@plt>
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
mov    %r15,%rdi
call   1090 <time@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %r15,%rdi
call   1040 <ctime@plt>
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
mov    %r15,%rdi
call   1090 <time@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %r15,%rdi
call   1040 <ctime@plt>
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc77(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c33(%rip),%rax
mov    (%rax),%rax
mov    0x2c39(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
jne    136f <main+0x1bf>
mov    0x8(%rbp),%rdi
mov    $0x1b6,%esi
call   10c0 <creat@plt>
mov    %eax,%ebx
jmp    11f6 <main+0x46>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [5 x i8] c"open\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17, i32 438)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %33
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @creat(i8* %29, i32 438)
store i32 %30, i32* %6
br label %32
31: ; preds = %21
store i32 -1, i32* %3
br label %56
32: ; preds = %26
br label %33
33: ; preds = %32, %2
%34 = bitcast [4096 x i8]* %7 to i8*
store i32 0, i32* %9
br label %35
35: ; preds = %50, %33
%36 = load i32, i32* %9
%37 = icmp slt i32 %36, 5
br i1 %37, label %38, label %53
38: ; preds = %35
%39 = call i32 @time(i32* %8)
%40 = call i32 @sleep(i32 1)
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%42 = call i8* @ctime(i32* %8)
%43 = call i32 @snprintf(i8* %41, i32 4095, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %6
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%47 = call i64 @strlen(i8* %46)
%48 = add nsw i64 %47, 1
%49 = call i32 @write(i32 %44, i8* %45, i64 %48)
br label %50
50: ; preds = %38
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %35
53: ; preds = %35
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
store i32 0, i32* %3
br label %56
56: ; preds = %53, %31
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [5 x i8] c"open\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_EXCL
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %6, i32 %11, i32 438)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = load i64, i64* @errno
%17 = load i64, i64* @EEXIST
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %62
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = tail call i32 @creat(i8* %20, i32 438)
br label %22
22: ; preds = %19, %2
%23 = phi i32 [ %21, %19 ], [ %12, %2 ]
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%25 = bitcast i32* %4 to i8*
%26 = call i32 @time(i32* %4)
%27 = call i32 @sleep(i32 1)
%28 = call i8* @ctime(i32* %4)
%29 = call i32 @snprintf(i8* %24, i32 4095, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i64 @strlen(i8* %24)
%31 = add nsw i64 %30, 1
%32 = call i32 @write(i32 %23, i8* %24, i64 %31)
%33 = call i32 @time(i32* %4)
%34 = call i32 @sleep(i32 1)
%35 = call i8* @ctime(i32* %4)
%36 = call i32 @snprintf(i8* %24, i32 4095, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = call i64 @strlen(i8* %24)
%38 = add nsw i64 %37, 1
%39 = call i32 @write(i32 %23, i8* %24, i64 %38)
%40 = call i32 @time(i32* %4)
%41 = call i32 @sleep(i32 1)
%42 = call i8* @ctime(i32* %4)
%43 = call i32 @snprintf(i8* %24, i32 4095, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = call i64 @strlen(i8* %24)
%45 = add nsw i64 %44, 1
%46 = call i32 @write(i32 %23, i8* %24, i64 %45)
%47 = call i32 @time(i32* %4)
%48 = call i32 @sleep(i32 1)
%49 = call i8* @ctime(i32* %4)
%50 = call i32 @snprintf(i8* %24, i32 4095, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%51 = call i64 @strlen(i8* %24)
%52 = add nsw i64 %51, 1
%53 = call i32 @write(i32 %23, i8* %24, i64 %52)
%54 = call i32 @time(i32* %4)
%55 = call i32 @sleep(i32 1)
%56 = call i8* @ctime(i32* %4)
%57 = call i32 @snprintf(i8* %24, i32 4095, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %56)
%58 = call i64 @strlen(i8* %24)
%59 = add nsw i64 %58, 1
%60 = call i32 @write(i32 %23, i8* %24, i64 %59)
%61 = call i32 @close(i32 %23)
br label %62
62: ; preds = %14, %22
%63 = phi i32 [ 0, %22 ], [ -1, %14 ]
ret i32 %63
}