#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ RTLD_NOW;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  void *handle;
  if (rename("libafk-temp.so", ".libafk-temp.so.temp")) {
    fprintf(stderr, "Unable to rename %s: %s\n", "libafk-temp.so",
            strerror(errno));
    return EXIT_FAILURE;
  }
  handle = dlopen("libafk.so", RTLD_NOW);
  if (!handle) {
    fprintf(stderr, "Could not open ./%s: %s\n", "libafk.so", dlerror());
    return EXIT_FAILURE;
  }
  if (rename(".libafk-temp.so.temp", "libafk-temp.so")) {
    fprintf(stderr, "Unable to rename %s: %s\n", ".libafk-temp.so.temp",
            strerror(errno));
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <rename@plt>
cmp    $0x0,%rax
je     11c7 <main+0x77>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rcx
lea    0xe7a(%rip),%rsi
lea    0xe4f(%rip),%rdx
call   1050 <fprintf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1295 <main+0x145>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1228 <main+0xd8>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rcx
lea    0xe3c(%rip),%rsi
lea    0xe2b(%rip),%rdx
call   1050 <fprintf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1295 <main+0x145>
lea    0xde0(%rip),%rdi
lea    0xdca(%rip),%rsi
call   1060 <rename@plt>
cmp    $0x0,%rax
je     1289 <main+0x139>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rcx
lea    0xdb8(%rip),%rsi
lea    0xd9c(%rip),%rdx
call   1050 <fprintf@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1295 <main+0x145>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1060 <rename@plt>
test   %rax,%rax
je     11a3 <main+0x53>
mov    0x2e68(%rip),%rax
mov    (%rax),%ebx
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe9d(%rip),%rsi
lea    0xe72(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11fc <main+0xac>
lea    0xe4b(%rip),%rdi
lea    0xe35(%rip),%rsi
call   1060 <rename@plt>
test   %rax,%rax
je     121f <main+0xcf>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebx
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe31(%rip),%rsi
lea    0xe15(%rip),%rdx
jmp    118e <main+0x3e>
mov    0x2dd5(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe34(%rip),%rsi
lea    0xe23(%rip),%rdx
jmp    118e <main+0x3e>
mov    0x2d9a(%rip),%rax
jmp    119f <main+0x4f>

LLVM IR (-O0):
@.str = [15 x i8] c"libafk-temp.so\00"
@.str.1 = [21 x i8] c".libafk-temp.so.temp\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Unable to rename %s: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"libafk.so\00"
@RTLD_NOW = global i32 0
@.str.4 = [25 x i8] c"Could not open ./%s: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 @rename(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %3
br label %36
15: ; preds = %2
%16 = load i32, i32* @RTLD_NOW
%17 = call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i8* (...) @dlerror()
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
store i32 %24, i32* %3
br label %36
25: ; preds = %15
%26 = call i64 @rename(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
store i32 %33, i32* %3
br label %36
34: ; preds = %25
%35 = load i32, i32* @EXIT_SUCCESS
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %28, %20, %9
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [15 x i8] c"libafk-temp.so\00"
@.str.1 = [21 x i8] c".libafk-temp.so.temp\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Unable to rename %s: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"libafk.so\00"
@RTLD_NOW = global i32 0
@.str.4 = [25 x i8] c"Could not open ./%s: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 @rename(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %26
10: ; preds = %2
%11 = load i32, i32* @RTLD_NOW
%12 = tail call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i8* (...) @dlerror()
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %16)
br label %26
18: ; preds = %10
%19 = tail call i64 @rename(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %18, %21, %14, %5
%27 = phi i32* [ @EXIT_FAILURE, %5 ], [ @EXIT_FAILURE, %21 ], [ @EXIT_FAILURE, %14 ], [ @EXIT_SUCCESS, %18 ]
%28 = load i32, i32* %27
ret i32 %28
}