#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 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);
  }
  for (rc = 0;;) {
    sprintf(buf, "write %d\n", rc);
    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    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
jl     11ee <main+0x4e>
cmpl   $0x3,-0x8(%rbp)
jle    1204 <main+0x64>
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    0xded(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1232 <main+0x92>
movl   $0x1,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2daa(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1285 <main+0xe5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd8d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xd78(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12ca <main+0x12a>
mov    $0x0,%al
call   1070 <sync@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    128c <main+0xec>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%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    1267 <main+0xc7>
mov    0x8(%r14),%rdi
lea    0xe26(%rip),%rsi
call   1060 <strcmp@plt>
mov    %rax,%r15
xor    %eax,%eax
test   %r15,%r15
sete   %al
mov    0x8(%r14,%rax,8),%rdi
mov    0x2dea(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1279 <main+0xd9>
mov    %eax,%r12d
lea    0xdfb(%rip),%r14
mov    %rsp,%rbx
xor    %ebp,%ebp
jmp    123a <main+0x9a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %r15,%r15
jne    1230 <main+0x90>
xor    %eax,%eax
call   1070 <sync@plt>
jmp    1230 <main+0x90>
mov    (%r14),%rdi
call   1080 <usage@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x8(%r14),%rsi
lea    0xd7f(%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 [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 1, i32* %9
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 4096)
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 3
br i1 %17, label %18, label %24
18: ; preds = %15, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @usage(i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
store i32 1, i32* %8
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 1
store i32 %32, i32* %9
br label %33
33: ; preds = %30, %24
%34 = load i8**, i8*** %5
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
%39 = load i32, i32* @O_RDWR
%40 = load i32, i32* @O_CREAT
%41 = or i32 %39, %40
%42 = call i32 @open(i8* %38, i32 %41, i32 384)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %33
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %33
store i32 0, i32* %7
br label %52
52: ; preds = %63, %51
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%54 = load i32, i32* %7
%55 = call i32 @sprintf(i8* %53, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %6
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%58 = call i32 @write(i32 %56, i8* %57, i32 4096)
store i32 %58, i32* %7
%59 = load i32, i32* %8
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %52
%62 = call i32 (...) @sync()
br label %63
63: ; preds = %61, %52
%64 = call i32 @sleep(i32 1)
br label %52
}

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, %35
%30 = phi i32 [ %32, %35 ], [ 0, %12 ]
%31 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = call i32 @write(i32 %23, i8* %4, i32 4096)
br i1 %16, label %33, label %35
33: ; preds = %29
%34 = call i32 (...) @sync()
br label %35
35: ; preds = %33, %29
%36 = call i32 @sleep(i32 1)
br label %29
}