#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 */
typedef int /*<<< orphan*/ upgrade;
typedef int /*<<< orphan*/ tmp;

/* Variables and functions */
int LOG_NDELAY;
int /*<<< orphan*/ LOG_NOTICE;
int LOG_PID;
int /*<<< orphan*/ LOG_SYSLOG;
int PATH_MAX;
int /*<<< orphan*/ X_OK;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execv(char *, char **);
char *getenv(char *);
int /*<<< orphan*/ openlog(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strerror(int /*<<< orphan*/);
char *strrchr(char *, char);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int main(int argc, char **argv) {
  char tmp[PATH_MAX];
  char upgrade[PATH_MAX];
  snprintf(tmp, sizeof(tmp), "%s", argv[0]);
  char *x = strrchr(tmp, '/');
  if (x == NULL)
    x = tmp;
  else
    x++;
  snprintf(x, sizeof(tmp) - (x - tmp), "%s", "Movian.bin");
  const char *home = getenv("HOME");
  snprintf(upgrade, sizeof(upgrade), "%s/.hts/showtime/movian-upgrade.bin",
           home);
  setenv("UPGRADE_BINARY_PATH", upgrade, 1);
  openlog("Movian-Launcher", LOG_PID | LOG_NDELAY, LOG_SYSLOG);
  if (!(access(upgrade, X_OK))) {
    syslog(LOG_NOTICE, "Launching %s", upgrade);
    closelog();
    execv(upgrade, argv);
    openlog("Movian-Launcher", LOG_PID | LOG_NDELAY, LOG_SYSLOG);
    syslog(LOG_NOTICE, "Failed to launch upgrade -- %s\n", strerror(errno));
  }
  syslog(LOG_NOTICE, "Launching %s", tmp);
  closelog();
  execv(tmp, argv);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rcx
mov    (%rcx),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe4a(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x40(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1247 <main+0xa7>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1253 <main+0xb3>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x58(%rbp),%rax
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rcx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%esi
lea    0xdf0(%rip),%rdx
lea    0xd87(%rip),%rcx
call   1060 <snprintf@plt>
lea    0xd86(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
mov    %rcx,-0x38(%rbp)
mov    %eax,%esi
mov    -0x38(%rbp),%rcx
lea    0xd6a(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x48(%rbp),%rsi
lea    0xd7e(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
mov    0x2cd8(%rip),%rax
or     (%rax),%esi
mov    0x2cef(%rip),%rax
mov    (%rax),%edx
lea    0xd66(%rip),%rdi
call   10a0 <openlog@plt>
mov    -0x48(%rbp),%rdi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%eax
jne    1382 <main+0x1e2>
mov    -0x48(%rbp),%rdx
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
lea    0xd42(%rip),%rsi
call   1080 <syslog@plt>
mov    $0x0,%al
call   1040 <closelog@plt>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <execv@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%esi
mov    0x2c69(%rip),%rax
or     (%rax),%esi
mov    0x2c80(%rip),%rax
mov    (%rax),%edx
lea    0xcf7(%rip),%rdi
call   10a0 <openlog@plt>
mov    0x2c5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2c57(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xce8(%rip),%rsi
call   1080 <syslog@plt>
mov    -0x40(%rbp),%rdx
mov    0x2c2b(%rip),%rax
mov    (%rax),%edi
lea    0xcc2(%rip),%rsi
call   1080 <syslog@plt>
mov    $0x0,%al
call   1040 <closelog@plt>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <execv@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e38(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r15
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%rsi),%rcx
lea    0xe80(%rip),%r13
mov    %r15,%rdi
mov    %r12d,%esi
mov    %r13,%rdx
call   1060 <snprintf@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %r15,%rdi
mov    %r15d,%esi
sub    %edi,%esi
add    %r12d,%esi
lea    0xde9(%rip),%rcx
mov    %r13,%rdx
call   1060 <snprintf@plt>
lea    0xde5(%rip),%rdi
call   1030 <getenv@plt>
lea    0xdde(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
lea    0xded(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    0x2d75(%rip),%rax
mov    0x2d46(%rip),%r13
mov    0x0(%r13),%esi
or     (%rax),%esi
mov    0x2d59(%rip),%r12
mov    (%r12),%edx
lea    0xdce(%rip),%rdi
call   10a0 <openlog@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
test   %eax,%eax
jne    1303 <main+0x163>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    %rbx,%rdx
call   1080 <syslog@plt>
xor    %eax,%eax
call   1040 <closelog@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <execv@plt>
mov    0x0(%r13),%esi
mov    0x2d07(%rip),%rax
or     (%rax),%esi
mov    (%r12),%edx
lea    0xd72(%rip),%rdi
call   10a0 <openlog@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd6c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <syslog@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
lea    0xd45(%rip),%rsi
mov    %r15,%rdx
call   1080 <syslog@plt>
xor    %eax,%eax
call   1040 <closelog@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10c0 <execv@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [11 x i8] c"Movian.bin\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [36 x i8] c"%s/.hts/showtime/movian-upgrade.bin\00"
@.str.4 = [20 x i8] c"UPGRADE_BINARY_PATH\00"
@.str.5 = [16 x i8] c"Movian-Launcher\00"
@LOG_PID = global i32 0
@LOG_NDELAY = global i32 0
@LOG_SYSLOG = global i32 0
@X_OK = global i32 0
@LOG_NOTICE = global i32 0
@.str.6 = [13 x i8] c"Launching %s\00"
@.str.7 = [32 x i8] c"Failed to launch upgrade -- %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
store i64 %16, i64* %8
%18 = trunc i64 %12 to i32
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @snprintf(i8* %14, i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i8* @strrchr(i8* %14, i8 signext 47)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %2
store i8* %14, i8** %9
br label %30
27: ; preds = %2
%28 = load i8*, i8** %9
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %9
br label %30
30: ; preds = %27, %26
%31 = load i8*, i8** %9
%32 = load i8*, i8** %9
%33 = ptrtoint i8* %32 to i64
%34 = ptrtoint i8* %14 to i64
%35 = sub i64 %33, %34
%36 = sub i64 %12, %35
%37 = trunc i64 %36 to i32
%38 = call i32 @snprintf(i8* %31, i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%39 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %39, i8** %10
%40 = trunc i64 %16 to i32
%41 = load i8*, i8** %10
%42 = call i32 @snprintf(i8* %17, i32 %40, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = call i32 @setenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %17, i32 1)
%44 = load i32, i32* @LOG_PID
%45 = load i32, i32* @LOG_NDELAY
%46 = or i32 %44, %45
%47 = load i32, i32* @LOG_SYSLOG
%48 = call i32 @openlog(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 %47)
%49 = load i32, i32* @X_OK
%50 = call i32 @access(i8* %17, i32 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %67, label %52
52: ; preds = %30
%53 = load i32, i32* @LOG_NOTICE
%54 = call i32 @syslog(i32 %53, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %17)
%55 = call i32 (...) @closelog()
%56 = load i8**, i8*** %5
%57 = call i32 @execv(i8* %17, i8** %56)
%58 = load i32, i32* @LOG_PID
%59 = load i32, i32* @LOG_NDELAY
%60 = or i32 %58, %59
%61 = load i32, i32* @LOG_SYSLOG
%62 = call i32 @openlog(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %60, i32 %61)
%63 = load i32, i32* @LOG_NOTICE
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 @syslog(i32 %63, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %52, %30
%68 = load i32, i32* @LOG_NOTICE
%69 = call i32 @syslog(i32 %68, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %14)
%70 = call i32 (...) @closelog()
%71 = load i8**, i8*** %5
%72 = call i32 @execv(i8* %14, i8** %71)
%73 = load i8*, i8** %6
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [11 x i8] c"Movian.bin\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [36 x i8] c"%s/.hts/showtime/movian-upgrade.bin\00"
@.str.4 = [20 x i8] c"UPGRADE_BINARY_PATH\00"
@.str.5 = [16 x i8] c"Movian-Launcher\00"
@LOG_PID = global i32 0
@LOG_NDELAY = global i32 0
@LOG_SYSLOG = global i32 0
@X_OK = global i32 0
@LOG_NOTICE = global i32 0
@.str.6 = [13 x i8] c"Launching %s\00"
@.str.7 = [32 x i8] c"Failed to launch upgrade -- %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = load i8*, i8** %1
%8 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = call i8* @strrchr(i8* %5, i8 signext 47)
%10 = icmp eq i8* %9, null
%11 = getelementptr inbounds i8, i8* %9, i64 1
%12 = select i1 %10, i8* %5, i8* %11
%13 = ptrtoint i8* %12 to i64
%14 = ptrtoint i8* %5 to i64
%15 = sub i64 %14, %13
%16 = trunc i64 %15 to i32
%17 = add i32 %3, %16
%18 = call i32 @snprintf(i8* %12, i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @snprintf(i8* %6, i32 %3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = call i32 @setenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %6, i32 1)
%22 = load i32, i32* @LOG_PID
%23 = load i32, i32* @LOG_NDELAY
%24 = or i32 %23, %22
%25 = load i32, i32* @LOG_SYSLOG
%26 = call i32 @openlog(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* @X_OK
%28 = call i32 @access(i8* %6, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %2
%31 = load i32, i32* @LOG_NOTICE
%32 = call i32 @syslog(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%33 = call i32 (...) @closelog()
%34 = call i32 @execv(i8* %6, i8** %1)
%35 = load i32, i32* @LOG_PID
%36 = load i32, i32* @LOG_NDELAY
%37 = or i32 %36, %35
%38 = load i32, i32* @LOG_SYSLOG
%39 = call i32 @openlog(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %37, i32 %38)
%40 = load i32, i32* @LOG_NOTICE
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @syslog(i32 %40, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %30, %2
%45 = load i32, i32* @LOG_NOTICE
%46 = call i32 @syslog(i32 %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %5)
%47 = call i32 (...) @closelog()
%48 = call i32 @execv(i8* %5, i8** %1)
ret i32 0
}