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

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


x86 assembly (clang, -O0):
<my_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rcx
xor    %edx,%edx
call   1050 <socketpair@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    1261 <my_open+0xb1>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
lea    -0x2a(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0xa,%esi
lea    0xdf6(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x34(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0xa,%esi
lea    0xdde(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x2a(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    -0x34(%rbp),%r8
lea    0xdc9(%rip),%rdi
lea    0xdc4(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r9d
call   10c0 <execl@plt>
lea    0xdbc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
jne    129f <my_open+0xef>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    12c3 <my_open+0x113>
mov    -0x18(%rbp),%edi
call   1060 <recv_fd@plt>
mov    %eax,-0x10(%rbp)
jmp    12d6 <my_open+0x126>
mov    -0x1c(%rbp),%ecx
mov    0x2d03(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rcx
xor    %edx,%edx
call   1050 <socketpair@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1259 <my_open+0xa9>
mov    %eax,%ebx
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %rax,%rax
je     12c1 <my_open+0x111>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     1236 <my_open+0x86>
mov    0x2da3(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1241 <my_open+0x91>
mov    0x4(%rsp),%edi
call   1060 <recv_fd@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%ecx
lea    0xd93(%rip),%rbx
lea    0xc(%rsp),%r15
mov    %r15,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1030 <snprintf@plt>
lea    0x16(%rsp),%r12
mov    %r12,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1030 <snprintf@plt>
lea    0xd63(%rip),%rdi
lea    0xd5e(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
xor    %r9d,%r9d
call   10c0 <execl@plt>
lea    0xd4f(%rip),%rdi
jmp    12c8 <my_open+0x118>
lea    0xd52(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [11 x i8] c"./openfile\00"
@.str.2 = [9 x i8] c"openfile\00"
@.str.3 = [12 x i8] c"execl error\00"
@.str.4 = [24 x i8] c"child did not terminate\00"
@errno = global i32 0
define i32 @my_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i8]
%10 = alloca [10 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = load i32, i32* @AF_LOCAL
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%14 = call i32 @socketpair(i32 %11, i32 %12, i32 0, i32* %13)
%15 = call i32 (...) @fork()
store i32 %15, i32* %8
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %2
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @snprintf(i8* %21, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%26 = load i32, i32* %4
%27 = call i32 @snprintf(i8* %25, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%29 = load i8*, i8** %3
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%31 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %28, i8* %29, i8* %30, i8* null)
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %2
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %8
%39 = call i32 @waitpid(i32 %38, i32* %7, i32 0)
%40 = load i32, i32* %7
%41 = call i64 @WIFEXITED(i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %34
%47 = load i32, i32* %7
%48 = call i32 @WEXITSTATUS(i32 %47)
store i32 %48, i32* %7
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @recv_fd(i32 %52)
store i32 %53, i32* %5
br label %56
54: ; preds = %46
%55 = load i32, i32* %7
store i32 %55, i32* @errno
store i32 -1, i32* %5
br label %56
56: ; preds = %54, %50
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [11 x i8] c"./openfile\00"
@.str.2 = [9 x i8] c"openfile\00"
@.str.3 = [12 x i8] c"execl error\00"
@.str.4 = [24 x i8] c"child did not terminate\00"
@errno = global i32 0
define i32 @my_open(i8* %0, i32 %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca [10 x i8]
%6 = alloca [10 x i8]
%7 = bitcast [2 x i32]* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @AF_LOCAL
%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 = call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %2
%18 = load i32, i32* %13
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @snprintf(i8* %9, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @snprintf(i8* %10, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%24 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %9, i8* %0, i8* %10, i8* null)
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = call i32 @waitpid(i32 %15, i32* %4, i32 0)
%32 = load i32, i32* %4
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %27
%39 = load i32, i32* %4
%40 = call i32 @WEXITSTATUS(i32 %39)
store i32 %40, i32* %4
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %13
%44 = call i32 @recv_fd(i32 %43)
br label %46
45: ; preds = %38
store i32 %40, i32* @errno
br label %46
46: ; preds = %45, %42
%47 = phi i32 [ %44, %42 ], [ -1, %45 ]
%48 = load i32, i32* %13
%49 = call i32 @close(i32 %48)
ret i32 %47
}