#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd1 = open("2.6.text", O_RDONLY);
  int fd2 = open("2.6.text", O_WRONLY);
  int fd3 = open("2.6.text", O_RDONLY);
  char buf[21];
  buf[20] = '\0';
  if (read(fd1, buf, 20) == 20) {
    printf("fd1: %s\n", buf);
  }
  close(fd1);
  strcpy(buf, "testing 123...");
  int len = strlen(buf);
  if (write(fd2, buf, len) != len) {
    perror("2.6.text");
    exit(EXIT_FAILURE);
  }
  close(fd2);
  if (read(fd3, buf, 20) == 20) {
    printf("fd1: %s\n", buf);
  }
  close(fd3);
  return EXIT_SUCCESS;
}


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)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
movb   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
cmp    $0x14,%eax
jne    121c <main+0x8c>
lea    -0x40(%rbp),%rsi
lea    0xdf2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    -0x40(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1040 <write@plt>
cmp    -0x44(%rbp),%eax
je     1272 <main+0xe2>
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
cmp    $0x14,%eax
jne    12a4 <main+0x114>
lea    -0x40(%rbp),%rsi
lea    0xd6a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e47(%rip),%r14
mov    (%r14),%esi
lea    0xe55(%rip),%rbp
mov    %rbp,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
mov    %eax,%r15d
mov    (%r14),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
mov    %eax,%r14d
movb   $0x0,0x14(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x14,%edx
call   1080 <read@plt>
cmp    $0x14,%eax
jne    11ff <main+0x6f>
lea    0xe12(%rip),%rdi
mov    %rsp,%rsi
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xe05(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
jne    127b <main+0xeb>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x14,%edx
call   1080 <read@plt>
cmp    $0x14,%eax
jne    125f <main+0xcf>
lea    0xdb2(%rip),%rdi
mov    %rsp,%rsi
call   1060 <printf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"2.6.text\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [9 x i8] c"fd1: %s\0A\00"
@.str.2 = [15 x i8] c"testing 123...\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
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 [21 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %8
%17 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 20
store i8 0, i8* %17
%18 = load i32, i32* %6
%19 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 20)
%21 = icmp eq i32 %20, 20
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%24 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %2
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%30 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
store i32 %31, i32* %10
%32 = load i32, i32* %7
%33 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%34 = load i32, i32* %10
%35 = call i32 @write(i32 %32, i8* %33, i32 %34)
%36 = load i32, i32* %10
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %42
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %25
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %8
%46 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8* %46, i32 20)
%48 = icmp eq i32 %47, 20
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = getelementptr inbounds [21 x i8], [21 x i8]* %9, i64 0, i64 0
%51 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %42
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @EXIT_SUCCESS
ret i32 %55
}

LLVM IR (-O2):
@.str = [9 x i8] c"2.6.text\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [9 x i8] c"fd1: %s\0A\00"
@.str.2 = [15 x i8] c"testing 123...\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [21 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_WRONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 20
store i8 0, i8* %11
%12 = call i32 @read(i32 %5, i8* %10, i32 20)
%13 = icmp eq i32 %12, 20
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %16
16: ; preds = %14, %2
%17 = call i32 @close(i32 %5)
%18 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @strlen(i8* %10)
%20 = call i32 @write(i32 %7, i8* %10, i32 %19)
%21 = icmp eq i32 %20, %19
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = call i32 @close(i32 %7)
%28 = call i32 @read(i32 %9, i8* %10, i32 20)
%29 = icmp eq i32 %28, 20
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %32
32: ; preds = %30, %26
%33 = call i32 @close(i32 %9)
%34 = load i32, i32* @EXIT_SUCCESS
ret i32 %34
}