#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_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main() {
  int fd;
  int ret;
  fd = open("1.log", O_RDWR | O_CREAT, 0640);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  lseek(fd, 2, SEEK_SET);
  char buffer[1024] = {0};
  ret = read(fd, buffer, 1024);
  if (ret < 0) {
    printf("read error:\n");
    close(fd);
    return -1;
  }
  printf("ret = %d , read:%s\n", ret, buffer);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
mov    $0x1a0,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ca <main+0x5a>
lea    0xe4d(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <main+0xed>
mov    -0x8(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    $0x2,%esi
call   1040 <lseek@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1236 <main+0xc6>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <main+0xed>
mov    -0xc(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e4e(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5c(%rip),%rdi
mov    $0x1a0,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1239 <main+0xb9>
mov    %eax,%ebx
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <lseek@plt>
mov    %rsp,%rbp
xor    %r14d,%r14d
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     1211 <main+0x91>
lea    0xe0f(%rip),%rdi
mov    %rsp,%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    122a <main+0xaa>
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   10a0 <perror@plt>
jmp    1224 <main+0xa4>

LLVM IR (-O0):
@.str = [6 x i8] c"1.log\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"read error:\0A\00"
@.str.3 = [20 x i8] c"ret = %d , read:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7, i32 416)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
13: ; preds = %0
%14 = load i32, i32* %2
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @lseek(i32 %14, i32 2, i32 %15)
%17 = bitcast [1024 x i8]* %4 to i8*
%18 = load i32, i32* %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 1024)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %1
br label %33
27: ; preds = %13
%28 = load i32, i32* %3
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %28, i8* %29)
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
store i32 0, i32* %1
br label %33
33: ; preds = %27, %23, %11
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"1.log\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.3 = [20 x i8] c"ret = %d , read:%s\0A\00"
@str = [12 x i8] c"read error:\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4, i32 416)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
9: ; preds = %0
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %5, i32 2, i32 %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%13 = call i32 @read(i32 %5, i8* %12, i32 1024)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @close(i32 %5)
br label %21
18: ; preds = %9
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %13, i8* %12)
%20 = call i32 @close(i32 %5)
br label %21
21: ; preds = %18, %15
%22 = phi i32 [ -1, %15 ], [ 0, %18 ]
br label %23
23: ; preds = %21, %7
%24 = phi i32 [ -1, %7 ], [ %22, %21 ]
ret i32 %24
}