#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*/ F_GETFL;
int /*<<< orphan*/ SEEK_CUR;
int STDOUT_FILENO;
int dup(int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd = dup(STDOUT_FILENO);
  if (fd == -1) {
    fprintf(stderr, "dup: %s\n", strerror(errno));
    return 1;
  }
  int stdout_flags = fcntl(STDOUT_FILENO, F_GETFL);
  if (stdout_flags == -1) {
    fprintf(stderr, "fcntl: stdout: %s\n", strerror(errno));
    return 1;
  }
  int fd_flags = fcntl(fd, F_GETFL);
  if (fd_flags == -1) {
    fprintf(stderr, "fcntl: fd: %s\n", strerror(errno));
    return 1;
  }
  if (stdout_flags != fd_flags) {
    fprintf(stderr, "Flags are different\n");
    return 1;
  }
  off_t stdout_off = lseek(STDOUT_FILENO, 0, SEEK_CUR);
  if (stdout_off == -1) {
    fprintf(stderr, "lseek: stdout: %s\n", strerror(errno));
    return 1;
  }
  off_t fd_off = lseek(fd, 0, SEEK_CUR);
  if (fd_off == -1) {
    fprintf(stderr, "lseek: fd: %s\n", strerror(errno));
    return 1;
  }
  if (stdout_off != fd_off) {
    fprintf(stderr, "Offsets are different\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
call   1040 <dup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bb <main+0x6b>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x240>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1219 <main+0xc9>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe03(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x240>
mov    -0x14(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1271 <main+0x121>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x240>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12a0 <main+0x150>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x240>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1300 <main+0x1b0>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xd53(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x240>
mov    -0x14(%rbp),%edi
mov    0x2cd6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    135a <main+0x20a>
mov    0x2ca9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x240>
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     1389 <main+0x239>
mov    0x2c63(%rip),%rax
mov    (%rax),%edi
lea    0xcec(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1390 <main+0x240>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e92(%rip),%r14
mov    (%r14),%edi
call   1040 <dup@plt>
cmp    $0xffffffff,%eax
je     11fb <main+0xab>
mov    %eax,%ebx
mov    (%r14),%edi
mov    0x2e45(%rip),%r15
mov    (%r15),%esi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     121e <main+0xce>
mov    %eax,%ebp
mov    (%r15),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     123e <main+0xee>
cmp    %eax,%ebp
jne    125e <main+0x10e>
mov    (%r14),%edi
mov    0x2e2d(%rip),%rbp
mov    0x0(%rbp),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     1277 <main+0x127>
mov    %eax,%r14d
mov    0x0(%rbp),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     1297 <main+0x147>
cmp    %eax,%r14d
je     12c6 <main+0x176>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
jmp    126e <main+0x11e>
mov    0x2dce(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xde7(%rip),%rsi
jmp    12b5 <main+0x165>
mov    0x2dab(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdcd(%rip),%rsi
jmp    12b5 <main+0x165>
mov    0x2d8b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdc0(%rip),%rsi
jmp    12b5 <main+0x165>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12c1 <main+0x171>
mov    0x2d52(%rip),%rax
mov    (%rax),%ebx
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdab(%rip),%rsi
jmp    12b5 <main+0x165>
mov    0x2d32(%rip),%rax
mov    (%rax),%ebx
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd9e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [9 x i8] c"dup: %s\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@.str.1 = [19 x i8] c"fcntl: stdout: %s\0A\00"
@.str.2 = [15 x i8] c"fcntl: fd: %s\0A\00"
@.str.3 = [21 x i8] c"Flags are different\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [19 x i8] c"lseek: stdout: %s\0A\00"
@.str.5 = [15 x i8] c"lseek: fd: %s\0A\00"
@.str.6 = [23 x i8] c"Offsets are different\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* @STDOUT_FILENO
%12 = call i32 @dup(i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 1, i32* %3
br label %79
20: ; preds = %2
%21 = load i32, i32* @STDOUT_FILENO
%22 = load i32, i32* @F_GETFL
%23 = call i32 @fcntl(i32 %21, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 1, i32* %3
br label %79
31: ; preds = %20
%32 = load i32, i32* %6
%33 = load i32, i32* @F_GETFL
%34 = call i32 @fcntl(i32 %32, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %40)
store i32 1, i32* %3
br label %79
42: ; preds = %31
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %79
49: ; preds = %42
%50 = load i32, i32* @STDOUT_FILENO
%51 = load i32, i32* @SEEK_CUR
%52 = call i32 @lseek(i32 %50, i32 0, i32 %51)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %58)
store i32 1, i32* %3
br label %79
60: ; preds = %49
%61 = load i32, i32* %6
%62 = load i32, i32* @SEEK_CUR
%63 = call i32 @lseek(i32 %61, i32 0, i32 %62)
store i32 %63, i32* %10
%64 = load i32, i32* %10
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %71
66: ; preds = %60
%67 = load i32, i32* @stderr
%68 = load i32, i32* @errno
%69 = call i8* @strerror(i32 %68)
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %69)
store i32 1, i32* %3
br label %79
71: ; preds = %60
%72 = load i32, i32* %9
%73 = load i32, i32* %10
%74 = icmp ne i32 %72, %73
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = load i32, i32* @stderr
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %79
78: ; preds = %71
store i32 0, i32* %3
br label %79
79: ; preds = %78, %75, %66, %55, %46, %37, %26, %15
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [9 x i8] c"dup: %s\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@.str.1 = [19 x i8] c"fcntl: stdout: %s\0A\00"
@.str.2 = [15 x i8] c"fcntl: fd: %s\0A\00"
@.str.3 = [21 x i8] c"Flags are different\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [19 x i8] c"lseek: stdout: %s\0A\00"
@.str.5 = [15 x i8] c"lseek: fd: %s\0A\00"
@.str.6 = [23 x i8] c"Offsets are different\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @STDOUT_FILENO
%4 = tail call i32 @dup(i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %59
11: ; preds = %2
%12 = load i32, i32* @STDOUT_FILENO
%13 = load i32, i32* @F_GETFL
%14 = tail call i32 @fcntl(i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %59
21: ; preds = %11
%22 = load i32, i32* @F_GETFL
%23 = tail call i32 @fcntl(i32 %4, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %59
30: ; preds = %21
%31 = icmp eq i32 %14, %23
br i1 %31, label %35, label %32
32: ; preds = %30
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %59
35: ; preds = %30
%36 = load i32, i32* @STDOUT_FILENO
%37 = load i32, i32* @SEEK_CUR
%38 = tail call i32 @lseek(i32 %36, i32 0, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %59
45: ; preds = %35
%46 = load i32, i32* @SEEK_CUR
%47 = tail call i32 @lseek(i32 %4, i32 0, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = tail call i8* @strerror(i32 %51)
%53 = tail call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %52)
br label %59
54: ; preds = %45
%55 = icmp eq i32 %38, %47
br i1 %55, label %59, label %56
56: ; preds = %54
%57 = load i32, i32* @stderr
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %16, %40, %54, %56, %49, %32, %25, %6
%60 = phi i32 [ 1, %6 ], [ 1, %16 ], [ 1, %25 ], [ 1, %32 ], [ 1, %40 ], [ 1, %49 ], [ 1, %56 ], [ 0, %54 ]
ret i32 %60
}