#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 scalar_t__ pid_t;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int pipe(int *);
int /*<<< orphan*/ stderr;
char *strerror_r(int, char *, int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int get_readonly_fd_pointing_to(const char *membuf) {
  char errmsgbuf[BUFSIZ];
  int pipefd[2];
  pid_t pid;
  if (pipe(pipefd) == -1) {
    fprintf(stderr,
            "[libfakefile] pipe call failed with errno=%i, msg=\"%s\"\n", errno,
            strerror_r(errno, errmsgbuf, BUFSIZ));
    exit(EXIT_FAILURE);
  }
  pid = fork();
  if (pid < (pid_t)0) {
    fprintf(stderr,
            "[libfakefile] fork call failed with errno=%i, msg=\"%s\"\n", errno,
            strerror_r(errno, errmsgbuf, BUFSIZ));
    exit(EXIT_FAILURE);
  }
  if (pid == (pid_t)0) {
    close(pipefd[0]);
    write(pipefd[1], membuf, strlen(membuf));
    close(pipefd[1]);
    exit(EXIT_SUCCESS);
  }
  close(pipefd[1]);
  wait(NULL);
  return pipefd[0];
}


x86 assembly (clang, -O0):
<get_readonly_fd_pointing_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1230 <get_readonly_fd_pointing_to+0xa0>
mov    -0x30(%rbp),%rsi
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
call   1080 <strerror_r@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xde3(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    129c <get_readonly_fd_pointing_to+0x10c>
mov    -0x30(%rbp),%rsi
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
call   1080 <strerror_r@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %rax,%rcx
lea    0xdaf(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12ea <get_readonly_fd_pointing_to+0x15a>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x20(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_readonly_fd_pointing_to>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e2f(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11fd <get_readonly_fd_pointing_to+0x6d>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1229 <get_readonly_fd_pointing_to+0x99>
je     126f <get_readonly_fd_pointing_to+0xdf>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    -0x28(%rbp),%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ddc(%rip),%rax
mov    (%rax),%r14d
mov    0x2dc2(%rip),%rax
mov    (%rax),%r15d
mov    (%r12),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <strerror_r@plt>
lea    0xdd9(%rip),%rsi
jmp    1253 <get_readonly_fd_pointing_to+0xc3>
mov    0x2db0(%rip),%rax
mov    (%rax),%r14d
mov    0x2d96(%rip),%rax
mov    (%rax),%r15d
mov    (%r12),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <strerror_r@plt>
lea    0xde5(%rip),%rsi
mov    %r14d,%edi
mov    %r15d,%edx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"[libfakefile] pipe call failed with errno=%i, msg=\22%s\22\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [56 x i8] c"[libfakefile] fork call failed with errno=%i, msg=\22%s\22\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @get_readonly_fd_pointing_to(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [2 x i32]
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %23
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = load i32, i32* @errno
%18 = load i32, i32* @BUFSIZ
%19 = call i8* @strerror_r(i32 %17, i8* %10, i32 %18)
%20 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %16, i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %1
%24 = call i64 (...) @fork()
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = load i32, i32* @errno
%31 = load i32, i32* @BUFSIZ
%32 = call i8* @strerror_r(i32 %30, i8* %10, i32 %31)
%33 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %23
%37 = load i64, i64* %6
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i8*, i8** %2
%46 = load i8*, i8** %2
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @write(i32 %44, i8* %45, i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @EXIT_SUCCESS
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %36
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = call i32 @wait(i32* null)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%60 = load i32, i32* %59
%61 = load i8*, i8** %3
ret i32 %60
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"[libfakefile] pipe call failed with errno=%i, msg=\22%s\22\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [56 x i8] c"[libfakefile] fork call failed with errno=%i, msg=\22%s\22\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @get_readonly_fd_pointing_to(i8* %0) {
%2 = alloca [2 x i32]
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast [2 x i32]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = load i32, i32* @BUFSIZ
%14 = call i8* @strerror_r(i32 %12, i8* %5, i32 %13)
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
%19 = call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = load i32, i32* @BUFSIZ
%25 = call i8* @strerror_r(i32 %23, i8* %5, i32 %24)
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %23, i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = icmp eq i64 %19, 0
br i1 %30, label %31, label %42
31: ; preds = %29
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @strlen(i8* %0)
%37 = call i32 @write(i32 %35, i8* %0, i32 %36)
%38 = load i32, i32* %34
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %29
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = call i32 @wait(i32* null)
%47 = load i32, i32* %7
ret i32 %47
}