#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 EXIT_SUCCESS;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SEEK_END;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd = -1;
  int i = 0;
  fd = open("/tmp/exlog.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
  if (fd != -1) {
    for (i = 0; i < 100000; i++) {
      lseek(fd, 0, SEEK_END);
      write(fd, "AAAA", 4);
    }
  } else
    perror("Open error:");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    0x2e59(%rip),%rax
or     (%rax),%edx
lea    0xe78(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11e8 <main+0xa8>
movl   $0x0,-0xc(%rbp)
cmpl   $0x186a0,-0xc(%rbp)
jge    11e3 <main+0xa3>
mov    -0x8(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    0xe44(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a1 <main+0x61>
jmp    11f4 <main+0xb4>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2eaa(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e68(%rip),%rax
mov    0x2e71(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe8e(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11d4 <main+0x94>
mov    %eax,%ebx
mov    $0x186a0,%ebp
mov    0x2e56(%rip),%r15
lea    0xe7e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebp
jne    11a0 <main+0x60>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
jmp    11c0 <main+0x80>

LLVM IR (-O0):
@.str = [15 x i8] c"/tmp/exlog.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SEEK_END = global i32 0
@.str.1 = [5 x i8] c"AAAA\00"
@.str.2 = [12 x i8] c"Open error:\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %27
13: ; preds = %0
store i32 0, i32* %3
br label %14
14: ; preds = %23, %13
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 100000
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i32, i32* %2
%19 = load i32, i32* @SEEK_END
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
%21 = load i32, i32* %2
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
br label %23
23: ; preds = %17
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %14
26: ; preds = %14
br label %29
27: ; preds = %0
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %26
%30 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}

LLVM IR (-O2):
@.str = [15 x i8] c"/tmp/exlog.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SEEK_END = global i32 0
@.str.1 = [5 x i8] c"AAAA\00"
@.str.2 = [12 x i8] c"Open error:\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_WRONLY
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %16, label %9
9: ; preds = %0, %9
%10 = phi i32 [ %14, %9 ], [ 0, %0 ]
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @lseek(i32 %7, i32 0, i32 %11)
%13 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%14 = add nuw nsw i32 %10, 1
%15 = icmp eq i32 %14, 100000
br i1 %15, label %18, label %9
16: ; preds = %0
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %9, %16
%19 = load i32, i32* @EXIT_SUCCESS
ret i32 %19
}