#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 */
struct option {
  char *member_0;
  char member_3;
  int /*<<< orphan*/ *member_2;
  int /*<<< orphan*/ member_1;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ cadir;

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ execve(char *, char *const *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ geteuid();
int getopt_long(int, char **, char *, struct option *, int /*<<< orphan*/ *);
#define no_argument 128
size_t readlink(char *, char *, int);
int send_ca();
int setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[], char *envp[]) {
  int ch;
  char cadir[1024];
  ssize_t err = readlink("/proc/self/cwd", cadir, sizeof(cadir) - 1);
  if (err < 0) {
    fprintf(stderr, "Couldn't find executable path: %m\n. exiting");
    exit(1);
  }
  cadir[err] = '\0';
  if (chdir(cadir) != 0) {
    fprintf(stderr, "Unable to chdir(%s)\n", cadir);
    exit(1);
  }
  static struct option longopts[] = {{"get-ca", no_argument, NULL, 'c'},
                                     {NULL, 0, NULL, 0}};
  while ((ch = getopt_long(argc, argv, "c", longopts, NULL)) != -1) {
    switch (ch) {
    case 'c':
      exit(send_ca());
    }
  }
  if (-1 == setreuid(geteuid(), geteuid())) {
    fprintf(stderr, "Failed setreuid(): %m");
    exit(1);
  }
  char *const new_argv[] = {"/usr/bin/openssl", "ca",  "-batch",     "-config",
                            "ca-ssl.conf",      "-in", "/dev/stdin", "-out",
                            "/dev/stdout",      NULL};
  execve("/usr/bin/openssl", new_argv, envp);
  fprintf(stderr, "Error execing openssl\n");
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x420(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x3ff,%edx
call   1030 <readlink@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jae    120b <main+0x6b>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x428(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1258 <main+0xb8>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    125d <main+0xbd>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xdec(%rip),%rdx
lea    0x2e0e(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <getopt_long@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     12ac <main+0x10c>
mov    -0x1c(%rbp),%eax
sub    $0x63,%eax
jne    12a7 <main+0x107>
jmp    1299 <main+0xf9>
mov    $0x0,%al
call   10b0 <send_ca@plt>
mov    %eax,%edi
call   10c0 <exit@plt>
jmp    125d <main+0xbd>
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,-0x484(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    -0x484(%rbp),%edi
mov    %eax,%esi
call   10a0 <setreuid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12fd <main+0x15d>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x480(%rbp),%rdi
lea    0x2ab5(%rip),%rsi
mov    $0x50,%edx
call   1090 <memcpy@plt>
lea    -0x480(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd48(%rip),%rdi
call   1070 <execve@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x450,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe52(%rip),%rdi
lea    0x50(%rsp),%r15
mov    %r15,%rsi
mov    $0x3ff,%edx
call   1030 <readlink@plt>
movb   $0x0,0x50(%rsp,%rax,1)
mov    %r15,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    120b <main+0x7b>
lea    0xe52(%rip),%r15
lea    0x2e90(%rip),%r12
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1050 <getopt_long@plt>
cmp    $0xffffffff,%eax
je     1231 <main+0xa1>
cmp    $0x63,%eax
jne    11e0 <main+0x50>
xor    %eax,%eax
call   10a0 <send_ca@plt>
mov    %eax,%edi
call   10b0 <exit@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
lea    0x50(%rsp),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1090 <setreuid@plt>
cmp    $0xffffffff,%eax
jne    1261 <main+0xd1>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
jmp    12be <main+0x12e>
movaps 0x2b98(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0x2b7c(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0x2b60(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0x2b44(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0x2b28(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xda0(%rip),%rdi
mov    %rsp,%rsi
mov    %r14,%rdx
call   1070 <execve@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.option = type { i8*, i8, i32*, i32 }
@.str = [15 x i8] c"/proc/self/cwd\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"Couldn't find executable path: %m\0A. exiting\00"
@.str.2 = [21 x i8] c"Unable to chdir(%s)\0A\00"
@main.longopts = internal global [2 x %struct.option] [%struct.option { i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i32 0, i32 0), i8 -128, i32* null, i32 99 }, %struct.option zeroinitializer]
@.str.3 = [7 x i8] c"get-ca\00"
@.str.4 = [2 x i8] c"c\00"
@.str.5 = [22 x i8] c"Failed setreuid(): %m\00"
@.str.6 = [17 x i8] c"/usr/bin/openssl\00"
@.str.7 = [3 x i8] c"ca\00"
@.str.8 = [7 x i8] c"-batch\00"
@.str.9 = [8 x i8] c"-config\00"
@.str.10 = [12 x i8] c"ca-ssl.conf\00"
@.str.11 = [4 x i8] c"-in\00"
@.str.12 = [11 x i8] c"/dev/stdin\00"
@.str.13 = [5 x i8] c"-out\00"
@.str.14 = [12 x i8] c"/dev/stdout\00"
@__const.main.new_argv = [10 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.14, i32 0, i32 0), i8* null]
@.str.15 = [23 x i8] c"Error execing openssl\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i64
%11 = alloca [10 x i8*]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%13 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %12, i32 1023)
store i64 %13, i64* %10
%14 = load i64, i64* %10
%15 = icmp ult i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i64, i64* %10
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%24 = call i64 @chdir(i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
br label %32
32: ; preds = %42, %31
%33 = load i32, i32* %5
%34 = load i8**, i8*** %6
%35 = call i32 @getopt_long(i32 %33, i8** %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %struct.option* getelementptr inbounds ([2 x %struct.option], [2 x %struct.option]* @main.longopts, i64 0, i64 0), i32* null)
store i32 %35, i32* %8
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = load i32, i32* %8
switch i32 %38, label %42 [
i32 99, label %39
]
39: ; preds = %37
%40 = call i32 (...) @send_ca()
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %37
br label %32
43: ; preds = %32
%44 = call i32 (...) @geteuid()
%45 = call i32 (...) @geteuid()
%46 = call i32 @setreuid(i32 %44, i32 %45)
%47 = icmp eq i32 -1, %46
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %43
%53 = bitcast [10 x i8*]* %11 to i8*
%54 = getelementptr inbounds [10 x i8*], [10 x i8*]* %11, i64 0, i64 0
%55 = load i8**, i8*** %7
%56 = call i32 @execve(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8** %54, i8** %55)
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.15, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.option = type { i8*, i8, i32*, i32 }
@.str = [15 x i8] c"/proc/self/cwd\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"Unable to chdir(%s)\0A\00"
@main.longopts = internal global [2 x %struct.option] [%struct.option { i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i32 0, i32 0), i8 -128, i32* null, i32 99 }, %struct.option zeroinitializer]
@.str.3 = [7 x i8] c"get-ca\00"
@.str.4 = [2 x i8] c"c\00"
@.str.5 = [22 x i8] c"Failed setreuid(): %m\00"
@.str.6 = [17 x i8] c"/usr/bin/openssl\00"
@.str.7 = [3 x i8] c"ca\00"
@.str.8 = [7 x i8] c"-batch\00"
@.str.9 = [8 x i8] c"-config\00"
@.str.10 = [12 x i8] c"ca-ssl.conf\00"
@.str.11 = [4 x i8] c"-in\00"
@.str.12 = [11 x i8] c"/dev/stdin\00"
@.str.13 = [5 x i8] c"-out\00"
@.str.14 = [12 x i8] c"/dev/stdout\00"
@__const.main.new_argv = [10 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.14, i32 0, i32 0), i8* null]
@.str.15 = [23 x i8] c"Error execing openssl\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca [1024 x i8]
%5 = alloca [10 x i8*]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6, i32 1023)
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %7
store i8 0, i8* %8
%9 = call i64 @chdir(i8* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %3, %15
%16 = call i32 @getopt_long(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %struct.option* getelementptr inbounds ([2 x %struct.option], [2 x %struct.option]* @main.longopts, i64 0, i64 0), i32* null)
switch i32 %16, label %15 [
i32 -1, label %20
i32 99, label %17
]
17: ; preds = %15
%18 = call i32 (...) @send_ca()
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %15
%21 = call i32 (...) @geteuid()
%22 = call i32 (...) @geteuid()
%23 = call i32 @setreuid(i32 %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = bitcast [10 x i8*]* %5 to i8*
%31 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
%32 = call i32 @execve(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8** %31, i8** %2)
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.15, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
}