#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_SUCCESS;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_APPEND;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ assert(int);
int dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ exitErr(char *);
int fcntl(int, int /*<<< orphan*/, ...);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int main(__attribute__((unused)) int _argc,
         __attribute__((unused)) char **argv) {
  int fd1, fd2, flagsBefore, flagsAfter;
  off_t offsetBefore, offsetAfter, offsetFd2;
  fd1 = open("example.txt", O_RDONLY);
  fd2 = dup(fd1);
  flagsBefore = fcntl(fd1, F_GETFL);
  if (flagsBefore == -1)
    exitErr("fcntl");
  if (fcntl(fd2, F_SETFL, flagsBefore | O_APPEND) == -1)
    exitErr("fcntl");
  flagsAfter = fcntl(fd1, F_GETFL);
  if (flagsAfter == -1)
    exitErr("fcntl");
  assert(flagsBefore != flagsAfter);
  assert(flagsAfter & O_APPEND);
  offsetBefore = lseek(fd1, 0, SEEK_CUR);
  if (offsetBefore == -1)
    exitErr("lseek");
  offsetFd2 = lseek(fd2, 10, SEEK_END);
  if (offsetFd2 == -1)
    exitErr("lseek");
  offsetAfter = lseek(fd1, 0, SEEK_CUR);
  if (offsetAfter == -1)
    exitErr("lseek");
  assert(offsetAfter != offsetBefore);
  assert(offsetAfter == offsetFd2);
  exit(EXIT_SUCCESS);
}


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)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d5 <main+0x65>
lea    0xe3c(%rip),%rdi
call   1040 <exitErr@plt>
mov    -0x18(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    0x2dbd(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1209 <main+0x99>
lea    0xe08(%rip),%rdi
call   1040 <exitErr@plt>
mov    -0x14(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1235 <main+0xc5>
lea    0xddc(%rip),%rdi
call   1040 <exitErr@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x20(%rbp),%edi
mov    0x2d56(%rip),%rax
and    (%rax),%edi
call   1070 <assert@plt>
mov    -0x14(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1285 <main+0x115>
lea    0xd92(%rip),%rdi
call   1040 <exitErr@plt>
mov    -0x18(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1060 <lseek@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12b4 <main+0x144>
lea    0xd63(%rip),%rdi
call   1040 <exitErr@plt>
mov    -0x14(%rbp),%edi
mov    0x2d22(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12e0 <main+0x170>
lea    0xd37(%rip),%rdi
call   1040 <exitErr@plt>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x28(%rbp),%eax
cmp    -0x2c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1050 <dup@plt>
mov    %eax,%r14d
mov    0x2e1f(%rip),%rbx
mov    (%rbx),%esi
mov    %r15d,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11c0 <main+0x50>
lea    0xe51(%rip),%rdi
call   1040 <exitErr@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%r12
mov    (%r12),%edx
or     %ebp,%edx
mov    %r14d,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11f1 <main+0x81>
lea    0xe20(%rip),%rdi
call   1040 <exitErr@plt>
mov    (%rbx),%esi
mov    %r15d,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1210 <main+0xa0>
lea    0xe01(%rip),%rdi
call   1040 <exitErr@plt>
xor    %edi,%edi
cmp    %ebx,%ebp
setne  %dil
call   1070 <assert@plt>
mov    (%r12),%edi
and    %ebx,%edi
call   1070 <assert@plt>
mov    0x2db1(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    1250 <main+0xe0>
lea    0xdc7(%rip),%rdi
call   1040 <exitErr@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    $0xa,%esi
call   1060 <lseek@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1279 <main+0x109>
lea    0xd9e(%rip),%rdi
call   1040 <exitErr@plt>
mov    0x0(%rbp),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1299 <main+0x129>
lea    0xd7e(%rip),%rdi
call   1040 <exitErr@plt>
xor    %edi,%edi
cmp    %r12d,%ebp
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
cmp    %ebx,%ebp
sete   %dil
call   1070 <assert@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"example.txt\00"
@O_RDONLY = global i32 0
@F_GETFL = global i32 0
@.str.1 = [6 x i8] c"fcntl\00"
@F_SETFL = global i32 0
@O_APPEND = global i32 0
@SEEK_CUR = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@SEEK_END = 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 i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = call i32 @dup(i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %6
%18 = load i32, i32* @F_GETFL
%19 = call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %2
%25 = load i32, i32* %7
%26 = load i32, i32* @F_SETFL
%27 = load i32, i32* %8
%28 = load i32, i32* @O_APPEND
%29 = or i32 %27, %28
%30 = call i32 (i32, i32, ...) @fcntl(i32 %25, i32 %26, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %24
%35 = load i32, i32* %6
%36 = load i32, i32* @F_GETFL
%37 = call i32 (i32, i32, ...) @fcntl(i32 %35, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = load i32, i32* %8
%44 = load i32, i32* %9
%45 = icmp ne i32 %43, %44
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = load i32, i32* %9
%49 = load i32, i32* @O_APPEND
%50 = and i32 %48, %49
%51 = call i32 @assert(i32 %50)
%52 = load i32, i32* %6
%53 = load i32, i32* @SEEK_CUR
%54 = call i32 @lseek(i32 %52, i32 0, i32 %53)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %42
%58 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %57, %42
%60 = load i32, i32* %7
%61 = load i32, i32* @SEEK_END
%62 = call i32 @lseek(i32 %60, i32 10, i32 %61)
store i32 %62, i32* %12
%63 = load i32, i32* %12
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65, %59
%68 = load i32, i32* %6
%69 = load i32, i32* @SEEK_CUR
%70 = call i32 @lseek(i32 %68, i32 0, i32 %69)
store i32 %70, i32* %11
%71 = load i32, i32* %11
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %67
%74 = call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %75
75: ; preds = %73, %67
%76 = load i32, i32* %11
%77 = load i32, i32* %10
%78 = icmp ne i32 %76, %77
%79 = zext i1 %78 to i32
%80 = call i32 @assert(i32 %79)
%81 = load i32, i32* %11
%82 = load i32, i32* %12
%83 = icmp eq i32 %81, %82
%84 = zext i1 %83 to i32
%85 = call i32 @assert(i32 %84)
%86 = load i32, i32* @EXIT_SUCCESS
%87 = call i32 @exit(i32 %86)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"example.txt\00"
@O_RDONLY = global i32 0
@F_GETFL = global i32 0
@.str.1 = [6 x i8] c"fcntl\00"
@F_SETFL = global i32 0
@O_APPEND = global i32 0
@SEEK_CUR = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@SEEK_END = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @dup(i32 %4)
%6 = load i32, i32* @F_GETFL
%7 = tail call i32 (i32, i32, ...) @fcntl(i32 %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @O_APPEND
%14 = or i32 %13, %7
%15 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %12, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i32, i32* @F_GETFL
%21 = tail call i32 (i32, i32, ...) @fcntl(i32 %4, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = icmp ne i32 %7, %21
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
%29 = load i32, i32* @O_APPEND
%30 = and i32 %29, %21
%31 = tail call i32 @assert(i32 %30)
%32 = load i32, i32* @SEEK_CUR
%33 = tail call i32 @lseek(i32 %4, i32 0, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %25
%38 = load i32, i32* @SEEK_END
%39 = tail call i32 @lseek(i32 %5, i32 10, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load i32, i32* @SEEK_CUR
%45 = tail call i32 @lseek(i32 %4, i32 0, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = icmp ne i32 %45, %33
%51 = zext i1 %50 to i32
%52 = tail call i32 @assert(i32 %51)
%53 = icmp eq i32 %45, %39
%54 = zext i1 %53 to i32
%55 = tail call i32 @assert(i32 %54)
%56 = load i32, i32* @EXIT_SUCCESS
%57 = tail call i32 @exit(i32 %56)
unreachable
}