#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 pollfd {
  int fd;
  int /*<<< orphan*/ revents;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ POLLOUT;
int /*<<< orphan*/ RNDADDTOENTCNT;
int /*<<< orphan*/ RNDGETENTCNT;
scalar_t__ geteuid();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
scalar_t__ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  if (geteuid()) {
    printf("You are not root. Go away.\n");
    return 1;
  }
  int have;
  int add = 4096;
  int d = open("/dev/random", O_WRONLY);
  struct pollfd poll_data = {
      .fd = d,
      .events = POLLOUT,
      .revents = 0,
  };
  printf("breakmycryptod: Your system is now INSECURE!!!\n");
  while (poll(&poll_data, 1, -1) > 0) {
    ioctl(d, RNDGETENTCNT, &have);
    printf("breakmycryptod: Entropy estimate fell to %d, replenishing.\n",
           have);
    ioctl(d, RNDADDTOENTCNT, &add);
  }
  printf("breakmycryptod: An error occurred, exiting.\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    $0x0,%rax
je     118a <main+0x3a>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1242 <main+0xf2>
movl   $0x1000,-0xc(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
cmp    $0x0,%rax
jle    122d <main+0xdd>
mov    -0x10(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x8(%rbp),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdx
call   1050 <ioctl@plt>
jmp    11d0 <main+0x80>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %rax,%rax
je     1186 <main+0x26>
lea    0xf22(%rip),%rdi
jmp    124c <main+0xec>
movl   $0x1000,(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    %eax,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
test   %rax,%rax
jle    1245 <main+0xe5>
lea    0x4(%rsp),%r14
lea    0xe1e(%rip),%r15
mov    0x2dcb(%rip),%rbp
mov    %rsp,%r12
lea    0x8(%rsp),%r13
nopl   (%rax)
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
mov    0x4(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
test   %rax,%rax
jg     1200 <main+0xa0>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [28 x i8] c"You are not root. Go away.\0A\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_WRONLY = global i32 0
@POLLOUT = global i32 0
@.str.2 = [48 x i8] c"breakmycryptod: Your system is now INSECURE!!!\0A\00"
@RNDGETENTCNT = global i32 0
@.str.3 = [60 x i8] c"breakmycryptod: Entropy estimate fell to %d, replenishing.\0A\00"
@RNDADDTOENTCNT = global i32 0
@.str.4 = [45 x i8] c"breakmycryptod: An error occurred, exiting.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.pollfd
store i32 0, i32* %1
%6 = call i64 (...) @geteuid()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %33
10: ; preds = %0
store i32 4096, i32* %3
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i32 0, i32 0
%14 = load i32, i32* %4
store i32 %14, i32* %13
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i32 0, i32 1
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i32 0, i32 2
%17 = load i32, i32* @POLLOUT
store i32 %17, i32* %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %22, %10
%20 = call i64 @poll(%struct.pollfd* %5, i32 1, i32 -1)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = load i32, i32* %4
%24 = load i32, i32* @RNDGETENTCNT
%25 = call i32 @ioctl(i32 %23, i32 %24, i32* %2)
%26 = load i32, i32* %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %4
%29 = load i32, i32* @RNDADDTOENTCNT
%30 = call i32 @ioctl(i32 %28, i32 %29, i32* %3)
br label %19
31: ; preds = %19
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %33
33: ; preds = %31, %8
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str.1 = [12 x i8] c"/dev/random\00"
@O_WRONLY = global i32 0
@POLLOUT = global i32 0
@RNDGETENTCNT = global i32 0
@.str.3 = [60 x i8] c"breakmycryptod: Entropy estimate fell to %d, replenishing.\0A\00"
@RNDADDTOENTCNT = global i32 0
@str = [47 x i8] c"breakmycryptod: Your system is now INSECURE!!!\00"
@str.5 = [44 x i8] c"breakmycryptod: An error occurred, exiting.\00"
@str.6 = [27 x i8] c"You are not root. Go away.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.pollfd
%4 = tail call i64 (...) @geteuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
br label %32
8: ; preds = %0
%9 = bitcast i32* %1 to i8*
%10 = bitcast i32* %2 to i8*
store i32 4096, i32* %2
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = bitcast %struct.pollfd* %3 to i8*
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %12, i32* %14
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
%17 = load i32, i32* @POLLOUT
store i32 %17, i32* %16
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%19 = call i64 @poll(%struct.pollfd* %3, i32 1, i32 -1)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %8, %21
%22 = load i32, i32* @RNDGETENTCNT
%23 = call i32 @ioctl(i32 %12, i32 %22, i32* %1)
%24 = load i32, i32* %1
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @RNDADDTOENTCNT
%27 = call i32 @ioctl(i32 %12, i32 %26, i32* %2)
%28 = call i64 @poll(%struct.pollfd* %3, i32 1, i32 -1)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %21, label %30
30: ; preds = %21, %8
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %6
ret i32 1
}