#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
char *dirname(char *);
scalar_t__ dup2(int, int const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *get_self_path();
int /*<<< orphan*/ is_secure();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
void __attribute__((visibility("hidden"))) __attribute__((constructor))
init_prefix_fd() {
  char *prefix;
  if (!is_secure()) {
    fprintf(stderr, "init_prefix_fd(): I am suid and hard linked, "
                    "relocatability security check failed\n");
    fprintf(stderr, "init_prefix_fd(): Assuming installation prefix of /usr\n");
    prefix = strdup("/usr");
  } else {
    char *exepath = get_self_path();
    char *exedir = dirname(exepath);
    prefix = dirname(exedir);
    printf("exepath=%s, exedir=%s, prefix=%s\n", exepath, exedir, prefix);
    if (exepath)
      free(exepath);
    if (exedir)
      free(exedir);
    if (!prefix)
      return;
  }
  int fd = -1;
  const int requested_fd = 200;
  if ((fd = open(prefix, O_RDONLY)) < 0) {
    perror("open");
    free(prefix);
    return;
  }
  if (dup2(fd, requested_fd) < 0) {
    perror("dup2");
    free(prefix);
    close(fd);
    return;
  }
  free(prefix);
  close(fd);
}


x86 assembly (clang, -O0):
<init_prefix_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10a0 <is_secure@plt>
cmp    $0x0,%eax
jne    1207 <init_prefix_fd+0x57>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1090 <fprintf@plt>
lea    0xe92(%rip),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1281 <init_prefix_fd+0xd1>
mov    $0x0,%al
call   1040 <get_self_path@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe51(%rip),%rdi
call   1060 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     1258 <init_prefix_fd+0xa8>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
je     126c <init_prefix_fd+0xbc>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x8(%rbp)
jne    127c <init_prefix_fd+0xcc>
jmp    1311 <init_prefix_fd+0x161>
jmp    1281 <init_prefix_fd+0xd1>
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xc8,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12c7 <init_prefix_fd+0x117>
lea    0xdfe(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
jmp    1311 <init_prefix_fd+0x161>
mov    -0x1c(%rbp),%edi
mov    $0xc8,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1300 <init_prefix_fd+0x150>
lea    0xdd2(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
jmp    1311 <init_prefix_fd+0x161>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_prefix_fd>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   10a0 <is_secure@plt>
test   %eax,%eax
je     1218 <init_prefix_fd+0x68>
xor    %eax,%eax
call   1040 <get_self_path@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    %rax,%r14
lea    0xeaa(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1060 <printf@plt>
test   %rbx,%rbx
je     1201 <init_prefix_fd+0x51>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %rbp,%rbp
je     120e <init_prefix_fd+0x5e>
mov    %rbp,%rdi
call   1030 <free@plt>
test   %r14,%r14
jne    124a <init_prefix_fd+0x9a>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2db9(%rip),%rbx
mov    (%rbx),%edi
lea    0xdd8(%rip),%rsi
call   1090 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe1d(%rip),%rsi
call   1090 <fprintf@plt>
lea    0xe49(%rip),%rdi
call   10d0 <strdup@plt>
mov    %rax,%r14
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1285 <init_prefix_fd+0xd5>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0xc8,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     129d <init_prefix_fd+0xed>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <close@plt>
lea    0xe26(%rip),%rdi
call   10c0 <perror@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <free@plt>
lea    0xe13(%rip),%rdi
call   10c0 <perror@plt>
jmp    1272 <init_prefix_fd+0xc2>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [83 x i8] c"init_prefix_fd(): I am suid and hard linked, relocatability security check failed\0A\00"
@.str.1 = [56 x i8] c"init_prefix_fd(): Assuming installation prefix of /usr\0A\00"
@.str.2 = [5 x i8] c"/usr\00"
@.str.3 = [34 x i8] c"exepath=%s, exedir=%s, prefix=%s\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [5 x i8] c"dup2\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @init_prefix_fd, i8* null }]
define hidden void @init_prefix_fd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = call i32 (...) @is_secure()
%7 = icmp ne i32 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%13 = call i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %13, i8** %1
br label %40
14: ; preds = %0
%15 = call i8* (...) @get_self_path()
store i8* %15, i8** %2
%16 = load i8*, i8** %2
%17 = call i8* @dirname(i8* %16)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = call i8* @dirname(i8* %18)
store i8* %19, i8** %1
%20 = load i8*, i8** %2
%21 = load i8*, i8** %3
%22 = load i8*, i8** %1
%23 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %20, i8* %21, i8* %22)
%24 = load i8*, i8** %2
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = load i8*, i8** %2
%28 = call i32 @free(i8* %27)
br label %29
29: ; preds = %26, %14
%30 = load i8*, i8** %3
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %3
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %32, %29
%36 = load i8*, i8** %1
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %35
br label %64
39: ; preds = %35
br label %40
40: ; preds = %39, %8
store i32 -1, i32* %4
store i32 200, i32* %5
%41 = load i8*, i8** %1
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %4
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%47 = load i8*, i8** %1
%48 = call i32 @free(i8* %47)
br label %64
49: ; preds = %40
%50 = load i32, i32* %4
%51 = call i64 @dup2(i32 %50, i32 200)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%55 = load i8*, i8** %1
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %4
%58 = call i32 @close(i32 %57)
br label %64
59: ; preds = %49
%60 = load i8*, i8** %1
%61 = call i32 @free(i8* %60)
%62 = load i32, i32* %4
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %59, %53, %45, %38
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [83 x i8] c"init_prefix_fd(): I am suid and hard linked, relocatability security check failed\0A\00"
@.str.1 = [56 x i8] c"init_prefix_fd(): Assuming installation prefix of /usr\0A\00"
@.str.2 = [5 x i8] c"/usr\00"
@.str.3 = [34 x i8] c"exepath=%s, exedir=%s, prefix=%s\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [5 x i8] c"dup2\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @init_prefix_fd, i8* null }]
define hidden void @init_prefix_fd() {
%1 = tail call i32 (...) @is_secure()
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call dereferenceable_or_null(5) i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %23
9: ; preds = %0
%10 = tail call i8* (...) @get_self_path()
%11 = tail call i8* @dirname(i8* %10)
%12 = tail call i8* @dirname(i8* %11)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %10, i8* %11, i8* %12)
%14 = icmp eq i8* %10, null
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = tail call i32 @free(i8* %10)
br label %17
17: ; preds = %15, %9
%18 = icmp eq i8* %11, null
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i32 @free(i8* %11)
br label %21
21: ; preds = %19, %17
%22 = icmp eq i8* %12, null
br i1 %22, label %41, label %23
23: ; preds = %21, %3
%24 = phi i8* [ %12, %21 ], [ %8, %3 ]
%25 = load i32, i32* @O_RDONLY
%26 = tail call i32 @open(i8* %24, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%30 = tail call i32 @free(i8* %24)
br label %41
31: ; preds = %23
%32 = tail call i64 @dup2(i32 %26, i32 200)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%36 = tail call i32 @free(i8* %24)
%37 = tail call i32 @close(i32 %26)
br label %41
38: ; preds = %31
%39 = tail call i32 @free(i8* %24)
%40 = tail call i32 @close(i32 %26)
br label %41
41: ; preds = %28, %34, %38, %21
ret void
}