#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read_fd(int, char *, int, int *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
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 c, 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() failed");
    exit(-1);
  }
  close(sockfd[1]);
  waitpid(childpid, &status, 0);
  if (WIFEXITED(status) == 0) {
    fprintf(stderr, "child did not terminate\n");
    exit(-1);
  }
  if ((status = WEXITSTATUS(status)) == 0) {
    read_fd(sockfd[0], &c, 1, &fd);
  } 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    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    1271 <my_open+0xb1>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    -0x2b(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0xa,%esi
lea    0xde6(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x35(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0xa,%esi
lea    0xdce(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x2b(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    -0x35(%rbp),%r8
lea    0xdb9(%rip),%rdi
lea    0xdb4(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r9d
call   10d0 <execl@plt>
lea    0xdac(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
jne    12b8 <my_open+0xf8>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    12e6 <my_open+0x126>
mov    -0x18(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
lea    -0x10(%rbp),%rcx
call   1030 <read_fd@plt>
jmp    12f9 <my_open+0x139>
mov    -0x1c(%rbp),%ecx
mov    0x2cd8(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <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    $0x30,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     127f <my_open+0xbf>
mov    %eax,%ebx
mov    0x14(%rsp),%edi
call   1050 <close@plt>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x18(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
je     12f4 <my_open+0x134>
mov    0x18(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,0x18(%rsp)
test   %eax,%eax
je     124d <my_open+0x8d>
mov    0x2d87(%rip),%rcx
mov    %eax,(%rcx)
movl   $0xffffffff,0x24(%rsp)
ff
jmp    1265 <my_open+0xa5>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
lea    0x24(%rsp),%rcx
mov    $0x1,%edx
call   1030 <read_fd@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x24(%rsp),%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%ecx
lea    0xd6d(%rip),%rbx
lea    0x24(%rsp),%r15
mov    %r15,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1040 <snprintf@plt>
lea    0x18(%rsp),%r12
mov    %r12,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1040 <snprintf@plt>
lea    0xd3d(%rip),%rdi
lea    0xd38(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
xor    %r9d,%r9d
call   10d0 <execl@plt>
lea    0xd29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    0xd19(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <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 = [15 x i8] c"execl() failed\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"child did not terminate\0A\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 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]* %6, i64 0, i64 0
%15 = call i32 @socketpair(i32 %12, i32 %13, i32 0, i32* %14)
%16 = call i32 (...) @fork()
store i32 %16, i32* %8
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %2
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @snprintf(i8* %22, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%27 = load i32, i32* %4
%28 = call i32 @snprintf(i8* %26, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%30 = load i8*, i8** %3
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%32 = 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* %29, i8* %30, i8* %31, i8* null)
%33 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %2
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %8
%40 = call i32 @waitpid(i32 %39, i32* %7, i32 0)
%41 = load i32, i32* %7
%42 = call i64 @WIFEXITED(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %35
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %35
%49 = load i32, i32* %7
%50 = call i32 @WEXITSTATUS(i32 %49)
store i32 %50, i32* %7
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%54 = load i32, i32* %53
%55 = call i32 @read_fd(i32 %54, i8* %9, i32 1, i32* %5)
br label %58
56: ; preds = %48
%57 = load i32, i32* %7
store i32 %57, i32* @errno
store i32 -1, i32* %5
br label %58
58: ; preds = %56, %52
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %5
ret i32 %62
}

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