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

/* Variables and functions */
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ R_OK;
int S_IFCHR;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ makedev(int, int);
int /*<<< orphan*/ mknod(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int xopen2(char *, int);
int /*<<< orphan*/ xxread(int, char *, int);
void gen_rand_str(char *buf, int len) {
  const char base[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.";
  int urandom;
  if (access("/dev/urandom", R_OK) == 0) {
    urandom = xopen2("/dev/urandom", O_RDONLY | O_CLOEXEC);
  } else {
    mknod("/urandom", S_IFCHR | 0666, makedev(1, 9));
    urandom = xopen2("/urandom", O_RDONLY | O_CLOEXEC);
    unlink("/urandom");
  }
  xxread(urandom, buf, len - 1);
  close(urandom);
  for (int i = 0; i < len - 1; ++i) {
    buf[i] = base[buf[i] % (sizeof(base) - 1)];
  }
  buf[len - 1] = '\0';
}


x86 assembly (clang, -O0):
<gen_rand_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe66(%rip),%rsi
mov    $0x41,%edx
call   1080 <memcpy@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    11e9 <gen_rand_str+0x69>
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
call   1050 <xopen2@plt>
mov    %eax,-0x54(%rbp)
jmp    1247 <gen_rand_str+0xc7>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
or     $0x1b6,%eax
mov    %eax,-0x5c(%rbp)
mov    $0x1,%edi
mov    $0x9,%esi
call   10a0 <makedev@plt>
mov    -0x5c(%rbp),%esi
mov    %eax,%edx
lea    0xe30(%rip),%rdi
call   1070 <mknod@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2d96(%rip),%rax
or     (%rax),%esi
lea    0xe12(%rip),%rdi
call   1050 <xopen2@plt>
mov    %eax,-0x54(%rbp)
lea    0xe03(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x54(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1040 <xxread@plt>
mov    -0x54(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12a7 <gen_rand_str+0x127>
mov    -0x8(%rbp),%rax
movslq -0x58(%rbp),%rcx
movsbq (%rax,%rcx,1),%rax
and    $0x3f,%rax
mov    -0x50(%rbp,%rax,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x58(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    1268 <gen_rand_str+0xe8>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_rand_str>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xeb3(%rip),%rdi
call   1080 <access@plt>
test   %rax,%rax
je     11ef <gen_rand_str+0x7f>
mov    0x2e39(%rip),%rax
mov    $0x1b6,%ebp
or     (%rax),%ebp
mov    $0x1,%edi
mov    $0x9,%esi
call   1090 <makedev@plt>
lea    0xe89(%rip),%r15
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <mknod@plt>
mov    0x2e19(%rip),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
call   1050 <xopen2@plt>
mov    %eax,%r12d
mov    %r15,%rdi
call   1030 <unlink@plt>
jmp    1210 <gen_rand_str+0xa0>
mov    0x2df2(%rip),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe39(%rip),%rdi
call   1050 <xopen2@plt>
mov    %eax,%r12d
movslq %r14d,%rbp
lea    -0x1(%rbp),%r14
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <xxread@plt>
mov    %r12d,%edi
call   1060 <close@plt>
cmp    $0x2,%ebp
jl     1291 <gen_rand_str+0x121>
mov    %r14d,%ebp
cmp    $0x1,%r14d
jne    123f <gen_rand_str+0xcf>
xor    %eax,%eax
jmp    1277 <gen_rand_str+0x107>
mov    %ebp,%edx
and    $0xfffffffe,%edx
xor    %eax,%eax
lea    0xdb3(%rip),%rsi
nopl   (%rax)
movzbl (%rbx,%rax,1),%edi
and    $0x3f,%edi
movzbl (%rdi,%rsi,1),%ecx
mov    %cl,(%rbx,%rax,1)
movzbl 0x1(%rbx,%rax,1),%ecx
and    $0x3f,%ecx
movzbl (%rcx,%rsi,1),%ecx
mov    %cl,0x1(%rbx,%rax,1)
add    $0x2,%rax
cmp    %rax,%rdx
jne    1250 <gen_rand_str+0xe0>
test   $0x1,%bpl
je     1291 <gen_rand_str+0x121>
movzbl (%rbx,%rax,1),%ecx
and    $0x3f,%ecx
lea    0xd75(%rip),%rdx
mov    (%rcx,%rdx,1),%cl
mov    %cl,(%rbx,%rax,1)
movb   $0x0,(%rbx,%r14,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.gen_rand_str.base = [65 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.\00"
@.str = [13 x i8] c"/dev/urandom\00"
@R_OK = global i32 0
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [9 x i8] c"/urandom\00"
@S_IFCHR = global i32 0
define void @gen_rand_str(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [65 x i8]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = bitcast [65 x i8]* %5 to i8*
%9 = load i32, i32* @R_OK
%10 = call i64 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @O_RDONLY
%14 = load i32, i32* @O_CLOEXEC
%15 = or i32 %13, %14
%16 = call i32 @xopen2(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
br label %27
17: ; preds = %2
%18 = load i32, i32* @S_IFCHR
%19 = or i32 %18, 438
%20 = call i32 @makedev(i32 1, i32 9)
%21 = call i32 @mknod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* @O_RDONLY
%23 = load i32, i32* @O_CLOEXEC
%24 = or i32 %22, %23
%25 = call i32 @xopen2(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 %25, i32* %6
%26 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %17, %12
%28 = load i32, i32* %6
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = sub nsw i32 %30, 1
%32 = call i32 @xxread(i32 %28, i8* %29, i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 0, i32* %7
br label %35
35: ; preds = %54, %27
%36 = load i32, i32* %7
%37 = load i32, i32* %4
%38 = sub nsw i32 %37, 1
%39 = icmp slt i32 %36, %38
br i1 %39, label %40, label %57
40: ; preds = %35
%41 = load i8*, i8** %3
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i64
%47 = urem i64 %46, 64
%48 = getelementptr inbounds [65 x i8], [65 x i8]* %5, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = load i8*, i8** %3
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 %49, i8* %53
br label %54
54: ; preds = %40
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %35
57: ; preds = %35
%58 = load i8*, i8** %3
%59 = load i32, i32* %4
%60 = sub nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
store i8 0, i8* %62
ret void
}

LLVM IR (-O2):
@__const.gen_rand_str.base = [65 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.\00"
@.str = [13 x i8] c"/dev/urandom\00"
@R_OK = global i32 0
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [9 x i8] c"/urandom\00"
@S_IFCHR = global i32 0
define void @gen_rand_str(i8* %0, i32 %1) {
%3 = load i32, i32* @R_OK
%4 = tail call i64 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_CLOEXEC
%9 = or i32 %8, %7
%10 = tail call i32 @xopen2(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %21
11: ; preds = %2
%12 = load i32, i32* @S_IFCHR
%13 = or i32 %12, 438
%14 = tail call i32 @makedev(i32 1, i32 9)
%15 = tail call i32 @mknod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_CLOEXEC
%18 = or i32 %17, %16
%19 = tail call i32 @xopen2(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = tail call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %11, %6
%22 = phi i32 [ %10, %6 ], [ %19, %11 ]
%23 = add nsw i32 %1, -1
%24 = tail call i32 @xxread(i32 %22, i8* %0, i32 %23)
%25 = tail call i32 @close(i32 %22)
%26 = icmp sgt i32 %1, 1
br i1 %26, label %27, label %43
27: ; preds = %21
%28 = zext i32 %23 to i64
%29 = and i64 %28, 1
%30 = icmp eq i32 %23, 1
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = and i64 %28, 4294967294
br label %46
33: ; preds = %46, %27
%34 = phi i64 [ 0, %27 ], [ %62, %46 ]
%35 = icmp eq i64 %29, 0
br i1 %35, label %43, label %36
36: ; preds = %33
%37 = getelementptr inbounds i8, i8* %0, i64 %34
%38 = load i8, i8* %37
%39 = and i8 %38, 63
%40 = zext i8 %39 to i64
%41 = getelementptr inbounds [65 x i8], [65 x i8]* @__const.gen_rand_str.base, i64 0, i64 %40
%42 = load i8, i8* %41
store i8 %42, i8* %37
br label %43
43: ; preds = %36, %33, %21
%44 = sext i32 %23 to i64
%45 = getelementptr inbounds i8, i8* %0, i64 %44
store i8 0, i8* %45
ret void
46: ; preds = %46, %31
%47 = phi i64 [ 0, %31 ], [ %62, %46 ]
%48 = phi i64 [ 0, %31 ], [ %63, %46 ]
%49 = getelementptr inbounds i8, i8* %0, i64 %47
%50 = load i8, i8* %49
%51 = and i8 %50, 63
%52 = zext i8 %51 to i64
%53 = getelementptr inbounds [65 x i8], [65 x i8]* @__const.gen_rand_str.base, i64 0, i64 %52
%54 = load i8, i8* %53
store i8 %54, i8* %49
%55 = or i64 %47, 1
%56 = getelementptr inbounds i8, i8* %0, i64 %55
%57 = load i8, i8* %56
%58 = and i8 %57, 63
%59 = zext i8 %58 to i64
%60 = getelementptr inbounds [65 x i8], [65 x i8]* @__const.gen_rand_str.base, i64 0, i64 %59
%61 = load i8, i8* %60
store i8 %61, i8* %56
%62 = add nuw nsw i64 %47, 2
%63 = add i64 %48, 2
%64 = icmp eq i64 %63, %32
br i1 %64, label %33, label %46
}