#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 **, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int const, int, int);
int const getpid();
unsigned int lseek(int const, int /*<<< orphan*/, int);
char *mmap(char *, unsigned int const, int, int, int const, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ unlink(char const *);
char *upx_itoa(int const, char *const);
scalar_t__ write(int const, char *const, unsigned int 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, argv, envp);
      unsigned const lenf = lseek(fdi, 0, 2);
      char *const buf = mmap((char *)0, lenf, 0x1 | 0x2, 0x02, fdi, 0);
      int const fdo = open(tmpname, 01 | 0100 | 0200, 0700);
      if (lenf != (unsigned)write(fdo, buf, lenf)) {
        exit(127);
      }
      close(fdo);
    }
    close(fdi);
  }
  return fdi;
}


x86 assembly (clang, -O0):
<go_self>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jg     1327 <go_self+0x167>
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   1060 <getpid@plt>
mov    %eax,%edi
lea    -0x40(%rbp),%rsi
add    $0x6,%rsi
call   1070 <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   1070 <upx_itoa@plt>
lea    -0x40(%rbp),%rdi
mov    $0x5,%esi
call   10d0 <access@plt>
mov    %eax,%ecx
mov    $0x32585055,%eax
cmp    %ecx,%eax
jne    131f <go_self+0x15f>
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   10b0 <execve@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
mov    $0x2,%edx
call   1090 <lseek@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%esi
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x3,%edx
mov    $0x2,%ecx
xor    %r9d,%r9d
call   1080 <mmap@plt>
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0xc1,%esi
mov    $0x1c0,%edx
call   10c0 <open@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1050 <write@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%eax
cmp    %ecx,%eax
je     1317 <go_self+0x157>
mov    $0x7f,%edi
call   10e0 <exit@plt>
mov    -0x5c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x60,%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,%r15
mov    %rsi,%r12
mov    %rdi,%r14
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12bd <go_self+0xfd>
movabs $0x2f636f72702f,%rax
mov    %rax,(%rsp)
lea    0x6(%rsp),%rbp
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   1070 <upx_itoa@plt>
movl   $0x2f64662f,(%rax)
lea    0x4(%rax),%rsi
mov    %ebx,%edi
call   1070 <upx_itoa@plt>
mov    %rsp,%rdi
mov    $0x5,%esi
call   10d0 <access@plt>
cmp    $0x32585055,%eax
jne    12b6 <go_self+0xf6>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %ebx,%edi
mov    $0x2,%esi
mov    $0x1,%edx
call   1040 <fcntl@plt>
mov    %rsp,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   10b0 <execve@plt>
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x2,%edx
call   1090 <lseek@plt>
mov    %eax,%r15d
xor    %edi,%edi
mov    %eax,%esi
mov    $0x3,%edx
mov    $0x2,%ecx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1080 <mmap@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    $0xc1,%esi
mov    $0x1c0,%edx
call   10c0 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <write@plt>
cmp    %eax,%r15d
jne    12cc <go_self+0x10c>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x7f,%edi
call   10e0 <exit@plt>

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*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%13 = load i8*, i8** %4
%14 = call i32 @open(i8* %13, i32 0, i32 0)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp sle i32 0, %15
br i1 %16, label %17, label %93
17: ; preds = %3
%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 0
store i8 47, 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 1
store i8 112, 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 2
store i8 114, i8* %26
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = getelementptr inbounds i8, i8* %28, i64 3
store i8 111, 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 0
store i8 99, 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 1
store i8 47, 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 2
store i8 0, i8* %38
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%40 = getelementptr inbounds i8, i8* %39, i64 4
%41 = getelementptr inbounds i8, i8* %40, i64 3
store i8 0, i8* %41
%42 = call i32 (...) @getpid()
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%44 = getelementptr inbounds i8, i8* %43, i64 6
%45 = call i8* @upx_itoa(i32 %42, i8* %44)
store i8* %45, i8** %9
%46 = load i8*, i8** %9
%47 = getelementptr inbounds i8, i8* %46, i64 0
store i8 47, i8* %47
%48 = load i8*, i8** %9
%49 = getelementptr inbounds i8, i8* %48, i64 1
store i8 102, i8* %49
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i64 2
store i8 100, i8* %51
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i64 3
store i8 47, i8* %53
%54 = load i32, i32* %7
%55 = load i8*, i8** %9
%56 = getelementptr inbounds i8, i8* %55, i64 4
%57 = call i8* @upx_itoa(i32 %54, i8* %56)
%58 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%59 = call i64 @access(i8* %58, i32 5)
%60 = trunc i64 %59 to i32
%61 = icmp eq i32 844648533, %60
br i1 %61, label %62, label %90
62: ; preds = %17
%63 = load i8*, i8** %4
%64 = call i32 @unlink(i8* %63)
%65 = load i32, i32* %7
%66 = call i32 @fcntl(i32 %65, i32 2, i32 1)
%67 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%68 = load i8**, i8*** %5
%69 = load i8**, i8*** %6
%70 = call i32 @execve(i8* %67, i8** %68, i8** %69)
%71 = load i32, i32* %7
%72 = call i32 @lseek(i32 %71, i32 0, i32 2)
store i32 %72, i32* %10
%73 = load i32, i32* %10
%74 = load i32, i32* %7
%75 = call i8* @mmap(i8* null, i32 %73, i32 3, i32 2, i32 %74, i32 0)
store i8* %75, i8** %11
%76 = load i8*, i8** %4
%77 = call i32 @open(i8* %76, i32 193, i32 448)
store i32 %77, i32* %12
%78 = load i32, i32* %10
%79 = load i32, i32* %12
%80 = load i8*, i8** %11
%81 = load i32, i32* %10
%82 = call i64 @write(i32 %79, i8* %80, i32 %81)
%83 = trunc i64 %82 to i32
%84 = icmp ne i32 %78, %83
br i1 %84, label %85, label %87
85: ; preds = %62
%86 = call i32 @exit(i32 127)
unreachable
87: ; preds = %62
%88 = load i32, i32* %12
%89 = call i32 @close(i32 %88)
br label %90
90: ; preds = %87, %17
%91 = load i32, i32* %7
%92 = call i32 @close(i32 %91)
br label %93
93: ; preds = %90, %3
%94 = load i32, i32* %7
ret i32 %94
}

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 %42
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 %40
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)
%30 = call i32 @lseek(i32 %5, i32 0, i32 2)
%31 = call i8* @mmap(i8* null, i32 %30, i32 3, i32 2, i32 %5, i32 0)
%32 = call i32 @open(i8* %0, i32 193, i32 448)
%33 = call i64 @write(i32 %32, i8* %31, i32 %30)
%34 = trunc i64 %33 to i32
%35 = icmp eq i32 %30, %34
br i1 %35, label %38, label %36
36: ; preds = %26
%37 = call i32 @exit(i32 127)
unreachable
38: ; preds = %26
%39 = call i32 @close(i32 %32)
br label %40
40: ; preds = %38, %7
%41 = call i32 @close(i32 %5)
br label %42
42: ; preds = %40, %3
ret i32 %5
}