#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*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ lock_operation(int, int /*<<< orphan*/);
int lseek(int, long, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int write(int, char *, int /*<<< orphan*/);
int main(int ac, char *av[]) {
  int fd;
  time_t now;
  char *message;
  if (ac != 2) {
    fprintf(stderr, "useage:file_ts filename\n");
    exit(1);
  }
  if ((fd = open(av[1], O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) {
    perror(av[1]);
    exit(2);
  };
  while (1) {
    time(&now);
    message = ctime(&now);
    lock_operation(fd, F_WRLCK);
    if (lseek(fd, 0L, SEEK_SET) == -1) {
      perror("lseek");
      exit(3);
    }
    if (write(fd, message, strlen(message)) == -1) {
      perror("write");
      exit(4);
    };
    lock_operation(fd, F_UNLCK);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ef <main+0x3f>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
mov    0x2d98(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    123f <main+0x8f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
jmp    1244 <main+0x94>
lea    -0x18(%rbp),%rdi
call   1090 <time@plt>
lea    -0x18(%rbp),%rdi
call   1040 <ctime@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
call   1070 <lock_operation@plt>
mov    -0x14(%rbp),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    129f <main+0xef>
lea    0xd89(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12e3 <main+0x133>
lea    0xd4b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
call   1070 <lock_operation@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    1244 <main+0x94>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1204 <main+0x54>
mov    %rsi,%rbx
mov    0x2e2e(%rip),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2dc8(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    1223 <main+0x73>
mov    0x8(%rbx),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <time@plt>
mov    %rbx,%rdi
call   1040 <ctime@plt>
mov    %rax,%rbx
mov    0x2d74(%rip),%r15
mov    (%r15),%esi
mov    %ebp,%edi
call   1070 <lock_operation@plt>
mov    0x2d6b(%rip),%r12
mov    (%r12),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x12d>
mov    0x2d6a(%rip),%r13
lea    0x4(%rsp),%r14
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   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x143>
mov    0x0(%r13),%esi
mov    %ebp,%edi
call   1070 <lock_operation@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    %r14,%rdi
call   1090 <time@plt>
mov    %r14,%rdi
call   1040 <ctime@plt>
mov    %rax,%rbx
mov    (%r15),%esi
mov    %ebp,%edi
call   1070 <lock_operation@plt>
mov    (%r12),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1280 <main+0xd0>
lea    0xd35(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>
lea    0xd25(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"useage:file_ts filename\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@.str.2 = [6 x i8] c"write\00"
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = load i32, i32* @O_WRONLY
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %18, i32 %23, i32 420)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %15
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @perror(i8* %29)
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %15
br label %33
33: ; preds = %32, %56
%34 = call i32 @time(i32* %7)
%35 = call i8* @ctime(i32* %7)
store i8* %35, i8** %8
%36 = load i32, i32* %6
%37 = load i32, i32* @F_WRLCK
%38 = call i32 @lock_operation(i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @lseek(i32 %39, i64 0, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%45 = call i32 @exit(i32 3)
unreachable
46: ; preds = %33
%47 = load i32, i32* %6
%48 = load i8*, i8** %8
%49 = load i8*, i8** %8
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 %47, i8* %48, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @exit(i32 4)
unreachable
56: ; preds = %46
%57 = load i32, i32* %6
%58 = load i32, i32* @F_UNLCK
%59 = call i32 @lock_operation(i32 %57, i32 %58)
%60 = call i32 @sleep(i32 1)
br label %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"useage:file_ts filename\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@.str.2 = [6 x i8] c"write\00"
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %14, %13
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %12, i32 %17, i32 420)
%19 = icmp eq i32 %18, -1
br i1 %19, label %28, label %20
20: ; preds = %10
%21 = call i32 @time(i32* %3)
%22 = call i8* @ctime(i32* %3)
%23 = load i32, i32* @F_WRLCK
%24 = call i32 @lock_operation(i32 %18, i32 %23)
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @lseek(i32 %18, i64 0, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %35
28: ; preds = %10
%29 = load i8*, i8** %11
%30 = tail call i32 @perror(i8* %29)
%31 = tail call i32 @exit(i32 2)
unreachable
32: ; preds = %43, %20
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 3)
unreachable
35: ; preds = %20, %43
%36 = phi i8* [ %48, %43 ], [ %22, %20 ]
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %18, i8* %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 4)
unreachable
43: ; preds = %35
%44 = load i32, i32* @F_UNLCK
%45 = call i32 @lock_operation(i32 %18, i32 %44)
%46 = call i32 @sleep(i32 1)
%47 = call i32 @time(i32* %3)
%48 = call i8* @ctime(i32* %3)
%49 = load i32, i32* @F_WRLCK
%50 = call i32 @lock_operation(i32 %18, i32 %49)
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @lseek(i32 %18, i64 0, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %32, label %35
}