#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 *);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
int system(char *);
int main(int argc, char **argv) {
  setuid(0);
  if (argc != 2) {
    printf("usage: %s /device/to/umount\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;
  char cmd1[cmd1_len];
  sprintf(cmd1, "/bin/umount /dev/mapper/luks_%s", 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;
  char cmd2[cmd2_len];
  sprintf(cmd2, "/sbin/cryptsetup luksClose luks_%s", name);
  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   $0x2,-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    1317 <main+0x197>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
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
add    $0x20,%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    -0x28(%rbp),%rdx
lea    0xdef(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x60(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1278 <main+0xf8>
mov    -0x60(%rbp),%rdx
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%ecx
lea    0xddd(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1310 <main+0x190>
mov    -0x20(%rbp),%rax
add    $0x20,%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
lea    0xdbb(%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     1302 <main+0x182>
mov    -0x68(%rbp),%rdx
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%ecx
lea    0xd53(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1310 <main+0x190>
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   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0x2,%ebx
jne    1210 <main+0x90>
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    -0x20(%rbp),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1080 <base64_encode@plt>
mov    %rax,%r14
mov    %rsp,%r15
mov    -0x20(%rbp),%rax
mov    %rsp,%rbx
add    $0x2f,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe45(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
test   %eax,%eax
je     1226 <main+0xa6>
mov    0x2dea(%rip),%rcx
mov    (%rcx),%edi
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1070 <fprintf@plt>
mov    $0x3,%eax
jmp    1284 <main+0x104>
mov    (%r14),%rsi
lea    0xde6(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%eax
jmp    1287 <main+0x107>
mov    -0x20(%rbp),%rax
mov    %rsp,%rbx
add    $0x2f,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe28(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
test   %eax,%eax
je     1282 <main+0x102>
mov    0x2d78(%rip),%rcx
mov    (%rcx),%edi
lea    0xdcc(%rip),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1070 <fprintf@plt>
mov    $0x2,%eax
jmp    1284 <main+0x104>
xor    %eax,%eax
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"usage: %s /device/to/umount\0A\00"
@.str.1 = [32 x i8] c"/bin/umount /dev/mapper/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 = [35 x i8] c"/sbin/cryptsetup luksClose luks_%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, 2
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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %67
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strlen(i8* %27)
store i64 %28, i64* %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i64, i64* %6
%33 = call i8* @base64_encode(i8* %31, i64 %32, i64* %7)
store i8* %33, i8** %8
%34 = load i64, i64* %7
%35 = add i64 32, %34
store i64 %35, i64* %9
%36 = load i64, i64* %9
%37 = call i8* @llvm.stacksave()
store i8* %37, i8** %10
%38 = alloca i8, i64 %36
store i64 %36, i64* %11
%39 = load i8*, i8** %8
%40 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = call i32 @system(i8* %38)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %24
%45 = load i32, i32* @stderr
%46 = load i32, i32* %12
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %46)
store i32 3, i32* %3
store i32 1, i32* %13
br label %65
48: ; preds = %24
%49 = load i64, i64* %7
%50 = add i64 32, %49
store i64 %50, i64* %14
%51 = load i64, i64* %14
%52 = alloca i8, i64 %51
store i64 %51, i64* %15
%53 = load i8*, i8** %8
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i8*, i8** %8
%56 = call i32 @free(i8* %55)
%57 = call i32 @system(i8* %52)
store i32 %57, i32* %12
%58 = load i32, i32* %12
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %48
%61 = load i32, i32* @stderr
%62 = load i32, i32* %12
%63 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %52, i32 %62)
store i32 2, i32* %3
store i32 1, i32* %13
br label %65
64: ; preds = %48
store i32 0, i32* %3
store i32 1, i32* %13
br label %65
65: ; preds = %64, %60, %44
%66 = load i8*, i8** %10
br label %67
67: ; preds = %65, %19
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [29 x i8] c"usage: %s /device/to/umount\0A\00"
@.str.1 = [32 x i8] c"/bin/umount /dev/mapper/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 = [35 x i8] c"/sbin/cryptsetup luksClose luks_%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, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %38
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%13 = bitcast i64* %3 to i8*
%14 = call i8* @base64_encode(i8* %11, i64 %12, i64* %3)
%15 = load i64, i64* %3
%16 = add i64 %15, 32
%17 = call i8* @llvm.stacksave()
%18 = alloca i8, i64 %16
%19 = call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%20 = call i32 @system(i8* %18)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %9
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %18, i32 %20)
br label %36
25: ; preds = %9
%26 = load i64, i64* %3
%27 = add i64 %26, 32
%28 = alloca i8, i64 %27
%29 = call i32 @sprintf(i8* %28, i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%30 = call i32 @free(i8* %14)
%31 = call i32 @system(i8* %28)
%32 = icmp eq i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %25
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %28, i32 %31)
br label %36
36: ; preds = %33, %25, %22
%37 = phi i32 [ 3, %22 ], [ 2, %33 ], [ 0, %25 ]
br label %38
38: ; preds = %36, %6
%39 = phi i32 [ 1, %6 ], [ %37, %36 ]
ret i32 %39
}