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

/* Variables and functions */
long UINT_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned int *, int);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
char *generate(size_t len, char *table, int fd) {
  char *pstring = NULL;
  size_t table_len = strlen(table);
  if (table_len < 1) {
    fprintf(stderr, "E: invalid table length '%zd'\n", table_len);
    goto fail;
  }
  if (!(pstring = malloc((len + 1) * sizeof(char)))) {
    fprintf(stderr, "E: failed to allocate memory (malloc)\n");
    goto fail;
  }
  for (size_t i = 0; i < len; ++i) {
    unsigned int rand;
    unsigned int max_rand = UINT_MAX - ((long int)UINT_MAX + 1) % table_len;
    if (read(fd, &rand, sizeof rand) == -1) {
      perror("read");
      goto fail;
    }
    while (rand > max_rand) {
      if (read(fd, &rand, sizeof rand) == -1) {
        perror("read");
        goto fail;
      }
    }
    pstring[i] = table[rand % table_len];
  }
  pstring[len] = '\0';
  return pstring;
fail:
  if (pstring)
    free(pstring);
  return NULL;
}


x86 assembly (clang, -O0):
<generate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jae    11b3 <generate+0x53>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ed <generate+0x18d>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ee <generate+0x8e>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ed <generate+0x18d>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    12d4 <generate+0x174>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%rax
add    $0x1,%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x30(%rbp)
mov    -0x48(%rbp),%rax
sub    %rdx,%rax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    125d <generate+0xfd>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
jmp    12ed <generate+0x18d>
jmp    1262 <generate+0x102>
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jbe    129e <generate+0x13e>
mov    -0x1c(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1299 <generate+0x139>
lea    0xdb7(%rip),%rdi
call   1080 <perror@plt>
jmp    12ed <generate+0x18d>
jmp    1262 <generate+0x102>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x3c(%rbp),%eax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x30(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax,%rdx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11f6 <generate+0x96>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1309 <generate+0x1a9>
cmpq   $0x0,-0x28(%rbp)
je     1301 <generate+0x1a1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %rdi,%r12
mov    %rsi,0x20(%rsp)
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     126a <generate+0x10a>
mov    %rax,%r13
lea    0x1(%r12),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1288 <generate+0x128>
mov    %rax,%r15
test   %r12,%r12
je     12a4 <generate+0x144>
xor    %r14d,%r14d
lea    0xc(%rsp),%rbx
mov    %r15,0x18(%rsp)
mov    %r12,0x10(%rsp)
jmp    11de <generate+0x7e>
nop
mov    0x10(%rsp),%r12
mov    0x20(%rsp),%rcx
mov    (%rcx,%rax,1),%al
mov    %al,(%r15,%r14,1)
add    $0x1,%r14
cmp    %r12,%r14
je     12a4 <generate+0x144>
mov    0x2e03(%rip),%rax
mov    (%rax),%r12
lea    0x1(%r12),%rax
mov    %rax,%rcx
or     %r13,%rcx
shr    $0x20,%rcx
je     1210 <generate+0xb0>
xor    %edx,%edx
div    %r13
mov    %rdx,%r15
jmp    1218 <generate+0xb8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edx,%edx
div    %r13d
mov    %edx,%r15d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     12ab <generate+0x14b>
sub    %r15d,%r12d
nop
mov    0xc(%rsp),%eax
cmp    %r12d,%eax
jbe    1250 <generate+0xf0>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1230 <generate+0xd0>
jmp    12ab <generate+0x14b>
nop
cmp    %r13,%rax
mov    0x18(%rsp),%r15
jb     11c0 <generate+0x60>
xor    %edx,%edx
div    %r13d
mov    %edx,%eax
jmp    11c0 <generate+0x60>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
xor    %r15d,%r15d
xor    %edx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12c4 <generate+0x164>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12c4 <generate+0x164>
movb   $0x0,(%r15,%r12,1)
jmp    12c4 <generate+0x164>
lea    0xd94(%rip),%rdi
call   1080 <perror@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"E: invalid table length '%zd'\0A\00"
@.str.1 = [39 x i8] c"E: failed to allocate memory (malloc)\0A\00"
@UINT_MAX = global i64 0
@.str.2 = [5 x i8] c"read\00"
define i8* @generate(i64 %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i64 %0, i64* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* null, i8** %8
%13 = load i8*, i8** %6
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %9
%15 = load i64, i64* %9
%16 = icmp ult i64 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = load i64, i64* %9
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %19)
br label %79
21: ; preds = %3
%22 = load i64, i64* %5
%23 = add i64 %22, 1
%24 = mul i64 %23, 1
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %79
30: ; preds = %21
store i64 0, i64* %10
br label %31
31: ; preds = %71, %30
%32 = load i64, i64* %10
%33 = load i64, i64* %5
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %74
35: ; preds = %31
%36 = load i64, i64* @UINT_MAX
%37 = load i64, i64* @UINT_MAX
%38 = add nsw i64 %37, 1
%39 = load i64, i64* %9
%40 = urem i64 %38, %39
%41 = sub i64 %36, %40
%42 = trunc i64 %41 to i32
store i32 %42, i32* %12
%43 = load i32, i32* %7
%44 = call i32 @read(i32 %43, i32* %11, i32 4)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %35
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %79
48: ; preds = %35
br label %49
49: ; preds = %59, %48
%50 = load i32, i32* %11
%51 = load i32, i32* %12
%52 = icmp ugt i32 %50, %51
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i32, i32* %7
%55 = call i32 @read(i32 %54, i32* %11, i32 4)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %79
59: ; preds = %53
br label %49
60: ; preds = %49
%61 = load i8*, i8** %6
%62 = load i32, i32* %11
%63 = zext i32 %62 to i64
%64 = load i64, i64* %9
%65 = urem i64 %63, %64
%66 = getelementptr inbounds i8, i8* %61, i64 %65
%67 = load i8, i8* %66
%68 = load i8*, i8** %8
%69 = load i64, i64* %10
%70 = getelementptr inbounds i8, i8* %68, i64 %69
store i8 %67, i8* %70
br label %71
71: ; preds = %60
%72 = load i64, i64* %10
%73 = add i64 %72, 1
store i64 %73, i64* %10
br label %31
74: ; preds = %31
%75 = load i8*, i8** %8
%76 = load i64, i64* %5
%77 = getelementptr inbounds i8, i8* %75, i64 %76
store i8 0, i8* %77
%78 = load i8*, i8** %8
store i8* %78, i8** %4
br label %86
79: ; preds = %57, %46, %27, %17
%80 = load i8*, i8** %8
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i8*, i8** %8
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %82, %79
store i8* null, i8** %4
br label %86
86: ; preds = %85, %74
%87 = load i8*, i8** %4
ret i8* %87
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"E: invalid table length '%zd'\0A\00"
@.str.1 = [39 x i8] c"E: failed to allocate memory (malloc)\0A\00"
@UINT_MAX = global i64 0
@.str.2 = [5 x i8] c"read\00"
define i8* @generate(i64 %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 0)
br label %52
10: ; preds = %3
%11 = add i64 %0, 1
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = icmp eq i64 %0, 0
br i1 %15, label %46, label %16
16: ; preds = %14
%17 = bitcast i32* %4 to i8*
br label %21
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %52
21: ; preds = %16, %38
%22 = phi i64 [ 0, %16 ], [ %44, %38 ]
%23 = load i64, i64* @UINT_MAX
%24 = add nsw i64 %23, 1
%25 = urem i64 %24, %5
%26 = sub i64 %23, %25
%27 = trunc i64 %26 to i32
%28 = call i32 @read(i32 %2, i32* %4, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %50
32: ; preds = %21, %35
%33 = load i32, i32* %4
%34 = icmp ugt i32 %33, %27
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @read(i32 %2, i32* %4, i32 4)
%37 = icmp eq i32 %36, -1
br i1 %37, label %48, label %32
38: ; preds = %32
%39 = zext i32 %33 to i64
%40 = urem i64 %39, %5
%41 = getelementptr inbounds i8, i8* %1, i64 %40
%42 = load i8, i8* %41
%43 = getelementptr inbounds i8, i8* %12, i64 %22
store i8 %42, i8* %43
%44 = add nuw i64 %22, 1
%45 = icmp eq i64 %44, %0
br i1 %45, label %46, label %21
46: ; preds = %38, %14
%47 = getelementptr inbounds i8, i8* %12, i64 %0
store i8 0, i8* %47
br label %52
48: ; preds = %35
%49 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %30
%51 = tail call i32 @free(i8* %12)
br label %52
52: ; preds = %18, %7, %50, %46
%53 = phi i8* [ %12, %46 ], [ null, %50 ], [ null, %7 ], [ null, %18 ]
ret i8* %53
}