#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 */
scalar_t__ access(char *, int);
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ execve(char *, char const *const *, char const *const *);
int /*<<< orphan*/ fcntl(int const, int, int);
int const getpid();
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
char *upx_itoa(int const, char *const);
int __attribute__((__no__, __used__, __stdcall__))
go_self(char const *tmpname, char *argv[], char *envp[]) {
  int const fdi = open(tmpname, 00, 0);
  if (0 <= fdi) {
    char procself_buf[31 + 1];
    (procself_buf + 0)[0] = '/', (procself_buf + 0)[1] = 'p',
                    (procself_buf + 0)[2] = 'r', (procself_buf + 0)[3] = 'o';
    (procself_buf + 4)[0] = 'c', (procself_buf + 4)[1] = '/',
                    (procself_buf + 4)[2] = 0, (procself_buf + 4)[3] = 0;
    {
      char *const procself = upx_itoa(getpid(), procself_buf + 6);
      (procself)[0] = '/', (procself)[1] = 'f', (procself)[2] = 'd',
      (procself)[3] = '/';
      upx_itoa(fdi, procself + 4);
    }
    if (0x32585055 == (unsigned)access(procself_buf, 4 | 1)) {
      unlink(tmpname);
      fcntl(fdi, 2, 1);
      execve(procself_buf, (char const *const *)argv,
             (char const *const *)envp);
    }
    close(fdi);
  }
  return fdi;
}


x86 assembly (clang, -O0):
<go_self>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jg     1269 <go_self+0xe9>
movb   $0x2f,-0x40(%rbp)
movb   $0x70,-0x3f(%rbp)
movb   $0x72,-0x3e(%rbp)
movb   $0x6f,-0x3d(%rbp)
movb   $0x63,-0x3c(%rbp)
movb   $0x2f,-0x3b(%rbp)
movb   $0x0,-0x3a(%rbp)
movb   $0x0,-0x39(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%edi
lea    -0x40(%rbp),%rsi
add    $0x6,%rsi
call   1060 <upx_itoa@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x48(%rbp),%rax
movb   $0x66,0x1(%rax)
mov    -0x48(%rbp),%rax
movb   $0x64,0x2(%rax)
mov    -0x48(%rbp),%rax
movb   $0x2f,0x3(%rax)
mov    -0x1c(%rbp),%edi
mov    -0x48(%rbp),%rsi
add    $0x4,%rsi
call   1060 <upx_itoa@plt>
lea    -0x40(%rbp),%rdi
mov    $0x5,%esi
call   10a0 <access@plt>
mov    %eax,%ecx
mov    $0x32585055,%eax
cmp    %ecx,%eax
jne    1261 <go_self+0xe1>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
mov    $0x1,%edx
call   1040 <fcntl@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <execve@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<go_self>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %esi,%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     121b <go_self+0x9b>
movabs $0x2f636f72702f,%rax
mov    %rax,(%rsp)
lea    0x6(%rsp),%r12
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1060 <upx_itoa@plt>
movl   $0x2f64662f,(%rax)
lea    0x4(%rax),%rsi
mov    %ebp,%edi
call   1060 <upx_itoa@plt>
mov    %rsp,%rdi
mov    $0x5,%esi
call   10a0 <access@plt>
cmp    $0x32585055,%eax
jne    1214 <go_self+0x94>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
mov    $0x2,%esi
mov    $0x1,%edx
call   1040 <fcntl@plt>
mov    %rsp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <execve@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (i8*, i8**, i8**)* @go_self to i8*)], section "llvm.metadata"
define i32 @go_self(i8* %0, i8** %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%10 = load i8*, i8** %4
%11 = call i32 @open(i8* %10, i32 0, i32 0)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp sle i32 0, %12
br i1 %13, label %14, label %71
14: ; preds = %3
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 47, i8* %17
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8 112, i8* %20
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = getelementptr inbounds i8, i8* %22, i64 2
store i8 114, i8* %23
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = getelementptr inbounds i8, i8* %25, i64 3
store i8 111, i8* %26
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%28 = getelementptr inbounds i8, i8* %27, i64 4
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 99, i8* %29
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%31 = getelementptr inbounds i8, i8* %30, i64 4
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 47, i8* %32
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%34 = getelementptr inbounds i8, i8* %33, i64 4
%35 = getelementptr inbounds i8, i8* %34, i64 2
store i8 0, i8* %35
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%37 = getelementptr inbounds i8, i8* %36, i64 4
%38 = getelementptr inbounds i8, i8* %37, i64 3
store i8 0, i8* %38
%39 = call i32 (...) @getpid()
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%41 = getelementptr inbounds i8, i8* %40, i64 6
%42 = call i8* @upx_itoa(i32 %39, i8* %41)
store i8* %42, i8** %9
%43 = load i8*, i8** %9
%44 = getelementptr inbounds i8, i8* %43, i64 0
store i8 47, i8* %44
%45 = load i8*, i8** %9
%46 = getelementptr inbounds i8, i8* %45, i64 1
store i8 102, i8* %46
%47 = load i8*, i8** %9
%48 = getelementptr inbounds i8, i8* %47, i64 2
store i8 100, i8* %48
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i64 3
store i8 47, i8* %50
%51 = load i32, i32* %7
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i64 4
%54 = call i8* @upx_itoa(i32 %51, i8* %53)
%55 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%56 = call i64 @access(i8* %55, i32 5)
%57 = trunc i64 %56 to i32
%58 = icmp eq i32 844648533, %57
br i1 %58, label %59, label %68
59: ; preds = %14
%60 = load i8*, i8** %4
%61 = call i32 @unlink(i8* %60)
%62 = load i32, i32* %7
%63 = call i32 @fcntl(i32 %62, i32 2, i32 1)
%64 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%65 = load i8**, i8*** %5
%66 = load i8**, i8*** %6
%67 = call i32 @execve(i8* %64, i8** %65, i8** %66)
br label %68
68: ; preds = %59, %14
%69 = load i32, i32* %7
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %3
%72 = load i32, i32* %7
ret i32 %72
}

