#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*/ LOG_ERR;
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
char *mpk_filehandling_basename(char const *);
scalar_t__ mpk_package_unpackmpk(char const *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int mpk_unpack(const char *package_file, const char *dst_dir) {
  char *outdir_name, *package_fname;
  if (!package_file || !dst_dir)
    return -1;
  if (!(package_fname = mpk_filehandling_basename(package_file)))
    return -1;
  if (!(outdir_name =
            malloc(strlen(dst_dir) + 1 + strlen(package_fname) + 1))) {
    free(package_fname);
    return -1;
  }
  sprintf(outdir_name, "%s/%s", dst_dir, package_fname);
  free(package_fname);
  package_fname = NULL;
  if (mpk_package_unpackmpk(package_file, outdir_name) != 0) {
    syslog(LOG_ERR, "mpk_package_unpackmpk() failed");
    free(outdir_name);
    return -1;
  }
  free(outdir_name);
  return 0;
}


x86 assembly (clang, -O0):
<mpk_unpack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1196 <mpk_unpack+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    11a2 <mpk_unpack+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <mpk_unpack+0x11c>
mov    -0x10(%rbp),%rdi
call   1050 <mpk_filehandling_basename@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c5 <mpk_unpack+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <mpk_unpack+0x11c>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1212 <mpk_unpack+0xa2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <mpk_unpack+0x11c>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xddb(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <mpk_package_unpackmpk@plt>
cmp    $0x0,%rax
je     127c <mpk_unpack+0x10c>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
call   1070 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <mpk_unpack+0x11c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpk_unpack>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     1229 <mpk_unpack+0xb9>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1229 <mpk_unpack+0xb9>
mov    %rdi,%rbx
call   1050 <mpk_filehandling_basename@plt>
test   %rax,%rax
je     1229 <mpk_unpack+0xb9>
mov    %rax,%r12
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1219 <mpk_unpack+0xa9>
mov    %rax,%r14
lea    0xe24(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1090 <sprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <mpk_package_unpackmpk@plt>
test   %rax,%rax
je     121e <mpk_unpack+0xae>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1070 <syslog@plt>
jmp    1221 <mpk_unpack+0xb1>
mov    %r12,%r14
jmp    1221 <mpk_unpack+0xb1>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@LOG_ERR = global i32 0
@.str.1 = [31 x i8] c"mpk_package_unpackmpk() failed\00"
define i32 @mpk_unpack(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %10, %2
store i32 -1, i32* %3
br label %51
14: ; preds = %10
%15 = load i8*, i8** %4
%16 = call i8* @mpk_filehandling_basename(i8* %15)
store i8* %16, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
store i32 -1, i32* %3
br label %51
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = call i64 @strlen(i8* %20)
%22 = add nsw i64 %21, 1
%23 = load i8*, i8** %7
%24 = call i64 @strlen(i8* %23)
%25 = add nsw i64 %22, %24
%26 = add nsw i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %19
%30 = load i8*, i8** %7
%31 = call i32 @free(i8* %30)
store i32 -1, i32* %3
br label %51
32: ; preds = %19
%33 = load i8*, i8** %6
%34 = load i8*, i8** %5
%35 = load i8*, i8** %7
%36 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %35)
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
store i8* null, i8** %7
%39 = load i8*, i8** %4
%40 = load i8*, i8** %6
%41 = call i64 @mpk_package_unpackmpk(i8* %39, i8* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %32
%44 = load i32, i32* @LOG_ERR
%45 = call i32 @syslog(i32 %44, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
store i32 -1, i32* %3
br label %51
48: ; preds = %32
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
store i32 0, i32* %3
br label %51
51: ; preds = %48, %43, %29, %18, %13
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@LOG_ERR = global i32 0
@.str.1 = [31 x i8] c"mpk_package_unpackmpk() failed\00"
define i32 @mpk_unpack(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %28
6: ; preds = %2
%7 = tail call i8* @mpk_filehandling_basename(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %28, label %9
9: ; preds = %6
%10 = tail call i64 @strlen(i8* %1)
%11 = tail call i64 @strlen(i8* %7)
%12 = add i64 %10, 2
%13 = add i64 %12, %11
%14 = tail call i8* @malloc(i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %24, label %16
16: ; preds = %9
%17 = tail call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %7)
%18 = tail call i32 @free(i8* %7)
%19 = tail call i64 @mpk_package_unpackmpk(i8* %0, i8* %14)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @LOG_ERR
%23 = tail call i32 @syslog(i32 %22, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %16, %9, %21
%25 = phi i8* [ %14, %21 ], [ %7, %9 ], [ %14, %16 ]
%26 = phi i32 [ -1, %21 ], [ -1, %9 ], [ 0, %16 ]
%27 = tail call i32 @free(i8* %25)
br label %28
28: ; preds = %24, %6, %2
%29 = phi i32 [ -1, %2 ], [ -1, %6 ], [ %26, %24 ]
ret i32 %29
}