#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*/ buf;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ sync();
int /*<<< orphan*/ usage(char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, rc;
  int do_sync = 0;
  int i = 0;
  int file_arg = 1;
  char buf[4096];
  memset(buf, 0, 4096);
  if (argc < 2 || argc > 3) {
    usage(argv[0]);
    exit(1);
  }
  if (strcmp(argv[1], "-s") == 0) {
    do_sync = 1;
    file_arg++;
  }
  fd = open(argv[file_arg], O_RDWR | O_CREAT, 0600);
  if (fd == -1) {
    printf("Error opening %s\n", argv[1]);
    exit(1);
  }
  while (1) {
    sprintf(buf, "write %d\n", i);
    rc = write(fd, buf, sizeof(buf));
    if (do_sync)
      sync();
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
jl     11f5 <main+0x55>
cmpl   $0x3,-0x8(%rbp)
jle    120b <main+0x6b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xde6(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1239 <main+0x99>
movl   $0x1,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    0x2da3(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    128c <main+0xec>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd86(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1291 <main+0xf1>
lea    -0x1030(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xd73(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12cf <main+0x12f>
mov    $0x0,%al
call   1070 <sync@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1291 <main+0xf1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
add    $0xfffffffc,%ebx
cmp    $0xfffffffd,%ebx
jbe    1254 <main+0xb4>
mov    0x8(%r14),%rdi
lea    0xe28(%rip),%rsi
call   1060 <strcmp@plt>
mov    %rax,%r15
xor    %eax,%eax
test   %r15,%r15
sete   %al
mov    0x8(%r14,%rax,8),%rdi
mov    0x2dec(%rip),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1266 <main+0xc6>
mov    %eax,%ebp
lea    0xdfe(%rip),%r14
mov    %rsp,%rbx
jmp    122a <main+0x8a>
nopl   0x0(%rax)
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   10a0 <sprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
test   %r15,%r15
jne    1220 <main+0x80>
xor    %eax,%eax
call   1070 <sync@plt>
jmp    1220 <main+0x80>
mov    (%r14),%rdi
call   1080 <usage@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x8(%r14),%rsi
lea    0xd92(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"-s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [18 x i8] c"Error opening %s\0A\00"
@.str.2 = [10 x i8] c"write %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %9
store i32 1, i32* %10
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 4096)
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 3
br i1 %18, label %19, label %25
19: ; preds = %16, %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 @usage(i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
store i32 1, i32* %8
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
br label %34
34: ; preds = %31, %25
%35 = load i8**, i8*** %5
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %35, i64 %37
%39 = load i8*, i8** %38
%40 = load i32, i32* @O_RDWR
%41 = load i32, i32* @O_CREAT
%42 = or i32 %40, %41
%43 = call i32 @open(i8* %39, i32 %42, i32 384)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %52
46: ; preds = %34
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %34
br label %53
53: ; preds = %52, %64
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%55 = load i32, i32* %9
%56 = call i32 @sprintf(i8* %54, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %6
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%59 = call i32 @write(i32 %57, i8* %58, i32 4096)
store i32 %59, i32* %7
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %53
%63 = call i32 (...) @sync()
br label %64
64: ; preds = %62, %53
%65 = call i32 @sleep(i32 1)
br label %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"-s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [18 x i8] c"Error opening %s\0A\00"
@.str.2 = [10 x i8] c"write %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 4096)
%6 = add i32 %0, -4
%7 = icmp ult i32 %6, -2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = call i32 @usage(i8* %9)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i64 2, i64 1
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = call i32 @open(i8* %19, i32 %22, i32 384)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %12
%26 = load i8*, i8** %13
%27 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %12, %34
%30 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 0)
%31 = call i32 @write(i32 %23, i8* %4, i32 4096)
br i1 %16, label %32, label %34
32: ; preds = %29
%33 = call i32 (...) @sync()
br label %34
34: ; preds = %32, %29
%35 = call i32 @sleep(i32 1)
br label %29
}