#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 */
char *base64_encode(char *, size_t, size_t *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int system(char *);
int main(int argc, char **argv) {
  setuid(0);
  if (argc != 3) {
    printf("usage: %s /device/to/mount /mount/point\n", argv[0]);
    return 1;
  }
  size_t argv1_len = strlen(argv[1]);
  size_t name_len;
  char *name = base64_encode(argv[1], argv1_len, &name_len);
  size_t cmd1_len = 32 + name_len + argv1_len;
  char cmd1[cmd1_len];
  sprintf(cmd1, "/sbin/cryptsetup luksOpen '%s' luks_%s", argv[1], name);
  int ret = system(cmd1);
  if (ret != 0) {
    fprintf(stderr, "the command %s returned a nonzero value: %d\n", cmd1, ret);
    return 3;
  }
  size_t cmd2_len = 32 + name_len + strlen(argv[2]);
  char cmd2[cmd2_len];
  sprintf(cmd2, "/bin/mount /dev/mapper/luks_%s '%s'", name, argv[2]);
  free(name);
  ret = system(cmd2);
  if (ret != 0) {
    fprintf(stderr, "the command %s returned a nonzero value: %d\n", cmd2, ret);
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   10a0 <setuid@plt>
cmpl   $0x3,-0x8(%rbp)
je     11c6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4b(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1349 <main+0x1c9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <base64_encode@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
lea    0x20(%rax,%rcx,1),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdec(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x60(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1287 <main+0x107>
mov    -0x60(%rbp),%rdx
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%ecx
lea    0xde1(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1342 <main+0x1c2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
movslq %ecx,%rcx
lea    0x20(%rax,%rcx,1),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xd9c(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x68(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1334 <main+0x1b4>
mov    -0x68(%rbp),%rdx
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%ecx
lea    0xd34(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1342 <main+0x1c2>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x38(%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,%r12
mov    %edi,%ebx
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0x3,%ebx
jne    1226 <main+0xa6>
mov    0x8(%r12),%rdi
call   1040 <strlen@plt>
movslq %eax,%rbx
mov    0x8(%r12),%rdi
lea    -0x30(%rbp),%rdx
mov    %rbx,%rsi
call   1080 <base64_encode@plt>
mov    %rax,%r14
mov    %rsp,%r13
mov    -0x30(%rbp),%rax
add    %rbx,%rax
add    $0x2f,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x8(%r12),%rdx
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rcx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
test   %eax,%eax
je     123d <main+0xbd>
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%edi
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1070 <fprintf@plt>
mov    $0x3,%eax
jmp    12af <main+0x12f>
mov    (%r12),%rsi
lea    0xdcf(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%eax
jmp    12b2 <main+0x132>
mov    -0x30(%rbp),%rbx
mov    0x10(%r12),%rdi
call   1040 <strlen@plt>
cltq
add    %rbx,%rax
add    $0x2f,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    0x10(%r12),%rcx
lea    0xe10(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <system@plt>
test   %eax,%eax
je     12ad <main+0x12d>
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%edi
lea    0xdb4(%rip),%rsi
mov    %r15,%rdx
mov    %eax,%ecx
call   1070 <fprintf@plt>
mov    $0x2,%eax
jmp    12af <main+0x12f>
xor    %eax,%eax
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"usage: %s /device/to/mount /mount/point\0A\00"
@.str.1 = [39 x i8] c"/sbin/cryptsetup luksOpen '%s' luks_%s\00"
@stderr = global i32 0
@.str.2 = [45 x i8] c"the command %s returned a nonzero value: %d\0A\00"
@.str.3 = [36 x i8] c"/bin/mount /dev/mapper/luks_%s '%s'\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = call i32 @setuid(i32 0)
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 3
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %82
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @strlen(i8* %27)
%29 = sext i32 %28 to i64
store i64 %29, i64* %6
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i64, i64* %6
%34 = call i8* @base64_encode(i8* %32, i64 %33, i64* %7)
store i8* %34, i8** %8
%35 = load i64, i64* %7
%36 = add i64 32, %35
%37 = load i64, i64* %6
%38 = add i64 %36, %37
store i64 %38, i64* %9
%39 = load i64, i64* %9
%40 = call i8* @llvm.stacksave()
store i8* %40, i8** %10
%41 = alloca i8, i64 %39
store i64 %39, i64* %11
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = load i8*, i8** %8
%46 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %44, i8* %45)
%47 = call i32 @system(i8* %41)
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %24
%51 = load i32, i32* @stderr
%52 = load i32, i32* %12
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 %52)
store i32 3, i32* %3
store i32 1, i32* %13
br label %80
54: ; preds = %24
%55 = load i64, i64* %7
%56 = add i64 32, %55
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 2
%59 = load i8*, i8** %58
%60 = call i32 @strlen(i8* %59)
%61 = sext i32 %60 to i64
%62 = add i64 %56, %61
store i64 %62, i64* %14
%63 = load i64, i64* %14
%64 = alloca i8, i64 %63
store i64 %63, i64* %15
%65 = load i8*, i8** %8
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 2
%68 = load i8*, i8** %67
%69 = call i32 @sprintf(i8* %64, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %65, i8* %68)
%70 = load i8*, i8** %8
%71 = call i32 @free(i8* %70)
%72 = call i32 @system(i8* %64)
store i32 %72, i32* %12
%73 = load i32, i32* %12
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %54
%76 = load i32, i32* @stderr
%77 = load i32, i32* %12
%78 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %64, i32 %77)
store i32 2, i32* %3
store i32 1, i32* %13
br label %80
79: ; preds = %54
store i32 0, i32* %3
store i32 1, i32* %13
br label %80
80: ; preds = %79, %75, %50
%81 = load i8*, i8** %10
br label %82
82: ; preds = %80, %19
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str = [41 x i8] c"usage: %s /device/to/mount /mount/point\0A\00"
@.str.1 = [39 x i8] c"/sbin/cryptsetup luksOpen '%s' luks_%s\00"
@stderr = global i32 0
@.str.2 = [45 x i8] c"the command %s returned a nonzero value: %d\0A\00"
@.str.3 = [36 x i8] c"/bin/mount /dev/mapper/luks_%s '%s'\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = tail call i32 @setuid(i32 0)
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %48
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = bitcast i64* %3 to i8*
%15 = load i8*, i8** %10
%16 = call i8* @base64_encode(i8* %15, i64 %13, i64* %3)
%17 = load i64, i64* %3
%18 = add nsw i64 %13, 32
%19 = add i64 %18, %17
%20 = call i8* @llvm.stacksave()
%21 = alloca i8, i64 %19
%22 = load i8*, i8** %10
%23 = call i32 @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %16)
%24 = call i32 @system(i8* %21)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %9
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %21, i32 %24)
br label %46
29: ; preds = %9
%30 = load i64, i64* %3
%31 = add i64 %30, 32
%32 = getelementptr inbounds i8*, i8** %1, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @strlen(i8* %33)
%35 = sext i32 %34 to i64
%36 = add i64 %31, %35
%37 = alloca i8, i64 %36
%38 = load i8*, i8** %32
%39 = call i32 @sprintf(i8* %37, i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %16, i8* %38)
%40 = call i32 @free(i8* %16)
%41 = call i32 @system(i8* %37)
%42 = icmp eq i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %29
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 %41)
br label %46
46: ; preds = %43, %29, %26
%47 = phi i32 [ 3, %26 ], [ 2, %43 ], [ 0, %29 ]
br label %48
48: ; preds = %46, %6
%49 = phi i32 [ 1, %6 ], [ %47, %46 ]
ret i32 %49
}