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

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int revl = 0;
  int i = 0;
  int fd = 0;
  char filename[64];
  char buffer[64];
  if (argc > 1) {
    strcpy(filename, argv[1]);
  } else {
    strcpy(filename, "/tmp/test.tmp");
  }
  printf("file: %s\n", filename);
  fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0666);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  for (i = 0; i < 60; i++) {
    revl = sprintf(buffer, "content: %d\n", i);
    revl = write(fd, buffer, revl);
    if (revl < 0) {
      perror("write");
      break;
    }
    sleep(1);
  }
  close(fd);
  return revl;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ce <main+0x4e>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
jmp    11de <main+0x5e>
lea    -0x60(%rbp),%rdi
lea    0xe27(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x60(%rbp),%rsi
lea    0xe25(%rip),%rdi
call   1050 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    123c <main+0xbc>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bc <main+0x13c>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3c,-0x18(%rbp)
jge    12ae <main+0x12e>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xdbf(%rip),%rsi
call   1090 <sprintf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1296 <main+0x116>
lea    0xd9e(%rip),%rdi
call   1080 <perror@plt>
jmp    12ae <main+0x12e>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1243 <main+0xc3>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
cmp    $0x2,%edi
jl     119a <main+0x1a>
mov    0x8(%rsi),%rsi
jmp    11a1 <main+0x21>
lea    0xe5f(%rip),%rsi
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe59(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x2e34(%rip),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
test   %eax,%eax
js     124c <main+0xcc>
mov    %eax,%r15d
xor    %ebp,%ebp
lea    0xe28(%rip),%r14
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1090 <sprintf@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebx
test   %eax,%eax
js     125f <main+0xdf>
mov    $0x1,%edi
call   10a0 <sleep@plt>
add    $0x1,%ebp
cmp    $0x3c,%ebp
jne    1200 <main+0x80>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    123a <main+0xba>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
jmp    1232 <main+0xb2>

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/test.tmp\00"
@.str.1 = [10 x i8] c"file: %s\0A\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [13 x i8] c"content: %d\0A\00"
@.str.4 = [6 x i8] c"write\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 [64 x i8]
%10 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strcpy(i8* %14, i8* %17)
br label %22
19: ; preds = %2
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %13
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%24 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_APPEND
%28 = or i32 %26, %27
%29 = load i32, i32* @O_RDWR
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %25, i32 %30, i32 438)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %61
36: ; preds = %22
store i32 0, i32* %7
br label %37
37: ; preds = %54, %36
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 60
br i1 %39, label %40, label %57
40: ; preds = %37
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%42 = load i32, i32* %7
%43 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %42)
store i32 %43, i32* %6
%44 = load i32, i32* %8
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%46 = load i32, i32* %6
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %57
52: ; preds = %40
%53 = call i32 @sleep(i32 1)
br label %54
54: ; preds = %52
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %37
57: ; preds = %50, %37
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %6
store i32 %60, i32* %3
br label %61
61: ; preds = %57, %34
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/test.tmp\00"
@.str.1 = [10 x i8] c"file: %s\0A\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [13 x i8] c"content: %d\0A\00"
@.str.4 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
br label %11
11: ; preds = %2, %8
%12 = phi i8* [ %10, %8 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %2 ]
%13 = call i32 @strcpy(i8* %5, i8* %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_APPEND
%17 = or i32 %16, %15
%18 = load i32, i32* @O_RDWR
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %5, i32 %19, i32 438)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %37
24: ; preds = %11, %31
%25 = phi i32 [ %33, %31 ], [ 0, %11 ]
%26 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 @write(i32 %20, i8* %6, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %35
31: ; preds = %24
%32 = call i32 @sleep(i32 1)
%33 = add nuw nsw i32 %25, 1
%34 = icmp eq i32 %33, 60
br i1 %34, label %35, label %24
35: ; preds = %31, %29
%36 = call i32 @close(i32 %20)
br label %37
37: ; preds = %35, %22
%38 = phi i32 [ 1, %22 ], [ %27, %35 ]
ret i32 %38
}