LLVM IR (-O2):
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (i8*, i8**, i8**)* @go_self to i8*)], section "llvm.metadata"
define i32 @go_self(i8* nocapture %0, i8** %1, i8** %2) {
%4 = alloca [32 x i8]
%5 = tail call i32 @open(i8* %0, i32 0, i32 0)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %32
7: ; preds = %3
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
store i8 47, i8* %8
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 1
store i8 112, i8* %9
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 2
store i8 114, i8* %10
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 3
store i8 111, i8* %11
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 4
store i8 99, i8* %12
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 5
store i8 47, i8* %13
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 6
store i8 0, i8* %14
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 7
store i8 0, i8* %15
%16 = tail call i32 (...) @getpid()
%17 = call i8* @upx_itoa(i32 %16, i8* %14)
store i8 47, i8* %17
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8 102, i8* %18
%19 = getelementptr inbounds i8, i8* %17, i64 2
store i8 100, i8* %19
%20 = getelementptr inbounds i8, i8* %17, i64 3
store i8 47, i8* %20
%21 = getelementptr inbounds i8, i8* %17, i64 4
%22 = call i8* @upx_itoa(i32 %5, i8* %21)
%23 = call i64 @access(i8* %8, i32 5)
%24 = trunc i64 %23 to i32
%25 = icmp eq i32 %24, 844648533
br i1 %25, label %26, label %30
26: ; preds = %7
%27 = call i32 @unlink(i8* %0)
%28 = call i32 @fcntl(i32 %5, i32 2, i32 1)
%29 = call i32 @execve(i8* %8, i8** %1, i8** %2)
br label %30
30: ; preds = %26, %7
%31 = call i32 @close(i32 %5)
br label %32
32: ; preds = %30, %3
ret i32 %5
}