#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*/ execlp(char *, char *, int /*<<< orphan*/ *);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
main() {
  int flags, child;
  if ((flags = fcntl(0, F_GETFL)) < 0) {
    perror("fcntl 1st GETFL");
  }
  printf("flags = %x\n", flags);
  switch (child = fork()) {
  case -1:
    printf("error during fork\n");
    break;
  case 0:
    execlp("test_create", "test_create", NULL);
    break;
  default:
    wait(NULL);
    break;
  }
  while (1) {
    if ((flags = fcntl(0, F_GETFL)) < 0) {
      perror("fcntl parent GETFL");
    }
    printf("parent %d flags = %x\n", child, flags);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a7 <main+0x37>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%esi
lea    0xe9b(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x11(%rbp)
call   1040 <printf@plt>
mov    -0x11(%rbp),%al
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11e9 <main+0x79>
jmp    11d9 <main+0x69>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11fc <main+0x8c>
jmp    1214 <main+0xa4>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121d <main+0xad>
lea    0xe20(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1060 <execlp@plt>
jmp    121d <main+0xad>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
jmp    1222 <main+0xb2>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    124a <main+0xda>
lea    0xdea(%rip),%rdi
call   1050 <perror@plt>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1222 <main+0xb2>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e51(%rip),%r12
mov    (%r12),%esi
xor    %edi,%edi
call   1040 <fcntl@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1233 <main+0xb3>
lea    0xe8e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11d4 <main+0x54>
cmp    $0xffffffff,%ebx
jne    11e7 <main+0x67>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
jmp    11ee <main+0x6e>
lea    0xe35(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1070 <execlp@plt>
jmp    11ee <main+0x6e>
xor    %edi,%edi
call   1090 <wait@plt>
lea    0xe27(%rip),%r14
lea    0xe33(%rip),%r15
jmp    1218 <main+0x98>
xchg   %ax,%ax
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    (%r12),%esi
xor    %edi,%edi
call   1040 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1200 <main+0x80>
mov    %r14,%rdi
call   1060 <perror@plt>
jmp    1200 <main+0x80>
lea    0xdc6(%rip),%rdi
call   1060 <perror@plt>
jmp    11a4 <main+0x24>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [16 x i8] c"fcntl 1st GETFL\00"
@.str.1 = [12 x i8] c"flags = %x\0A\00"
@.str.2 = [19 x i8] c"error during fork\0A\00"
@.str.3 = [12 x i8] c"test_create\00"
@.str.4 = [19 x i8] c"fcntl parent GETFL\00"
@.str.5 = [22 x i8] c"parent %d flags = %x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @F_GETFL
%5 = call i32 @fcntl(i32 0, i32 %4)
store i32 %5, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %3
switch i32 %12, label %17 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %9
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %19
15: ; preds = %9
%16 = call i32 @execlp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %19
17: ; preds = %9
%18 = call i32 @wait(i32* null)
br label %19
19: ; preds = %17, %15, %13
br label %20
20: ; preds = %19, %26
%21 = load i32, i32* @F_GETFL
%22 = call i32 @fcntl(i32 0, i32 %21)
store i32 %22, i32* %2
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = load i32, i32* %3
%28 = load i32, i32* %2
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %27, i32 %28)
%30 = call i32 @sleep(i32 1)
br label %20
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [16 x i8] c"fcntl 1st GETFL\00"
@.str.1 = [12 x i8] c"flags = %x\0A\00"
@.str.3 = [12 x i8] c"test_create\00"
@.str.4 = [19 x i8] c"fcntl parent GETFL\00"
@.str.5 = [22 x i8] c"parent %d flags = %x\0A\00"
@str = [18 x i8] c"error during fork\00"
define i32 @main() {
%1 = load i32, i32* @F_GETFL
%2 = tail call i32 @fcntl(i32 0, i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%8 = tail call i32 (...) @fork()
switch i32 %8, label %13 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %15
11: ; preds = %6
%12 = tail call i32 @execlp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %15
13: ; preds = %6
%14 = tail call i32 @wait(i32* null)
br label %15
15: ; preds = %13, %11, %9
br label %16
16: ; preds = %15, %22
%17 = load i32, i32* @F_GETFL
%18 = tail call i32 @fcntl(i32 0, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %8, i32 %18)
%24 = tail call i32 @sleep(i32 1)
br label %16
}