#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 */
struct stat {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *, char *,
                         char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int mkstemp(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlink(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int prelinked_open(char *name) {
  pid_t pid;
  int fd, status;
  struct stat stb;
  char template[21];
  if (stat("/usr/sbin/prelink", &stb)) {
    perror("/usr/sbin/prelink");
    fprintf(stderr, "prelink not installed, cannot undo prelinking\n");
    exit(1);
  }
  strcpy(template, "/tmp/deltarpm.XXXXXX");
  if ((fd = mkstemp(template)) == -1) {
    perror("mkstemp");
    exit(1);
  }
  close(fd);
  pid = fork();
  if (pid == (pid_t)(-1)) {
    perror("fork");
    exit(1);
  }
  if (!pid) {
    execl("/usr/sbin/prelink", "prelink", "-o", template, "-u", name,
          (char *)0);
    perror("/usr/sbin/prelink");
    _exit(1);
  }
  while (waitpid(pid, &status, 0) == (pid_t)-1)
    ;
  if ((fd = open(template, O_RDONLY)) == -1) {
    perror(template);
    exit(1);
  }
  unlink(template);
  return fd;
}


x86 assembly (clang, -O0):
<prelinked_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe1d(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1221 <prelinked_open+0x51>
lea    0xe03(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x40(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1050 <strcpy@plt>
lea    -0x40(%rbp),%rdi
call   1090 <mkstemp@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    125c <prelinked_open+0x8c>
lea    0xe09(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1290 <prelinked_open+0xc0>
lea    0xddd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12e4 <prelinked_open+0x114>
lea    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%r9
lea    0xd56(%rip),%rdi
lea    0xd59(%rip),%rsi
lea    0xdab(%rip),%rdx
lea    0xda7(%rip),%r8
xor    %eax,%eax
movq   $0x0,(%rsp)
call   10e0 <execl@plt>
lea    0xd2b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
jmp    12e9 <prelinked_open+0x119>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    $0xffffffffffffffff,%rax
jne    1307 <prelinked_open+0x137>
jmp    12e9 <prelinked_open+0x119>
lea    -0x40(%rbp),%rdi
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1338 <prelinked_open+0x168>
lea    -0x40(%rbp),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x40(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prelinked_open>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0xe1f(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1295 <prelinked_open+0xc5>
lea    0xe46(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
call   1090 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     12c0 <prelinked_open+0xf0>
mov    %eax,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffffffffffff,%rax
je     12c9 <prelinked_open+0xf9>
mov    %rax,%rbx
test   %rax,%rax
je     12d2 <prelinked_open+0x102>
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    $0xffffffffffffffff,%rax
je     1250 <prelinked_open+0x80>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1318 <prelinked_open+0x148>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd64(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd8f(%rip),%rdi
jmp    131d <prelinked_open+0x14d>
lea    0xd8e(%rip),%rdi
jmp    131d <prelinked_open+0x14d>
movq   $0x0,(%rsp)
lea    0xd1f(%rip),%rbx
lea    0xd22(%rip),%rsi
lea    0xd74(%rip),%rdx
lea    0xd70(%rip),%r8
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%r9
call   10e0 <execl@plt>
mov    %rbx,%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
lea    0x10(%rsp),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [18 x i8] c"/usr/sbin/prelink\00"
@stderr = global i32 0
@.str.1 = [47 x i8] c"prelink not installed, cannot undo prelinking\0A\00"
@.str.2 = [21 x i8] c"/tmp/deltarpm.XXXXXX\00"
@.str.3 = [8 x i8] c"mkstemp\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [8 x i8] c"prelink\00"
@.str.6 = [3 x i8] c"-o\00"
@.str.7 = [3 x i8] c"-u\00"
@O_RDONLY = global i32 0
define i32 @prelinked_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca [21 x i8]
store i8* %0, i8** %2
%8 = call i64 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%17 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%18 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%19 = call i32 @mkstemp(i8* %18)
store i32 %19, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
%27 = call i64 (...) @fork()
store i64 %27, i64* %3
%28 = load i64, i64* %3
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %24
%34 = load i64, i64* %3
%35 = icmp ne i64 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %33
%37 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%38 = load i8*, i8** %2
%39 = call i32 @execl(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %38, i8* null)
%40 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @_exit(i32 1)
unreachable
42: ; preds = %33
br label %43
43: ; preds = %47, %42
%44 = load i64, i64* %3
%45 = call i64 @waitpid(i64 %44, i32* %5, i32 0)
%46 = icmp eq i64 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %43
br label %43
48: ; preds = %43
%49 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%50 = load i32, i32* @O_RDONLY
%51 = call i32 @open(i8* %49, i32 %50)
store i32 %51, i32* %4
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%55 = call i32 @perror(i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %48
%58 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%59 = call i32 @unlink(i8* %58)
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"/usr/sbin/prelink\00"
@stderr = global i32 0
@.str.1 = [47 x i8] c"prelink not installed, cannot undo prelinking\0A\00"
@.str.2 = [21 x i8] c"/tmp/deltarpm.XXXXXX\00"
@.str.3 = [8 x i8] c"mkstemp\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [8 x i8] c"prelink\00"
@.str.6 = [3 x i8] c"-o\00"
@.str.7 = [3 x i8] c"-u\00"
@O_RDONLY = global i32 0
define i32 @prelinked_open(i8* %0) {
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca [21 x i8]
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.stat* %3 to i8*
%7 = getelementptr inbounds [21 x i8], [21 x i8]* %4, i64 0, i64 0
%8 = call i64 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @mkstemp(i8* %7)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = call i32 @close(i32 %17)
%24 = call i64 (...) @fork()
switch i64 %24, label %32 [
i64 -1, label %25
i64 0, label %28
]
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = call i32 @execl(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* null)
%30 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @_exit(i32 1)
unreachable
32: ; preds = %22, %32
%33 = call i64 @waitpid(i64 %24, i32* %2, i32 0)
%34 = icmp eq i64 %33, -1
br i1 %34, label %32, label %35
35: ; preds = %32
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %7, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* %7)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = call i32 @unlink(i8* %7)
ret i32 %37
}