#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int fd;
  char buf[2];
  int n;
  fd = open("test.txt", O_RDONLY);
  lseek(fd, 1, SEEK_SET);
  if ((n = read(fd, buf, 1)) != 1) {
    perror("read error");
  } else
    printf("read:%s\n", buf);
  if ((n = write(fd, buf, 1)) != 1) {
    perror("write error");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x1,%eax
je     11cb <main+0x6b>
lea    0xe48(%rip),%rdi
call   1080 <perror@plt>
jmp    11db <main+0x7b>
lea    -0xa(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x1,%eax
je     1204 <main+0xa4>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <lseek@plt>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    11d6 <main+0x76>
lea    0xe66(%rip),%rdi
lea    0xe(%rsp),%rsi
call   1040 <printf@plt>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11e4 <main+0x84>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
jmp    11b8 <main+0x58>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
jmp    11ce <main+0x6e>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [11 x i8] c"read error\00"
@.str.2 = [9 x i8] c"read:%s\0A\00"
@.str.3 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = load i32, i32* @SEEK_SET
%9 = call i32 @lseek(i32 %7, i32 1, i32 %8)
%10 = load i32, i32* %2
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 1)
store i32 %12, i32* %4
%13 = icmp ne i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %19
16: ; preds = %0
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %14
%20 = load i32, i32* %2
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%22 = call i32 @write(i32 %20, i8* %21, i32 1)
store i32 %22, i32* %4
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [11 x i8] c"read error\00"
@.str.2 = [9 x i8] c"read:%s\0A\00"
@.str.3 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @lseek(i32 %4, i32 1, i32 %5)
%7 = call i32 @read(i32 %4, i8* %2, i32 1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %13
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %13
13: ; preds = %11, %9
%14 = call i32 @write(i32 %4, i8* %2, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
ret i32 0
}