#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_CUR;
int /*<<< orphan*/ exit(int);
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 main(void) {
  char *fname = "test.txt";
  int fd1, fd2, cnt;
  char buf[30];
  fd1 = open(fname, O_RDONLY);
  fd2 = open(fname, O_RDONLY);
  if (fd1 < 0 || fd2 < 0) {
    perror("open()");
    exit(-1);
  }
  cnt = read(fd1, buf, 12);
  buf[cnt] = '\0';
  printf("fd1's first printf: %s\n", buf);
  lseek(fd1, 1, SEEK_CUR);
  cnt = read(fd1, buf, 12);
  buf[cnt] = '\0';
  printf("fd1's second printf: %s\n", buf);
  cnt = read(fd2, buf, 12);
  buf[cnt] = '\0';
  printf("fd2's first printf: %s\n", buf);
  lseek(fd2, 1, SEEK_CUR);
  cnt = read(fd2, buf, 12);
  buf[cnt] = '\0';
  printf("fd2's second printf: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11b8 <main+0x58>
cmpl   $0x0,-0x18(%rbp)
jge    11ce <main+0x6e>
lea    0xe4a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1040 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xdef(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xddb(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1040 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xdb0(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e6f(%rip),%r14
mov    (%r14),%esi
lea    0xe85(%rip),%rbp
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    (%r14),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
or     %ebx,%eax
js     125f <main+0xff>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe57(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    0x2e20(%rip),%r15
mov    (%r15),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe35(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe2a(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    (%r15),%edx
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe0f(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda3(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [24 x i8] c"fd1's first printf: %s\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [25 x i8] c"fd1's second printf: %s\0A\00"
@.str.4 = [24 x i8] c"fd2's first printf: %s\0A\00"
@.str.5 = [25 x i8] c"fd2's second printf: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [30 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %3
%10 = load i8*, i8** %2
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %0
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15, %0
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %15
%22 = load i32, i32* %3
%23 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 12)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %3
%31 = load i32, i32* @SEEK_CUR
%32 = call i32 @lseek(i32 %30, i32 1, i32 %31)
%33 = load i32, i32* %3
%34 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 12)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%40 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %4
%42 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 12)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 %45
store i8 0, i8* %46
%47 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%48 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %4
%50 = load i32, i32* @SEEK_CUR
%51 = call i32 @lseek(i32 %49, i32 1, i32 %50)
%52 = load i32, i32* %4
%53 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 12)
store i32 %54, i32* %5
%55 = load i32, i32* %5
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 %56
store i8 0, i8* %57
%58 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%59 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %58)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [24 x i8] c"fd1's first printf: %s\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [25 x i8] c"fd1's second printf: %s\0A\00"
@.str.4 = [24 x i8] c"fd2's first printf: %s\0A\00"
@.str.5 = [25 x i8] c"fd2's second printf: %s\0A\00"
define i32 @main() {
%1 = alloca [30 x i8]
%2 = getelementptr inbounds [30 x i8], [30 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* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = or i32 %6, %4
%8 = icmp sgt i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i32 @read(i32 %4, i8* %2, i32 12)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%17 = load i32, i32* @SEEK_CUR
%18 = call i32 @lseek(i32 %4, i32 1, i32 %17)
%19 = call i32 @read(i32 %4, i8* %2, i32 12)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%23 = call i32 @read(i32 %6, i8* %2, i32 12)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%27 = load i32, i32* @SEEK_CUR
%28 = call i32 @lseek(i32 %6, i32 1, i32 %27)
%29 = call i32 @read(i32 %6, i8* %2, i32 12)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %2)
ret i32 0
}