#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 pid_t;
typedef int /*<<< orphan*/ flagstr;
typedef int /*<<< orphan*/ fdstr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execl(char *, char *, char *, char const *, char *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ readfd(int, char *, int, int *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socketpair(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int myopen(const char *pathname, int flags) {
  pid_t pid;
  int sockfd[2];
  int recvfd;
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) == -1) {
    fprintf(stderr, "sockpair: %m\n");
    exit(1);
  }
  printf("sock1 = %d sock2 = %d\n", sockfd[0], sockfd[1]);
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "fork: %m\n");
    exit(1);
  } else if (pid == 0) {
    close(sockfd[0]);
    char fdstr[32];
    char flagstr[32];
    snprintf(fdstr, sizeof(fdstr), "%d", sockfd[1]);
    snprintf(flagstr, sizeof(flagstr), "%d", flags);
    execl("./openfile", "openfile", fdstr, pathname, flagstr, NULL);
    exit(1);
  }
  close(sockfd[1]);
  int status;
  char c;
  waitpid(pid, &status, 0);
  if (!WIFEXITED(status))
    exit(1);
  if (WEXITSTATUS(status) == 0) {
    if (readfd(sockfd[0], &c, 1, &recvfd) < 0)
      exit(1);
  }
  return recvfd;
}


x86 assembly (clang, -O0):
<myopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0xffffffff,%eax
jne    1214 <myopen+0x54>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xded(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1259 <myopen+0x99>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jne    12c9 <myopen+0x109>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x20,%esi
lea    0xdb1(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x20,%esi
lea    0xd99(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    -0x60(%rbp),%r8
lea    0xd84(%rip),%rdi
lea    0xd7f(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r9d
call   10d0 <execl@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12ce <myopen+0x10e>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x64(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x64(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    12ff <myopen+0x13f>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x64(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    133f <myopen+0x17f>
mov    -0x18(%rbp),%edi
lea    -0x65(%rbp),%rsi
mov    $0x1,%edx
lea    -0x1c(%rbp),%rcx
call   10a0 <readfd@plt>
cmp    $0x0,%rax
jge    133a <myopen+0x17a>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    133f <myopen+0x17f>
mov    -0x1c(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myopen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0xffffffff,%eax
je     1293 <myopen+0xd3>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe07(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12a5 <myopen+0xe5>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     130e <myopen+0x14e>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x10(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
je     1289 <myopen+0xc9>
mov    0x10(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %rax,%rcx
test   %rcx,%rcx
jne    127c <myopen+0xbc>
mov    0x8(%rsp),%edi
lea    0x7(%rsp),%rsi
lea    0x30(%rsp),%rcx
mov    $0x1,%edx
call   10a0 <readfd@plt>
test   %rax,%rax
js     1289 <myopen+0xc9>
mov    0x30(%rsp),%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
jmp    131e <myopen+0x15e>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%ecx
lea    0xd76(%rip),%rbx
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
call   1040 <snprintf@plt>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1040 <snprintf@plt>
lea    0xd46(%rip),%rdi
lea    0xd41(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
xor    %r9d,%r9d
call   10d0 <execl@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"sockpair: %m\0A\00"
@.str.1 = [23 x i8] c"sock1 = %d sock2 = %d\0A\00"
@.str.2 = [10 x i8] c"fork: %m\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [11 x i8] c"./openfile\00"
@.str.5 = [9 x i8] c"openfile\00"
define i32 @myopen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca [32 x i8]
%10 = alloca i32
%11 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
%12 = load i32, i32* @AF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%15 = call i32 @socketpair(i32 %12, i32 %13, i32 0, i32* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %25)
%27 = call i32 (...) @fork()
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = load i32, i32* %5
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %53
37: ; preds = %34
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @snprintf(i8* %41, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* %4
%47 = call i32 @snprintf(i8* %45, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%49 = load i8*, i8** %3
%50 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%51 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %48, i8* %49, i8* %50, i32* null)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %34
br label %54
54: ; preds = %53
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %5
%59 = call i32 @waitpid(i32 %58, i32* %10, i32 0)
%60 = load i32, i32* %10
%61 = call i32 @WIFEXITED(i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %65, label %63
63: ; preds = %54
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %54
%66 = load i32, i32* %10
%67 = call i64 @WEXITSTATUS(i32 %66)
%68 = icmp eq i64 %67, 0
br i1 %68, label %69, label %77
69: ; preds = %65
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%71 = load i32, i32* %70
%72 = call i64 @readfd(i32 %71, i8* %11, i32 1, i32* %7)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %69
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %69
br label %77
77: ; preds = %76, %65
%78 = load i32, i32* %7
ret i32 %78
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"sockpair: %m\0A\00"
@.str.1 = [23 x i8] c"sock1 = %d sock2 = %d\0A\00"
@.str.2 = [10 x i8] c"fork: %m\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [11 x i8] c"./openfile\00"
@.str.5 = [9 x i8] c"openfile\00"
define i32 @myopen(i8* %0, i32 %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca [32 x i8]
%6 = alloca [32 x i8]
%7 = alloca i32
%8 = alloca i8
%9 = bitcast [2 x i32]* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%14 = call i32 @socketpair(i32 %11, i32 %12, i32 0, i32* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %13
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %23)
%25 = call i32 (...) @fork()
switch i32 %25, label %40 [
i32 -1, label %26
i32 0, label %30
]
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = load i32, i32* %13
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%35 = load i32, i32* %22
%36 = call i32 @snprintf(i8* %33, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = call i32 @snprintf(i8* %34, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%38 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %33, i8* %0, i8* %34, i32* null)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %20
%41 = load i32, i32* %22
%42 = call i32 @close(i32 %41)
%43 = bitcast i32* %7 to i8*
%44 = call i32 @waitpid(i32 %25, i32* %7, i32 0)
%45 = load i32, i32* %7
%46 = call i32 @WIFEXITED(i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %40
%51 = load i32, i32* %7
%52 = call i64 @WEXITSTATUS(i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %50
%55 = load i32, i32* %13
%56 = call i64 @readfd(i32 %55, i8* %8, i32 1, i32* %4)
%57 = icmp slt i64 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %54
%59 = load i32, i32* %4
br label %62
60: ; preds = %54
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %58, %50
%63 = phi i32 [ %59, %58 ], [ undef, %50 ]
ret i32 %63
}