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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
char *calloc(int, size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int write(int, char *, size_t);
int main(int argc, char **argv) {
  char *file = NULL;
  int fd = -1;
  char *buffer = NULL;
  size_t buf_size = 0;
  size_t written = 0;
  int ret = 0;
  off_t offset = 0;
  int i = 0;
  int loop_count = 5;
  if (argc < 2) {
    printf("Usage:%s <filename>\n", argv[0]);
    return -1;
  }
  file = argv[1];
  buf_size = 1024;
  buffer = calloc(1, buf_size);
  if (!buffer) {
    perror("calloc");
    return -1;
  }
  memset(buffer, 'R', buf_size);
  fd = open(file, O_WRONLY);
  if (fd == -1) {
    perror("open");
    return -1;
  }
  for (i = 0; i < loop_count; i++) {
    ret = write(fd, buffer, buf_size);
    if (ret == -1) {
      perror("write");
      return ret;
    } else {
      written += ret;
    }
    offset = lseek(fd, 0, SEEK_SET);
  }
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x5,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1202 <main+0x82>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <main+0x19b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
movq   $0x400,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    124b <main+0xcb>
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <main+0x19b>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x52,%esi
call   1070 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1294 <main+0x114>
lea    0xd99(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <main+0x19b>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    130b <main+0x18b>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12db <main+0x15b>
lea    0xd56(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    131b <main+0x19b>
movslq -0x3c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    129b <main+0x11b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11a0 <main+0x20>
mov    (%rsi),%rsi
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
jmp    12c2 <main+0x142>
mov    0x8(%rsi),%rbx
mov    $0x400,%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
test   %rax,%rax
je     12ad <main+0x12d>
mov    %rax,%r14
mov    $0x400,%edx
mov    %rax,%rdi
mov    $0x52,%esi
call   1070 <memset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12b6 <main+0x136>
mov    %eax,%ebp
mov    $0x400,%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x124>
mov    0x2dd4(%rip),%r15
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    $0x400,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x124>
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    $0x400,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x124>
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    $0x400,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x124>
mov    (%r15),%edx
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    $0x400,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x124>
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12c7 <main+0x147>
lea    0xd76(%rip),%rdi
jmp    12bd <main+0x13d>
lea    0xd61(%rip),%rdi
jmp    12bd <main+0x13d>
lea    0xd5f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Usage:%s <filename>\0A\00"
@.str.1 = [7 x i8] c"calloc\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i32 -1, i32* %7
store i8* null, i8** %8
store i64 0, i64* %9
store i64 0, i64* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 5, i32* %14
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %73
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %6
store i64 1024, i64* %9
%26 = load i64, i64* %9
%27 = call i8* @calloc(i32 1, i64 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
32: ; preds = %22
%33 = load i8*, i8** %8
%34 = load i64, i64* %9
%35 = call i32 @memset(i8* %33, i8 signext 82, i64 %34)
%36 = load i8*, i8** %6
%37 = load i32, i32* @O_WRONLY
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = 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 %73
43: ; preds = %32
store i32 0, i32* %13
br label %44
44: ; preds = %67, %43
%45 = load i32, i32* %13
%46 = load i32, i32* %14
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %70
48: ; preds = %44
%49 = load i32, i32* %7
%50 = load i8*, i8** %8
%51 = load i64, i64* %9
%52 = call i32 @write(i32 %49, i8* %50, i64 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* %11
store i32 %57, i32* %3
br label %73
58: ; preds = %48
%59 = load i32, i32* %11
%60 = sext i32 %59 to i64
%61 = load i64, i64* %10
%62 = add i64 %61, %60
store i64 %62, i64* %10
br label %63
63: ; preds = %58
%64 = load i32, i32* %7
%65 = load i32, i32* @SEEK_SET
%66 = call i32 @lseek(i32 %64, i32 0, i32 %65)
store i32 %66, i32* %12
br label %67
67: ; preds = %63
%68 = load i32, i32* %13
%69 = add nsw i32 %68, 1
store i32 %69, i32* %13
br label %44
70: ; preds = %44
%71 = load i8*, i8** %8
%72 = call i32 @free(i8* %71)
store i32 0, i32* %3
br label %73
73: ; preds = %70, %55, %41, %30, %17
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [21 x i8] c"Usage:%s <filename>\0A\00"
@.str.1 = [7 x i8] c"calloc\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %50
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i8* @calloc(i32 1, i64 1024)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %50
14: ; preds = %7
%15 = tail call i32 @memset(i8* %10, i8 signext 82, i64 1024)
%16 = load i32, i32* @O_WRONLY
%17 = tail call i32 @open(i8* %9, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @write(i32 %17, i8* %10, i64 1024)
%21 = icmp eq i32 %20, -1
br i1 %21, label %24, label %26
22: ; preds = %14
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %50
24: ; preds = %41, %36, %31, %26, %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %50
26: ; preds = %19
%27 = load i32, i32* @SEEK_SET
%28 = tail call i32 @lseek(i32 %17, i32 0, i32 %27)
%29 = tail call i32 @write(i32 %17, i8* %10, i64 1024)
%30 = icmp eq i32 %29, -1
br i1 %30, label %24, label %31
31: ; preds = %26
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @lseek(i32 %17, i32 0, i32 %32)
%34 = tail call i32 @write(i32 %17, i8* %10, i64 1024)
%35 = icmp eq i32 %34, -1
br i1 %35, label %24, label %36
36: ; preds = %31
%37 = load i32, i32* @SEEK_SET
%38 = tail call i32 @lseek(i32 %17, i32 0, i32 %37)
%39 = tail call i32 @write(i32 %17, i8* %10, i64 1024)
%40 = icmp eq i32 %39, -1
br i1 %40, label %24, label %41
41: ; preds = %36
%42 = load i32, i32* @SEEK_SET
%43 = tail call i32 @lseek(i32 %17, i32 0, i32 %42)
%44 = tail call i32 @write(i32 %17, i8* %10, i64 1024)
%45 = icmp eq i32 %44, -1
br i1 %45, label %24, label %46
46: ; preds = %41
%47 = load i32, i32* @SEEK_SET
%48 = tail call i32 @lseek(i32 %17, i32 0, i32 %47)
%49 = tail call i32 @free(i8* %10)
br label %50
50: ; preds = %46, %24, %22, %12, %4
%51 = phi i32 [ -1, %4 ], [ -1, %22 ], [ -1, %24 ], [ 0, %46 ], [ -1, %12 ]
ret i32 %51
}