#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_APPEND;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
int fork();
int lseek(int, int, int /*<<< orphan*/);
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  int fd, flags, ret;
  char template[] = "/tmp/testXXXXXX";
  fd = mkstemp(template);
  if (fd == -1) {
    perror("mkstemp");
    exit(1);
  }
  flags = fcntl(fd, F_GETFL);
  if (flags == -1) {
    perror("fcntl - F_GETFL");
    exit(1);
  }
  switch (fork()) {
  case -1:
    perror("fork");
    exit(1);
  case 0:
    if (lseek(fd, 1000, SEEK_SET) == -1) {
      perror("lseek");
      exit(1);
    }
    flags = fcntl(fd, F_GETFL);
    if (flags == -1) {
      perror("fcntl - F_GETFL");
      exit(1);
    }
    flags |= O_APPEND;
    if (fcntl(fd, F_SETFL, flags) == -1) {
      perror("fcntl - F_SETFL");
      exit(1);
    }
    break;
    printf("Child has exited\n");
  default:
    wait(NULL);
    printf("PARENT: File offset in parent: %lld\n",
           (long long)lseek(fd, 0, SEEK_CUR));
    flags = fcntl(fd, F_GETFL);
    printf("O_APPEND flag in parent is: %s\n",
           (flags & O_APPEND) ? "on" : "off");
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <mkstemp@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d1 <main+0x51>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1207 <main+0x87>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     1231 <main+0xb1>
jmp    1221 <main+0xa1>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     1247 <main+0xc7>
jmp    12fb <main+0x17b>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    127c <main+0xfc>
lea    0xdc0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12b2 <main+0x132>
lea    0xd75(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12f6 <main+0x176>
lea    0xd4c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    136d <main+0x1ed>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    -0x8(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
movslq %eax,%rsi
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2c96(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    0x2c70(%rip),%rax
and    (%rax),%ecx
lea    0xd3a(%rip),%rsi
lea    0xd30(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movaps 0xe72(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   1060 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0x116>
mov    %eax,%ebx
mov    0x2e1c(%rip),%r14
mov    (%r14),%esi
mov    %eax,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     128d <main+0x10d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1236 <main+0xb6>
cmp    $0xffffffff,%eax
je     129f <main+0x11f>
xor    %edi,%edi
call   1090 <wait@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
movslq %eax,%rsi
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2da6(%rip),%rcx
and    (%rcx),%eax
lea    0xe70(%rip),%rax
lea    0xe66(%rip),%rsi
cmove  %rax,%rsi
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1283 <main+0x103>
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x3e8,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     12a8 <main+0x128>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     128d <main+0x10d>
mov    0x2d50(%rip),%rcx
or     (%rcx),%eax
mov    0x2d67(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12b1 <main+0x131>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd84(%rip),%rdi
jmp    12b8 <main+0x138>
lea    0xd73(%rip),%rdi
jmp    12b8 <main+0x138>
lea    0xd82(%rip),%rdi
jmp    12b8 <main+0x138>
lea    0xd7e(%rip),%rdi
jmp    12b8 <main+0x138>
lea    0xd7b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.template = [16 x i8] c"/tmp/testXXXXXX\00"
@.str = [8 x i8] c"mkstemp\00"
@F_GETFL = global i32 0
@.str.1 = [16 x i8] c"fcntl - F_GETFL\00"
@.str.2 = [5 x i8] c"fork\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@O_APPEND = global i32 0
@F_SETFL = global i32 0
@.str.4 = [16 x i8] c"fcntl - F_SETFL\00"
@.str.5 = [37 x i8] c"PARENT: File offset in parent: %lld\0A\00"
@SEEK_CUR = global i32 0
@.str.6 = [32 x i8] c"O_APPEND flag in parent is: %s\0A\00"
@.str.7 = [3 x i8] c"on\00"
@.str.8 = [4 x i8] c"off\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
store i32 0, i32* %1
%6 = bitcast [16 x i8]* %5 to i8*
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = call i32 @mkstemp(i8* %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = load i32, i32* @F_GETFL
%17 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = call i32 (...) @fork()
switch i32 %24, label %58 [
i32 -1, label %25
i32 0, label %28
]
25: ; preds = %23
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %23
%29 = load i32, i32* %2
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %29, i32 1000, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = load i32, i32* %2
%38 = load i32, i32* @F_GETFL
%39 = call i32 (i32, i32, ...) @fcntl(i32 %37, i32 %38)
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = load i32, i32* @O_APPEND
%47 = load i32, i32* %3
%48 = or i32 %47, %46
store i32 %48, i32* %3
%49 = load i32, i32* %2
%50 = load i32, i32* @F_SETFL
%51 = load i32, i32* %3
%52 = call i32 (i32, i32, ...) @fcntl(i32 %49, i32 %50, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %45
%55 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %45
br label %75
58: ; preds = %23
%59 = call i32 @wait(i32* null)
%60 = load i32, i32* %2
%61 = load i32, i32* @SEEK_CUR
%62 = call i32 @lseek(i32 %60, i32 0, i32 %61)
%63 = sext i32 %62 to i64
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i64 %63)
%65 = load i32, i32* %2
%66 = load i32, i32* @F_GETFL
%67 = call i32 (i32, i32, ...) @fcntl(i32 %65, i32 %66)
store i32 %67, i32* %3
%68 = load i32, i32* %3
%69 = load i32, i32* @O_APPEND
%70 = and i32 %68, %69
%71 = icmp ne i32 %70, 0
%72 = zext i1 %71 to i64
%73 = select i1 %71, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0)
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %58, %57
ret i32 0
}

LLVM IR (-O2):
@__const.main.template = [16 x i8] c"/tmp/testXXXXXX\00"
@.str = [8 x i8] c"mkstemp\00"
@F_GETFL = global i32 0
@.str.1 = [16 x i8] c"fcntl - F_GETFL\00"
@.str.2 = [5 x i8] c"fork\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@O_APPEND = global i32 0
@F_SETFL = global i32 0
@.str.4 = [16 x i8] c"fcntl - F_SETFL\00"
@.str.5 = [37 x i8] c"PARENT: File offset in parent: %lld\0A\00"
@SEEK_CUR = global i32 0
@.str.6 = [32 x i8] c"O_APPEND flag in parent is: %s\0A\00"
@.str.7 = [3 x i8] c"on\00"
@.str.8 = [4 x i8] c"off\00"
define i32 @main() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = call i32 @mkstemp(i8* %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @F_GETFL
%10 = call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = call i32 (...) @fork()
switch i32 %16, label %43 [
i32 -1, label %17
i32 0, label %20
]
17: ; preds = %15
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %15
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lseek(i32 %3, i32 1000, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = load i32, i32* @F_GETFL
%29 = call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load i32, i32* @O_APPEND
%36 = or i32 %35, %29
%37 = load i32, i32* @F_SETFL
%38 = call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %37, i32 %36)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %56
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %15
%44 = call i32 @wait(i32* null)
%45 = load i32, i32* @SEEK_CUR
%46 = call i32 @lseek(i32 %3, i32 0, i32 %45)
%47 = sext i32 %46 to i64
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i64 %47)
%49 = load i32, i32* @F_GETFL
%50 = call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %49)
%51 = load i32, i32* @O_APPEND
%52 = and i32 %51, %50
%53 = icmp eq i32 %52, 0
%54 = select i1 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %34, %43
ret i32 0
}