#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*/ key;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ geteuid();
int /*<<< orphan*/ mkdir(char *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int main(void) {
  int authkey_fd;
  int random_fd;
  unsigned char key[128];
  int res;
  printf("OpenAIS Authentication key generator.\n");
  if (geteuid() != 0) {
    printf("Error: Authorization key must be generated as root user.\n");
    exit(1);
  }
  mkdir("/etc/ais", 0700);
  printf("Gathering %lu bits for key from /dev/random.\n",
         (unsigned long)(sizeof(key) * 8));
  random_fd = open("/dev/random", O_RDONLY);
  if (random_fd == -1) {
    perror("Is /dev/random present? Opening /dev/random");
    exit(1);
  }
  res = read(random_fd, key, sizeof(key));
  if (res == -1) {
    perror("Could not read /dev/random");
    exit(1);
  }
  authkey_fd = open("/etc/ais/authkey", O_CREAT | O_WRONLY);
  if (authkey_fd == -1) {
    perror("Could not create /etc/ais/authkey");
    exit(1);
  }
  fchown(authkey_fd, 0, 0);
  fchmod(authkey_fd, 0400);
  printf("Writing openais key to /etc/ais/authkey.\n");
  res = write(authkey_fd, key, sizeof(key));
  if (res == -1) {
    perror("Could not write /etc/ais/authkey");
    exit(1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%rax
je     11e9 <main+0x49>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xe71(%rip),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
lea    0xe69(%rip),%rdi
mov    $0x400,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1245 <main+0xa5>
lea    0xe62(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    1282 <main+0xe2>
lea    0xe51(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    0x2d36(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12c3 <main+0x123>
lea    0xe2b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10a0 <fchown@plt>
mov    -0x8(%rbp),%edi
mov    $0x100,%esi
call   1080 <fchmod@plt>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1040 <write@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    1327 <main+0x187>
lea    0xe13(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x80,%rsp
lea    0xf02(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <geteuid@plt>
test   %rax,%rax
jne    1295 <main+0xe5>
lea    0xe25(%rip),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
lea    0xe1d(%rip),%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12ab <main+0xfb>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x104>
mov    0x2dc3(%rip),%rax
mov    0x2d8c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe48(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12bd <main+0x10d>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <fchown@plt>
mov    %ebx,%edi
mov    $0x100,%esi
call   1090 <fchmod@plt>
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x80,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     12c6 <main+0x116>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
ret
lea    0xe74(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd85(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xda8(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xdba(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xdd3(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [39 x i8] c"OpenAIS Authentication key generator.\0A\00"
@.str.1 = [58 x i8] c"Error: Authorization key must be generated as root user.\0A\00"
@.str.2 = [9 x i8] c"/etc/ais\00"
@.str.3 = [46 x i8] c"Gathering %lu bits for key from /dev/random.\0A\00"
@.str.4 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.5 = [44 x i8] c"Is /dev/random present? Opening /dev/random\00"
@.str.6 = [27 x i8] c"Could not read /dev/random\00"
@.str.7 = [17 x i8] c"/etc/ais/authkey\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.8 = [34 x i8] c"Could not create /etc/ais/authkey\00"
@.str.9 = [42 x i8] c"Writing openais key to /etc/ais/authkey.\0A\00"
@.str.10 = [33 x i8] c"Could not write /etc/ais/authkey\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%7 = call i64 (...) @geteuid()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 @mkdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 448)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i64 1024)
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = load i32, i32* %3
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 128)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %22
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* @O_WRONLY
%34 = or i32 %32, %33
%35 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %34)
store i32 %35, i32* %2
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %31
%42 = load i32, i32* %2
%43 = call i32 @fchown(i32 %42, i32 0, i32 0)
%44 = load i32, i32* %2
%45 = call i32 @fchmod(i32 %44, i32 256)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.9, i64 0, i64 0))
%47 = load i32, i32* %2
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%49 = call i32 @write(i32 %47, i8* %48, i32 128)
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %41
%53 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.10, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %41
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [9 x i8] c"/etc/ais\00"
@.str.3 = [46 x i8] c"Gathering %lu bits for key from /dev/random.\0A\00"
@.str.4 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.5 = [44 x i8] c"Is /dev/random present? Opening /dev/random\00"
@.str.6 = [27 x i8] c"Could not read /dev/random\00"
@.str.7 = [17 x i8] c"/etc/ais/authkey\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.8 = [34 x i8] c"Could not create /etc/ais/authkey\00"
@.str.10 = [33 x i8] c"Could not write /etc/ais/authkey\00"
@str = [38 x i8] c"OpenAIS Authentication key generator.\00"
@str.11 = [41 x i8] c"Writing openais key to /etc/ais/authkey.\00"
@str.12 = [57 x i8] c"Error: Authorization key must be generated as root user.\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%4 = tail call i64 (...) @geteuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.12, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i32 @mkdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 448)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i64 1024)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = call i32 @read(i32 %13, i8* %2, i32 128)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = load i32, i32* @O_CREAT
%26 = load i32, i32* @O_WRONLY
%27 = or i32 %26, %25
%28 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %24
%34 = tail call i32 @fchown(i32 %28, i32 0, i32 0)
%35 = tail call i32 @fchmod(i32 %28, i32 256)
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.11, i64 0, i64 0))
%37 = call i32 @write(i32 %28, i8* %2, i32 128)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.10, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %33
ret i32 0
}