#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*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
int errno;
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int const);
int /*<<< orphan*/ strcpy(char *, char *);
int write(int, char *, int const);
int main(int argc, char *argv[]) {
  int ret = 0;
  if (argc < 2)
    err_sys("Usage: write_inter file");
  int fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, 0644);
  if (fd == -1)
    err_sys("open failed");
  int n = 0;
  int const size = 10;
  char buf[size];
  strcpy(buf, "ABCDEFGHI");
  while (n++ < 10) {
    ret = write(fd, buf, size);
    printf("write %d\n", ret);
    if (ret < size) {
      printf("write less than require, errno = %d\n", errno);
      break;
    }
    ret = lseek(fd, 4, SEEK_SET);
    printf("lseek ret %d, errno = %d\n", ret, errno);
  }
  ret = read(fd, buf, size - 1);
  buf[size] = 0;
  printf("read %d, %s\n", ret, buf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b3 <main+0x33>
lea    0xe52(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    0x2de4(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f9 <main+0x79>
lea    0xe24(%rip),%rdi
call   1040 <err_sys@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0xa,-0x20(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rsp,%rdi
add    $0xfffffffffffffff0,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
lea    0xe01(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0xa,%eax
jge    12bf <main+0x13f>
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    $0xa,%edx
call   1050 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0xa,-0x14(%rbp)
jge    1287 <main+0x107>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12bf <main+0x13f>
mov    -0x18(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1070 <lseek@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    0x2d26(%rip),%rax
mov    (%rax),%edx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1228 <main+0xa8>
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    $0x9,%edx
call   1090 <read@plt>
mov    -0x30(%rbp),%rdx
mov    %eax,-0x14(%rbp)
movb   $0x0,0xa(%rdx)
mov    -0x14(%rbp),%esi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11a2 <main+0x22>
lea    0xe63(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2e37(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2df9(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    11df <main+0x5f>
lea    0xe3e(%rip),%rdi
call   1040 <err_sys@plt>
mov    %rsp,%r14
add    $0xfffffffffffffff0,%r14
mov    %r14,%rsp
lea    0xe34(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    $0xa,%r15d
lea    0xe58(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1050 <write@plt>
mov    %eax,%ebx
lea    0xe05(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x9,%ebx
jle    126a <main+0xea>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    $0x4,%esi
call   1070 <lseek@plt>
mov    0x2d7c(%rip),%rcx
mov    (%rcx),%edx
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0xffffffff,%r15d
jne    1210 <main+0x90>
jmp    1281 <main+0x101>
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x9,%edx
call   1090 <read@plt>
movb   $0x0,0xa(%r14)
lea    0xdda(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
call   1080 <close@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [24 x i8] c"Usage: write_inter file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@.str.2 = [10 x i8] c"ABCDEFGHI\00"
@.str.3 = [10 x i8] c"write %d\0A\00"
@.str.4 = [37 x i8] c"write less than require, errno = %d\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.5 = [26 x i8] c"lseek ret %d, errno = %d\0A\00"
@.str.6 = [13 x i8] c"read %d, %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @err_sys(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_APPEND
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %8, i32 %13, i32 420)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %6
%19 = alloca [10 x i8]
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %19, i64 0, i64 0
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %18, %30
%23 = phi i32 [ 1, %18 ], [ %35, %30 ]
%24 = call i32 @write(i32 %14, i8* %20, i32 10)
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = icmp slt i32 %24, 10
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %28)
br label %37
30: ; preds = %22
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @lseek(i32 %14, i32 4, i32 %31)
%33 = load i32, i32* @errno
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %32, i32 %33)
%35 = add nuw nsw i32 %23, 1
%36 = icmp eq i32 %35, 11
br i1 %36, label %37, label %22
37: ; preds = %30, %27
%38 = call i32 @read(i32 %14, i8* %20, i32 9)
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %19, i64 0, i64 10
store i8 0, i8* %39
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %38, i8* %20)
%41 = call i32 @close(i32 %14)
ret i32 0
}