#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 */
scalar_t__ alloca(int);
int /*<<< orphan*/ base64_encode(unsigned char *, int, unsigned char *);
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ errno;
int read(int, unsigned char *, int);
int /*<<< orphan*/ strcpy(unsigned char *, unsigned char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void generate_random_password(int fd, int size, unsigned char *buf) {
  int rand_bytes;
  unsigned char *rnd_buf = 0;
  unsigned char *b64_buf = 0;
  unsigned char *p;
  int r, n;
  rnd_buf = (unsigned char *)alloca(size + 16);
  b64_buf = (unsigned char *)alloca(size + 16);
  if (size % 4) {
    rand_bytes = (size / 4 + 1) * 3;
  } else {
    rand_bytes = (size / 4) * 3;
  }
  r = rand_bytes;
  p = rnd_buf;
  while (r > 0) {
    n = read(fd, p, r);
    if (n < 0)
      die("read from /dev/urandom failed: %s", strerror(errno));
    if (!n)
      die("EOF on /dev/urandom???");
    p += n;
    r -= n;
  }
  base64_encode(rnd_buf, rand_bytes, b64_buf);
  b64_buf[size] = 0;
  for (p = b64_buf; *p; p++) {
    switch (*p) {
    case 'l':
      *p = '!';
      break;
    case 'I':
      *p = '@';
      break;
    case '1':
      *p = '^';
      break;
    case 'O':
      *p = '*';
      break;
    case '0':
      *p = '-';
      break;
    }
  }
  strcpy(buf, b64_buf);
}


x86 assembly (clang, -O0):
<generate_random_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x10,%edi
call   1050 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x10,%edi
call   1050 <alloca@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11cd <generate_random_password+0x6d>
mov    -0x8(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x14(%rbp)
jmp    11de <generate_random_password+0x7e>
mov    -0x8(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
imul   $0x3,%eax,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1269 <generate_random_password+0x109>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1230 <generate_random_password+0xd0>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
cmpl   $0x0,-0x38(%rbp)
jne    1248 <generate_random_password+0xe8>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x38(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
jmp    11ec <generate_random_password+0x8c>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1060 <base64_encode@plt>
mov    -0x28(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     1343 <generate_random_password+0x1e3>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x30,%eax
je     1326 <generate_random_password+0x1c6>
jmp    12b2 <generate_random_password+0x152>
mov    -0x3c(%rbp),%eax
sub    $0x31,%eax
je     130e <generate_random_password+0x1ae>
jmp    12c3 <generate_random_password+0x163>
mov    -0x3c(%rbp),%eax
sub    $0x49,%eax
je     1302 <generate_random_password+0x1a2>
jmp    12d4 <generate_random_password+0x174>
mov    -0x3c(%rbp),%eax
sub    $0x4f,%eax
je     131a <generate_random_password+0x1ba>
jmp    12e5 <generate_random_password+0x185>
mov    -0x3c(%rbp),%eax
sub    $0x6c,%eax
jne    132d <generate_random_password+0x1cd>
jmp    12f6 <generate_random_password+0x196>
mov    -0x30(%rbp),%rax
movb   $0x21,(%rax)
jmp    132d <generate_random_password+0x1cd>
mov    -0x30(%rbp),%rax
movb   $0x40,(%rax)
jmp    132d <generate_random_password+0x1cd>
mov    -0x30(%rbp),%rax
movb   $0x5e,(%rax)
jmp    132d <generate_random_password+0x1cd>
mov    -0x30(%rbp),%rax
movb   $0x2a,(%rax)
jmp    132d <generate_random_password+0x1cd>
mov    -0x30(%rbp),%rax
movb   $0x2d,(%rax)
jmp    1332 <generate_random_password+0x1d2>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    128d <generate_random_password+0x12d>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_random_password>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %esi,%r14d
mov    %edi,%ebp
lea    0x10(%r14),%ebx
mov    %ebx,%edi
call   1050 <alloca@plt>
mov    %rax,0x10(%rsp)
mov    %ebx,%edi
call   1050 <alloca@plt>
mov    %rax,0x18(%rsp)
mov    %r14d,%eax
and    $0x3,%eax
lea    0x3(%r14),%ecx
test   %r14d,%r14d
cmovns %r14d,%ecx
sar    $0x2,%ecx
cmp    $0x1,%eax
sbb    $0xffffffff,%ecx
lea    (%rcx,%rcx,2),%eax
mov    %eax,0xc(%rsp)
test   %ecx,%ecx
jle    121b <generate_random_password+0xbb>
lea    0xec0(%rip),%rbx
mov    0xc(%rsp),%r13d
mov    0x10(%rsp),%r15
jmp    11f5 <generate_random_password+0x95>
nopl   0x0(%rax)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <die@plt>
movslq %r12d,%rax
add    %rax,%r15
sub    %r12d,%r13d
jle    121b <generate_random_password+0xbb>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r13d,%edx
call   1040 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
js     11d0 <generate_random_password+0x70>
jne    11ea <generate_random_password+0x8a>
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
jmp    11ea <generate_random_password+0x8a>
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%esi
mov    0x18(%rsp),%rbx
mov    %rbx,%rdx
call   1060 <base64_encode@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
lea    0xdc1(%rip),%rax
mov    %rbx,%rcx
jmp    124c <generate_random_password+0xec>
mov    $0x2d,%dl
mov    %dl,(%rcx)
add    $0x1,%rcx
movzbl (%rcx),%esi
lea    -0x30(%rsi),%edx
cmp    $0x1f,%dl
ja     1270 <generate_random_password+0x110>
movzbl %dl,%edx
movslq (%rax,%rdx,4),%rdx
add    %rax,%rdx
jmp    *%rdx
mov    $0x5e,%dl
jmp    1246 <generate_random_password+0xe6>
nopw   0x0(%rax,%rax,1)
mov    $0x21,%dl
cmp    $0x6c,%sil
je     1246 <generate_random_password+0xe6>
test   %sil,%sil
jne    1248 <generate_random_password+0xe8>
jmp    1287 <generate_random_password+0x127>
mov    $0x40,%dl
jmp    1246 <generate_random_password+0xe6>
mov    $0x2a,%dl
jmp    1246 <generate_random_password+0xe6>
mov    0x20(%rsp),%rdi
mov    %rbx,%rsi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strcpy@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"read from /dev/urandom failed: %s\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"EOF on /dev/urandom???\00"
define void @generate_random_password(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i8* null, i8** %8
store i8* null, i8** %9
%13 = load i32, i32* %5
%14 = add nsw i32 %13, 16
%15 = call i64 @alloca(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 16
%19 = call i64 @alloca(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %9
%21 = load i32, i32* %5
%22 = srem i32 %21, 4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %3
%25 = load i32, i32* %5
%26 = sdiv i32 %25, 4
%27 = add nsw i32 %26, 1
%28 = mul nsw i32 %27, 3
store i32 %28, i32* %7
br label %33
29: ; preds = %3
%30 = load i32, i32* %5
%31 = sdiv i32 %30, 4
%32 = mul nsw i32 %31, 3
store i32 %32, i32* %7
br label %33
33: ; preds = %29, %24
%34 = load i32, i32* %7
store i32 %34, i32* %11
%35 = load i8*, i8** %8
store i8* %35, i8** %10
br label %36
36: ; preds = %55, %33
%37 = load i32, i32* %11
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %63
39: ; preds = %36
%40 = load i32, i32* %4
%41 = load i8*, i8** %10
%42 = load i32, i32* %11
%43 = call i32 @read(i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %39
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %39
%51 = load i32, i32* %12
%52 = icmp ne i32 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %50
%54 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
%56 = load i32, i32* %12
%57 = load i8*, i8** %10
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8* %59, i8** %10
%60 = load i32, i32* %12
%61 = load i32, i32* %11
%62 = sub nsw i32 %61, %60
store i32 %62, i32* %11
br label %36
63: ; preds = %36
%64 = load i8*, i8** %8
%65 = load i32, i32* %7
%66 = load i8*, i8** %9
%67 = call i32 @base64_encode(i8* %64, i32 %65, i8* %66)
%68 = load i8*, i8** %9
%69 = load i32, i32* %5
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = load i8*, i8** %9
store i8* %72, i8** %10
br label %73
73: ; preds = %92, %63
%74 = load i8*, i8** %10
%75 = load i8, i8* %74
%76 = icmp ne i8 %75, 0
br i1 %76, label %77, label %95
77: ; preds = %73
%78 = load i8*, i8** %10
%79 = load i8, i8* %78
%80 = zext i8 %79 to i32
switch i32 %80, label %91 [
i32 108, label %81
i32 73, label %83
i32 49, label %85
i32 79, label %87
i32 48, label %89
]
81: ; preds = %77
%82 = load i8*, i8** %10
store i8 33, i8* %82
br label %91
83: ; preds = %77
%84 = load i8*, i8** %10
store i8 64, i8* %84
br label %91
85: ; preds = %77
%86 = load i8*, i8** %10
store i8 94, i8* %86
br label %91
87: ; preds = %77
%88 = load i8*, i8** %10
store i8 42, i8* %88
br label %91
89: ; preds = %77
%90 = load i8*, i8** %10
store i8 45, i8* %90
br label %91
91: ; preds = %77, %89, %87, %85, %83, %81
br label %92
92: ; preds = %91
%93 = load i8*, i8** %10
%94 = getelementptr inbounds i8, i8* %93, i32 1
store i8* %94, i8** %10
br label %73
95: ; preds = %73
%96 = load i8*, i8** %6
%97 = load i8*, i8** %9
%98 = call i32 @strcpy(i8* %96, i8* %97)
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"read from /dev/urandom failed: %s\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"EOF on /dev/urandom???\00"
define void @generate_random_password(i32 %0, i32 %1, i8* %2) {
%4 = add nsw i32 %1, 16
%5 = tail call i64 @alloca(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i64 @alloca(i32 %4)
%8 = inttoptr i64 %7 to i8*
%9 = and i32 %1, 3
%10 = icmp ne i32 %9, 0
%11 = sdiv i32 %1, 4
%12 = zext i1 %10 to i32
%13 = add nsw i32 %11, %12
%14 = mul nsw i32 %13, 3
%15 = icmp sgt i32 %13, 0
br i1 %15, label %16, label %34
16: ; preds = %3, %29
%17 = phi i32 [ %32, %29 ], [ %14, %3 ]
%18 = phi i8* [ %31, %29 ], [ %6, %3 ]
%19 = tail call i32 @read(i32 %0, i8* %18, i32 %17)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = tail call i32 @strerror(i32 %22)
%24 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %29
25: ; preds = %16
%26 = icmp eq i32 %19, 0
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %21, %27, %25
%30 = sext i32 %19 to i64
%31 = getelementptr inbounds i8, i8* %18, i64 %30
%32 = sub nsw i32 %17, %19
%33 = icmp sgt i32 %32, 0
br i1 %33, label %16, label %34
34: ; preds = %29, %3
%35 = tail call i32 @base64_encode(i8* %6, i32 %14, i8* %8)
%36 = sext i32 %1 to i64
%37 = getelementptr inbounds i8, i8* %8, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %47, %34
%39 = phi i8* [ %8, %34 ], [ %48, %47 ]
%40 = load i8, i8* %39
switch i8 %40, label %47 [
i8 0, label %49
i8 108, label %45
i8 73, label %41
i8 49, label %42
i8 79, label %43
i8 48, label %44
]
41: ; preds = %38
br label %45
42: ; preds = %38
br label %45
43: ; preds = %38
br label %45
44: ; preds = %38
br label %45
45: ; preds = %38, %44, %43, %42, %41
%46 = phi i8 [ 64, %41 ], [ 94, %42 ], [ 42, %43 ], [ 45, %44 ], [ 33, %38 ]
store i8 %46, i8* %39
br label %47
47: ; preds = %45, %38
%48 = getelementptr inbounds i8, i8* %39, i64 1
br label %38
49: ; preds = %38
%50 = tail call i32 @strcpy(i8* %2, i8* %8)
ret void
}