#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd1, fd2, fd3;
  const char *file;
  char path[256];
  if (argc != 2) {
    printf("Exercise 5-6: Usage: %s <file>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  file = argv[1];
  sprintf(path, "cat %s", file);
  fd1 = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  printf("File descriptor %d created from %s\n", fd1, file);
  fd2 = dup(fd1);
  printf("File descriptor %d duplicated as %d\n", fd1, fd2);
  fd3 = open(file, O_RDWR);
  printf("File descriptor %d created from %s\n", fd3, file);
  write(fd1, "Hello,", 6);
  printf("Wrote 6 bytes to file descriptor %d:\n", fd1);
  system(path);
  puts("");
  write(fd2, "world", 6);
  printf("\nWrote 6 bytes to file descriptor %d:\n", fd2);
  system(path);
  puts("");
  lseek(fd2, 0, SEEK_SET);
  printf("\nSeek to position 0 of %d\n", fd2);
  write(fd1, "HELLO,", 6);
  printf("Wrote 6 bytes to file descriptor %d:\n", fd1);
  system(path);
  puts("");
  write(fd3, "Gidday", 6);
  printf("\nWrote 6 bytes to file descriptor %d:\n", fd3);
  system(path);
  puts("");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11cf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe33(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    0x2daf(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xdd9(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%esi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x130(%rbp),%rdi
call   1050 <system@plt>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%edi
lea    0xda3(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    -0x18(%rbp),%esi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x130(%rbp),%rdi
call   1050 <system@plt>
lea    0xda1(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%edi
mov    0x2caf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <lseek@plt>
mov    -0x18(%rbp),%esi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xd8a(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%esi
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x130(%rbp),%rdi
call   1050 <system@plt>
lea    0xd40(%rip),%rdi
call   1030 <puts@plt>
mov    -0x1c(%rbp),%edi
lea    0xd54(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%esi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x130(%rbp),%rdi
call   1050 <system@plt>
lea    0xd03(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
cmp    $0x2,%edi
jne    135c <main+0x1cc>
mov    0x8(%rsi),%rbx
lea    0xe6b(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   10a0 <sprintf@plt>
mov    0x2e0e(%rip),%r15
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
or     (%r15),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
mov    0x2dca(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
lea    0xe23(%rip),%r13
mov    %r13,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <dup@plt>
mov    %eax,%r12d
lea    0xe27(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r15d
mov    %r13,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe1b(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1040 <write@plt>
lea    0xe16(%rip),%r13
mov    %r13,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <system@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xdea(%rip),%rsi
mov    %r12d,%edi
mov    $0x6,%edx
call   1040 <write@plt>
lea    0xddc(%rip),%rbx
mov    %rbx,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <system@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1080 <lseek@plt>
lea    0xdca(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdd4(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1040 <write@plt>
mov    %r13,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <system@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xdaa(%rip),%rsi
mov    %r15d,%edi
mov    $0x6,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <system@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xc9a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c44(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Exercise 5-6: Usage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"cat %s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [36 x i8] c"File descriptor %d created from %s\0A\00"
@.str.3 = [37 x i8] c"File descriptor %d duplicated as %d\0A\00"
@.str.4 = [7 x i8] c"Hello,\00"
@.str.5 = [38 x i8] c"Wrote 6 bytes to file descriptor %d:\0A\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [6 x i8] c"world\00"
@.str.8 = [39 x i8] c"\0AWrote 6 bytes to file descriptor %d:\0A\00"
@SEEK_SET = global i32 0
@.str.9 = [27 x i8] c"\0ASeek to position 0 of %d\0A\00"
@.str.10 = [7 x i8] c"HELLO,\00"
@.str.11 = [7 x i8] c"Gidday\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [256 x i8]
store i32 %0, i32* %3
store i8** %1, i8*** %4
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %4
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %8
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%24 = load i8*, i8** %8
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %8
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %32, %33
%35 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = load i8*, i8** %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %36, i8* %37)
%39 = load i32, i32* %5
%40 = call i32 @dup(i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %5
%42 = load i32, i32* %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %42)
%44 = load i8*, i8** %8
%45 = load i32, i32* @O_RDWR
%46 = call i32 (i8*, i32, ...) @open(i8* %44, i32 %45)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = load i8*, i8** %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %47, i8* %48)
%50 = load i32, i32* %5
%51 = call i32 @write(i32 %50, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%52 = load i32, i32* %5
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%55 = call i32 @system(i8* %54)
%56 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%57 = load i32, i32* %6
%58 = call i32 @write(i32 %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 6)
%59 = load i32, i32* %6
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %59)
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%62 = call i32 @system(i8* %61)
%63 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%64 = load i32, i32* %6
%65 = load i32, i32* @SEEK_SET
%66 = call i32 @lseek(i32 %64, i32 0, i32 %65)
%67 = load i32, i32* %6
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %5
%70 = call i32 @write(i32 %69, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i32 6)
%71 = load i32, i32* %5
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %71)
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%74 = call i32 @system(i8* %73)
%75 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%76 = load i32, i32* %7
%77 = call i32 @write(i32 %76, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i32 6)
%78 = load i32, i32* %7
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %78)
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%81 = call i32 @system(i8* %80)
%82 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"Exercise 5-6: Usage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"cat %s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [36 x i8] c"File descriptor %d created from %s\0A\00"
@.str.3 = [37 x i8] c"File descriptor %d duplicated as %d\0A\00"
@.str.4 = [7 x i8] c"Hello,\00"
@.str.5 = [38 x i8] c"Wrote 6 bytes to file descriptor %d:\0A\00"
@.str.7 = [6 x i8] c"world\00"
@.str.8 = [39 x i8] c"\0AWrote 6 bytes to file descriptor %d:\0A\00"
@SEEK_SET = global i32 0
@.str.9 = [27 x i8] c"\0ASeek to position 0 of %d\0A\00"
@.str.10 = [7 x i8] c"HELLO,\00"
@.str.11 = [7 x i8] c"Gidday\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %19, i32 %22)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %23, i8* %13)
%25 = tail call i32 @dup(i32 %23)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %25)
%27 = load i32, i32* @O_RDWR
%28 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %27)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %28, i8* %13)
%30 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%32 = call i32 @system(i8* %4)
%33 = tail call i32 @putchar(i32 10)
%34 = tail call i32 @write(i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 6)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %25)
%36 = call i32 @system(i8* %4)
%37 = tail call i32 @putchar(i32 10)
%38 = load i32, i32* @SEEK_SET
%39 = tail call i32 @lseek(i32 %25, i32 0, i32 %38)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %25)
%41 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0), i32 6)
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%43 = call i32 @system(i8* %4)
%44 = tail call i32 @putchar(i32 10)
%45 = tail call i32 @write(i32 %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i32 6)
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %28)
%47 = call i32 @system(i8* %4)
%48 = tail call i32 @putchar(i32 10)
ret i32 0
}