#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 */
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int close(int);
int dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ info(int, int, int);
int lseek(int, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ puts(char *);
int main(int argc, char **argv) {
  int oldfd, newfd, flags;
  off_t ret, position;
  const char *file;
  if (argc != 2) {
    printf("Exercise 5-5: Verify settings of duplicate file "
           "descriptors\nUsage: %s <file>\n",
           argv[0]);
    exit(EXIT_FAILURE);
  }
  file = argv[1];
  if ((oldfd = open(file, O_RDONLY)) == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  if ((ret = lseek(oldfd, 1, SEEK_SET)) == -1) {
    perror("lseek");
    exit(EXIT_FAILURE);
  }
  printf("Original file descriptor (%s):\n", file);
  position = lseek(oldfd, 0, SEEK_CUR);
  flags = fcntl(oldfd, F_GETFL);
  info(oldfd, position, flags);
  newfd = dup(oldfd);
  flags = fcntl(newfd, F_GETFL);
  puts("Duplication of original file descriptor:");
  info(newfd, position, flags);
  if (close(oldfd) == -1) {
    perror("close");
  }
  if (close(newfd) == -1) {
    perror("close");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
call   1050 <printf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1225 <main+0x85>
lea    0xe3c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1070 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1261 <main+0xc1>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   1040 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   10c0 <info@plt>
mov    -0x14(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
call   1040 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
lea    0xdaa(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   10c0 <info@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
jne    12ff <main+0x15f>
lea    0xda8(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
jne    131c <main+0x17c>
lea    0xd8b(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    129f <main+0xff>
mov    0x8(%rsi),%rbp
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12b0 <main+0x110>
mov    %eax,%ebx
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     12b9 <main+0x119>
lea    0xe64(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,%r14d
mov    0x2daf(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1040 <fcntl@plt>
mov    %ebx,%edi
mov    %r14d,%esi
mov    %eax,%edx
call   10c0 <info@plt>
mov    %ebx,%edi
call   1060 <dup@plt>
mov    %eax,%ebp
mov    (%r15),%esi
mov    %eax,%edi
call   1040 <fcntl@plt>
mov    %eax,%r15d
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %r15d,%edx
call   10c0 <info@plt>
mov    %ebx,%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xe3>
mov    %ebp,%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0xf1>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe18(%rip),%rdi
call   10a0 <perror@plt>
jmp    126a <main+0xca>
lea    0xe0a(%rip),%rdi
call   10a0 <perror@plt>
jmp    1276 <main+0xd6>
mov    (%rsi),%rsi
lea    0xd57(%rip),%rdi
call   1050 <printf@plt>
jmp    12c5 <main+0x125>
lea    0xd97(%rip),%rdi
jmp    12c0 <main+0x120>
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [78 x i8] c"Exercise 5-5: Verify settings of duplicate file descriptors\0AUsage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [32 x i8] c"Original file descriptor (%s):\0A\00"
@SEEK_CUR = global i32 0
@F_GETFL = global i32 0
@.str.4 = [41 x i8] c"Duplication of original file descriptor:\00"
@.str.5 = [6 x i8] c"close\00"
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 i32
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %11
%25 = load i8*, i8** %11
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %21
%34 = load i32, i32* %6
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %34, i32 1, i32 %35)
store i32 %36, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = load i8*, i8** %11
%44 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %6
%46 = load i32, i32* @SEEK_CUR
%47 = call i32 @lseek(i32 %45, i32 0, i32 %46)
store i32 %47, i32* %10
%48 = load i32, i32* %6
%49 = load i32, i32* @F_GETFL
%50 = call i32 @fcntl(i32 %48, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %6
%52 = load i32, i32* %10
%53 = load i32, i32* %8
%54 = call i32 @info(i32 %51, i32 %52, i32 %53)
%55 = load i32, i32* %6
%56 = call i32 @dup(i32 %55)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = load i32, i32* @F_GETFL
%59 = call i32 @fcntl(i32 %57, i32 %58)
store i32 %59, i32* %8
%60 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32, i32* %7
%62 = load i32, i32* %10
%63 = load i32, i32* %8
%64 = call i32 @info(i32 %61, i32 %62, i32 %63)
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %42
%69 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %70
70: ; preds = %68, %42
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %70
%75 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %70
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [78 x i8] c"Exercise 5-5: Verify settings of duplicate file descriptors\0AUsage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [32 x i8] c"Original file descriptor (%s):\0A\00"
@SEEK_CUR = global i32 0
@F_GETFL = global i32 0
@.str.4 = [41 x i8] c"Duplication of original file descriptor:\00"
@.str.5 = [6 x i8] c"close\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %9
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @lseek(i32 %13, i32 1, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%29 = load i32, i32* @SEEK_CUR
%30 = tail call i32 @lseek(i32 %13, i32 0, i32 %29)
%31 = load i32, i32* @F_GETFL
%32 = tail call i32 @fcntl(i32 %13, i32 %31)
%33 = tail call i32 @info(i32 %13, i32 %30, i32 %32)
%34 = tail call i32 @dup(i32 %13)
%35 = load i32, i32* @F_GETFL
%36 = tail call i32 @fcntl(i32 %34, i32 %35)
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%38 = tail call i32 @info(i32 %34, i32 %30, i32 %36)
%39 = tail call i32 @close(i32 %13)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %27
%42 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %41, %27
%44 = tail call i32 @close(i32 %34)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
ret i32 0
}