#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*/ abort_me();
int /*<<< orphan*/ check(int, int);
int /*<<< orphan*/ ckpt_and_exit();
int /*<<< orphan*/ close(int);
int dup(int);
int dup2(int, int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int a, b, c, x, d;
  if (argc != 2) {
    usage(argv[0]);
  }
  x = open(argv[1], O_RDONLY, 0);
  a = open(argv[1], O_RDONLY, 0);
  b = open(argv[1], O_RDONLY, 0);
  c = dup(b);
  close(a);
  a = dup(b);
  close(b);
  close(x);
  d = dup2(c, 15);
  if (a == c) {
    printf("Error: a and c are the same: %d\n", a);
    abort_me();
    exit(1);
  }
  if (d != 15) {
    printf("Error: d's not 15, it's %d\n", d);
    abort_me();
    exit(1);
  }
  printf("OK\n");
  check(a, c);
  check(c, d);
  ckpt_and_exit();
  check(a, c);
  check(c, d);
  ckpt_and_exit();
  check(a, c);
  check(c, d);
  printf("Normal End of Job\n");
  exit(0);
}


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     11cc <main+0x2c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
mov    $0xf,%esi
call   1040 <dup2@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1289 <main+0xe9>
mov    -0x14(%rbp),%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort_me@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0xf,-0x24(%rbp)
je     12b5 <main+0x115>
mov    -0x24(%rbp),%esi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort_me@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   10a0 <check@plt>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10a0 <check@plt>
mov    $0x0,%al
call   10b0 <ckpt_and_exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   10a0 <check@plt>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10a0 <check@plt>
mov    $0x0,%al
call   10b0 <ckpt_and_exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   10a0 <check@plt>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10a0 <check@plt>
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11c7 <main+0x17>
mov    (%rbx),%rdi
call   1090 <usage@plt>
mov    0x8(%rbx),%rdi
mov    0x2e16(%rip),%r15
mov    (%r15),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,%r14d
mov    0x8(%rbx),%rdi
mov    (%r15),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
mov    (%r15),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <dup@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%edi
call   1070 <dup@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %r15d,%edi
mov    $0xf,%esi
call   1050 <dup2@plt>
cmp    %r15d,%ebp
jne    1249 <main+0x99>
lea    0xdbf(%rip),%rdi
mov    %r15d,%esi
jmp    12c9 <main+0x119>
cmp    $0xf,%eax
jne    12c0 <main+0x110>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   10b0 <check@plt>
mov    %r15d,%edi
mov    $0xf,%esi
call   10b0 <check@plt>
xor    %eax,%eax
call   10c0 <ckpt_and_exit@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   10b0 <check@plt>
mov    %r15d,%edi
mov    $0xf,%esi
call   10b0 <check@plt>
xor    %eax,%eax
call   10c0 <ckpt_and_exit@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   10b0 <check@plt>
mov    %r15d,%edi
mov    $0xf,%esi
call   10b0 <check@plt>
lea    0xd8c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <abort_me@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [33 x i8] c"Error: a and c are the same: %d\0A\00"
@.str.1 = [28 x i8] c"Error: d's not 15, it's %d\0A\00"
@.str.2 = [4 x i8] c"OK\0A\00"
@.str.3 = [19 x i8] c"Normal End of Job\0A\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @usage(i8* %16)
br label %18
18: ; preds = %13, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22, i32 0)
store i32 %23, i32* %9
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %26, i32 %27, i32 0)
store i32 %28, i32* %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32, i32 0)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = call i32 @dup(i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %7
%39 = call i32 @dup(i32 %38)
store i32 %39, i32* %6
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %9
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %8
%45 = call i32 @dup2(i32 %44, i32 15)
store i32 %45, i32* %10
%46 = load i32, i32* %6
%47 = load i32, i32* %8
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %54
49: ; preds = %18
%50 = load i32, i32* %6
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %50)
%52 = call i32 (...) @abort_me()
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %18
%55 = load i32, i32* %10
%56 = icmp ne i32 %55, 15
br i1 %56, label %57, label %62
57: ; preds = %54
%58 = load i32, i32* %10
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = call i32 (...) @abort_me()
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %54
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%64 = load i32, i32* %6
%65 = load i32, i32* %8
%66 = call i32 @check(i32 %64, i32 %65)
%67 = load i32, i32* %8
%68 = load i32, i32* %10
%69 = call i32 @check(i32 %67, i32 %68)
%70 = call i32 (...) @ckpt_and_exit()
%71 = load i32, i32* %6
%72 = load i32, i32* %8
%73 = call i32 @check(i32 %71, i32 %72)
%74 = load i32, i32* %8
%75 = load i32, i32* %10
%76 = call i32 @check(i32 %74, i32 %75)
%77 = call i32 (...) @ckpt_and_exit()
%78 = load i32, i32* %6
%79 = load i32, i32* %8
%80 = call i32 @check(i32 %78, i32 %79)
%81 = load i32, i32* %8
%82 = load i32, i32* %10
%83 = call i32 @check(i32 %81, i32 %82)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%85 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [33 x i8] c"Error: a and c are the same: %d\0A\00"
@.str.1 = [28 x i8] c"Error: d's not 15, it's %d\0A\00"
@str = [3 x i8] c"OK\00"
@str.4 = [18 x i8] c"Normal End of Job\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10, i32 0)
%12 = load i8*, i8** %8
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13, i32 0)
%15 = load i8*, i8** %8
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* %15, i32 %16, i32 0)
%18 = tail call i32 @dup(i32 %17)
%19 = tail call i32 @close(i32 %14)
%20 = tail call i32 @dup(i32 %17)
%21 = tail call i32 @close(i32 %17)
%22 = tail call i32 @close(i32 %11)
%23 = tail call i32 @dup2(i32 %18, i32 15)
%24 = icmp eq i32 %20, %18
br i1 %24, label %25, label %29
25: ; preds = %7
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %18)
%27 = tail call i32 (...) @abort_me()
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %7
%30 = icmp eq i32 %23, 15
br i1 %30, label %35, label %31
31: ; preds = %29
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%33 = tail call i32 (...) @abort_me()
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%37 = tail call i32 @check(i32 %20, i32 %18)
%38 = tail call i32 @check(i32 %18, i32 15)
%39 = tail call i32 (...) @ckpt_and_exit()
%40 = tail call i32 @check(i32 %20, i32 %18)
%41 = tail call i32 @check(i32 %18, i32 15)
%42 = tail call i32 (...) @ckpt_and_exit()
%43 = tail call i32 @check(i32 %20, i32 %18)
%44 = tail call i32 @check(i32 %18, i32 15)
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
%46 = tail call i32 @exit(i32 0)
unreachable
}