#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 /*<<< orphan*/ recv_buf;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ argsockfd;
typedef int /*<<< orphan*/ argmode;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ execl(char *, char *, char *, char const *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_fd(int, char *, int, int *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int my_open(const char *pathname, int mode) {
  pid_t pid;
  int fd, sockfd[2], status;
  char recv_buf[256], argsockfd[10], argmode[10];
  if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd) < 0) {
    perror("socketpair");
    exit(EXIT_FAILURE);
  }
  pid = fork();
  if (pid > 0) {
    close(sockfd[0]);
    if (waitpid(pid, &status, 0) < 0) {
      perror("waitpid");
      exit(EXIT_FAILURE);
    }
    if (0 == WIFEXITED(status)) {
      printf("child did not terminate\n");
      exit(EXIT_FAILURE);
    }
    if (0 == (status = WEXITSTATUS(status))) {
      bzero(recv_buf, sizeof(recv_buf));
      read_fd(sockfd[1], recv_buf, sizeof(recv_buf), &fd);
      printf("sockfd[1] = %d, recv_buf = %s, fd = %d.\n", sockfd[1], recv_buf,
             fd);
    } else {
      errno = status;
      fd = -1;
    }
    close(sockfd[1]);
    return fd;
  } else if (0 == pid) {
    close(sockfd[1]);
    snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[0]);
    snprintf(argmode, sizeof(argmode), "%d", mode);
    execl("./openfile", "openfile", argsockfd, pathname, argmode, (char *)NULL);
    printf("execl error\n");
    exit(EXIT_FAILURE);
  } else {
    perror("fork");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<my_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
jge    1223 <my_open+0x53>
lea    0xdf0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    132d <my_open+0x15d>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0x0,%rax
jge    1274 <my_open+0xa4>
lea    0xdaa(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%edi
call   10c0 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12a6 <my_open+0xd6>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jne    1306 <my_open+0x136>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    -0x20(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
lea    -0x1c(%rbp),%rcx
call   1030 <read_fd@plt>
mov    -0x20(%rbp),%esi
lea    -0x130(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1319 <my_open+0x149>
mov    -0x28(%rbp),%ecx
mov    0x2cc0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    13bd <my_open+0x1ed>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
lea    -0x13a(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    $0xa,%esi
lea    0xcfe(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x144(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0xa,%esi
lea    0xce3(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x13a(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    -0x144(%rbp),%r8
lea    0xcc8(%rip),%rdi
lea    0xcc3(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r9d
call   10e0 <execl@plt>
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c12(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xcac(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2bf8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<my_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
test   %rax,%rax
js     12e3 <my_open+0x103>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
jle    12ef <my_open+0x10f>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   10a0 <waitpid@plt>
test   %rax,%rax
js     1362 <my_open+0x182>
mov    0x8(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
je     136b <my_open+0x18b>
mov    0x8(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
je     1285 <my_open+0xa5>
mov    0x2d57(%rip),%rcx
mov    %eax,(%rcx)
movl   $0xffffffff,0x14(%rsp)
ff
jmp    12c6 <my_open+0xe6>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
mov    0x10(%rsp),%edi
lea    0x14(%rsp),%rcx
mov    %rbx,%rsi
mov    $0x100,%edx
call   1030 <read_fd@plt>
mov    0x10(%rsp),%esi
mov    0x14(%rsp),%ecx
lea    0xd57(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0x14(%rsp),%eax
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd16(%rip),%rdi
jmp    1380 <my_open+0x1a0>
jne    1379 <my_open+0x199>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%ecx
lea    0xd33(%rip),%rbx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1060 <snprintf@plt>
lea    0x14(%rsp),%r12
mov    %r12,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1060 <snprintf@plt>
lea    0xd03(%rip),%rdi
lea    0xcfe(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
xor    %r9d,%r9d
call   10f0 <execl@plt>
lea    0xcf4(%rip),%rdi
call   1040 <puts@plt>
jmp    1385 <my_open+0x1a5>
lea    0xca2(%rip),%rdi
jmp    1380 <my_open+0x1a0>
lea    0xce9(%rip),%rdi
call   1040 <puts@plt>
jmp    1385 <my_open+0x1a5>
lea    0xcca(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c3c(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [25 x i8] c"child did not terminate\0A\00"
@.str.3 = [41 x i8] c"sockfd[1] = %d, recv_buf = %s, fd = %d.\0A\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [11 x i8] c"./openfile\00"
@.str.6 = [9 x i8] c"openfile\00"
@.str.7 = [13 x i8] c"execl error\0A\00"
@.str.8 = [5 x i8] c"fork\00"
define i32 @my_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca [10 x i8]
%11 = alloca [10 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%12 = load i32, i32* @AF_LOCAL
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%15 = call i64 @socketpair(i32 %12, i32 %13, i32 0, i32* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = call i64 (...) @fork()
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %67
25: ; preds = %21
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = load i64, i64* %5
%30 = call i64 @waitpid(i64 %29, i32* %8, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %25
%37 = load i32, i32* %8
%38 = call i64 @WIFEXITED(i32 %37)
%39 = icmp eq i64 0, %38
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %36
%45 = load i32, i32* %8
%46 = call i32 @WEXITSTATUS(i32 %45)
store i32 %46, i32* %8
%47 = icmp eq i32 0, %46
br i1 %47, label %48, label %60
48: ; preds = %44
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%50 = call i32 @bzero(i8* %49, i32 256)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%52 = load i32, i32* %51
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%54 = call i32 @read_fd(i32 %52, i8* %53, i32 256, i32* %6)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%56 = load i32, i32* %55
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%58 = load i32, i32* %6
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %56, i8* %57, i32 %58)
br label %62
60: ; preds = %44
%61 = load i32, i32* %8
store i32 %61, i32* @errno
store i32 -1, i32* %6
br label %62
62: ; preds = %60, %48
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %6
ret i32 %66
67: ; preds = %21
%68 = load i64, i64* %5
%69 = icmp eq i64 0, %68
br i1 %69, label %70, label %88
70: ; preds = %67
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%76 = load i32, i32* %75
%77 = call i32 @snprintf(i8* %74, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %76)
%78 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%79 = load i32, i32* %4
%80 = call i32 @snprintf(i8* %78, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %79)
%81 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%82 = load i8*, i8** %3
%83 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%84 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %81, i8* %82, i8* %83, i8* null)
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%86 = load i32, i32* @EXIT_FAILURE
%87 = call i32 @exit(i32 %86)
unreachable
88: ; preds = %67
%89 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
}

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