#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;
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ lock_operation(int, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, 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 argc, char *argv[]) {
  int fd;
  time_t now;
  char *msg;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) {
    perror(argv[1]);
    exit(2);
  };
  while (1) {
    time(&now);
    msg = ctime(&now);
    lock_operation(fd, F_WRLCK);
    if (lseek(fd, 0, SEEK_SET) == -1) {
      perror("lseek");
      exit(3);
    };
    if (write(fd, msg, strlen(msg)) == -1) {
      perror("write");
      exit(4);
    };
    lock_operation(fd, F_UNLCK);
    sleep(1);
  }
  close(fd);
  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     11f6 <main+0x46>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
mov    0x2d91(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1246 <main+0x96>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
jmp    124b <main+0x9b>
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    0x2d4d(%rip),%rax
mov    (%rax),%esi
call   1070 <lock_operation@plt>
mov    -0x14(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12a4 <main+0xf4>
lea    0xd81(%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    12e8 <main+0x138>
lea    0xd43(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
call   1070 <lock_operation@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    124b <main+0x9b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1204 <main+0x54>
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    1226 <main+0x76>
mov    0x8(%rbx),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xde9(%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    0x2d71(%rip),%r15
mov    (%r15),%esi
mov    %ebp,%edi
call   1070 <lock_operation@plt>
mov    0x2d68(%rip),%r12
mov    (%r12),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x12d>
mov    0x2d67(%rip),%r13
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
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    0xd32(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>
lea    0xd22(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10c0 <exit@plt>

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

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