#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 uint8_t;
typedef int ssize_t;

/* Variables and functions */
ssize_t mygetrandom(void *pp, size_t ss, unsigned int flags) {
  ssize_t rc = sizeof(uint8_t);
  uint8_t *bb = (uint8_t *)pp;
  static int state = 0;
  if (ss == 0) {
    rc = -1;
  } else {
    switch (state) {
    case 0:
      *bb = 0x11;
      state = (state + 1) % 4;
      break;
    case 1:
      *bb = 0x22;
      state = (state + 1) % 4;
      break;
    case 2:
      *bb = 0x33;
      state = (state + 1) % 4;
      break;
    case 3:
      *bb = 0x44;
      state = (state + 1) % 4;
      break;
    default:
      rc = 0;
      break;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<mygetrandom>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x4,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1135 <mygetrandom+0x35>
movl   $0xffffffff,-0x18(%rbp)
jmp    1201 <mygetrandom+0x101>
mov    0x2ee9(%rip),%eax
mov    %rax,-0x28(%rbp)
sub    $0x3,%rax
ja     11f5 <mygetrandom+0xf5>
mov    -0x28(%rbp),%rax
lea    0xeac(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x20(%rbp),%rax
movl   $0x11,(%rax)
mov    0x2eb7(%rip),%eax
add    $0x1,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,0x2ea6(%rip)
jmp    11fc <mygetrandom+0xfc>
mov    -0x20(%rbp),%rax
movl   $0x22,(%rax)
mov    0x2e91(%rip),%eax
add    $0x1,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,0x2e80(%rip)
jmp    11fc <mygetrandom+0xfc>
mov    -0x20(%rbp),%rax
movl   $0x33,(%rax)
mov    0x2e6b(%rip),%eax
add    $0x1,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,0x2e5a(%rip)
jmp    11fc <mygetrandom+0xfc>
mov    -0x20(%rbp),%rax
movl   $0x44,(%rax)
mov    0x2e45(%rip),%eax
add    $0x1,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,0x2e34(%rip)
jmp    11fc <mygetrandom+0xfc>
movl   $0x0,-0x18(%rbp)
jmp    1201 <mygetrandom+0x101>
mov    -0x18(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<mygetrandom>:
test   %rsi,%rsi
je     1136 <mygetrandom+0x36>
movslq 0x2f18(%rip),%rcx
xor    %eax,%eax
cmp    $0x3,%rcx
ja     1135 <mygetrandom+0x35>
mov    %ecx,%eax
shl    $0x4,%eax
add    %ecx,%eax
add    $0x11,%eax
lea    0xedb(%rip),%rdx
mov    (%rdx,%rcx,4),%ecx
mov    %eax,(%rdi)
mov    %ecx,0x2ef4(%rip)
mov    $0x4,%eax
ret
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
@mygetrandom.state = internal global i32 0
define i32 @mygetrandom(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i32 4, i32* %7
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %8
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %7
br label %38
14: ; preds = %3
%15 = load i32, i32* @mygetrandom.state
switch i32 %15, label %36 [
i32 0, label %16
i32 1, label %21
i32 2, label %26
i32 3, label %31
]
16: ; preds = %14
%17 = load i32*, i32** %8
store i32 17, i32* %17
%18 = load i32, i32* @mygetrandom.state
%19 = add nsw i32 %18, 1
%20 = srem i32 %19, 4
store i32 %20, i32* @mygetrandom.state
br label %37
21: ; preds = %14
%22 = load i32*, i32** %8
store i32 34, i32* %22
%23 = load i32, i32* @mygetrandom.state
%24 = add nsw i32 %23, 1
%25 = srem i32 %24, 4
store i32 %25, i32* @mygetrandom.state
br label %37
26: ; preds = %14
%27 = load i32*, i32** %8
store i32 51, i32* %27
%28 = load i32, i32* @mygetrandom.state
%29 = add nsw i32 %28, 1
%30 = srem i32 %29, 4
store i32 %30, i32* @mygetrandom.state
br label %37
31: ; preds = %14
%32 = load i32*, i32** %8
store i32 68, i32* %32
%33 = load i32, i32* @mygetrandom.state
%34 = add nsw i32 %33, 1
%35 = srem i32 %34, 4
store i32 %35, i32* @mygetrandom.state
br label %37
36: ; preds = %14
store i32 0, i32* %7
br label %37
37: ; preds = %36, %31, %26, %21, %16
br label %38
38: ; preds = %37, %13
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
@mygetrandom.state = internal unnamed_addr global i32 0
@switch.table.mygetrandom = [4 x i32] [i32 1, i32 2, i32 3, i32 0]
define i32 @mygetrandom(i8* nocapture writeonly %0, i64 %1, i32 %2) {
%4 = bitcast i8* %0 to i32*
%5 = icmp eq i64 %1, 0
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = load i32, i32* @mygetrandom.state
%8 = icmp ult i32 %7, 4
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = mul i32 %7, 17
%11 = add i32 %10, 17
%12 = sext i32 %7 to i64
%13 = getelementptr inbounds [4 x i32], [4 x i32]* @switch.table.mygetrandom, i64 0, i64 %12
%14 = load i32, i32* %13
store i32 %11, i32* %4
store i32 %14, i32* @mygetrandom.state
br label %15
15: ; preds = %6, %9, %3
%16 = phi i32 [ -1, %3 ], [ 0, %6 ], [ 4, %9 ]
ret i32 %16
}