#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ base32_encode(char *, char *, int, unsigned int);
int getline(char **, size_t *, int /*<<< orphan*/);
char *malloc(unsigned int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcat(char *, char *);
unsigned int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int main(int argc, char **argv) {
  char *dest = malloc(sizeof *dest);
  char *help = "-h";
  char *src_buffer = malloc(sizeof *src_buffer);
  char *total_buffer = malloc(sizeof *total_buffer);
  char *src;
  struct timeval never_wait;
  never_wait.tv_sec = 0;
  never_wait.tv_usec = 4000;
  unsigned int srclen = 0;
  unsigned int outlen = 0;
  fd_set read_file_descriptors;
  FD_ZERO(&read_file_descriptors);
  FD_SET(STDIN_FILENO, &read_file_descriptors);
  if (argc > 1 && strncmp(argv[1], help, 3) != 0) {
    src = argv[1];
    srclen = strlen(src);
    outlen = (srclen * 8 / 5);
    outlen = outlen + (8 - (outlen % 8));
    dest = malloc(outlen);
    base32_encode(dest, src, srclen, outlen);
    printf("%s", dest);
  } else if (select(1, &read_file_descriptors, NULL, NULL, &never_wait)) {
    int read_bytes = 0;
    int read_status = 0;
    size_t len = 0;
    read_status = getline(&src_buffer, &len, stdin);
    while (read_status != -1) {
      if (read_status != -1) {
        read_bytes += read_status;
        strcat(total_buffer, src_buffer);
        src_buffer = NULL;
      }
      read_status = getline(&src_buffer, &len, stdin);
    }
    outlen = (read_bytes * 8 / 5);
    outlen = outlen + (8 - (outlen % 8));
    dest = malloc(outlen);
    base32_encode(dest, total_buffer, read_bytes, outlen);
    printf("%s", dest);
  } else {
    printf("%s", "If no arguments given it reads from stdin until it's at the "
                 "end. Otherwise it reads the first argument passed to the "
                 "program as a string to encode.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe35(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0xfa0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
lea    -0x54(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    -0x54(%rbp),%rsi
call   10c0 <FD_SET@plt>
cmpl   $0x1,-0x8(%rbp)
jle    12c0 <main+0x120>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12c0 <main+0x120>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
shl    $0x3,%eax
mov    $0x5,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    -0x50(%rbp),%edx
and    $0x7,%edx
mov    $0x8,%ecx
sub    %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x4c(%rbp),%edx
mov    -0x50(%rbp),%ecx
call   1090 <base32_encode@plt>
mov    -0x18(%rbp),%rsi
lea    0xd4d(%rip),%rdi
call   1050 <printf@plt>
jmp    13d4 <main+0x234>
mov    $0x1,%edi
lea    -0x54(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
cmp    $0x0,%rax
je     13bc <main+0x21c>
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
movq   $0x0,-0x68(%rbp)
mov    0x2ce8(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rdi
lea    -0x68(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
je     1362 <main+0x1c2>
cmpl   $0xffffffff,-0x5c(%rbp)
je     1344 <main+0x1a4>
mov    -0x5c(%rbp),%eax
add    -0x58(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <strcat@plt>
movq   $0x0,-0x28(%rbp)
mov    0x2c9d(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rdi
lea    -0x68(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x5c(%rbp)
jmp    1312 <main+0x172>
mov    -0x58(%rbp),%eax
shl    $0x3,%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    -0x50(%rbp),%edx
and    $0x7,%edx
mov    $0x8,%ecx
sub    %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x58(%rbp),%edx
mov    -0x50(%rbp),%ecx
call   1090 <base32_encode@plt>
mov    -0x18(%rbp),%rsi
lea    0xc51(%rip),%rdi
call   1050 <printf@plt>
jmp    13cf <main+0x22f>
lea    0xc40(%rip),%rdi
lea    0xc3c(%rip),%rsi
call   1050 <printf@plt>
jmp    13d4 <main+0x234>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
movq   $0x0,0x28(%rsp)
movl   $0xfa0,0x20(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
cmp    $0x2,%ebp
jl     126e <main+0xce>
mov    0x8(%r15),%rdi
lea    0xdf4(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     126e <main+0xce>
mov    0x8(%r15),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0x0(,%r15,8),%eax
mov    $0xcccccccd,%ebx
imul   %rax,%rbx
shr    $0x22,%rbx
and    $0xfffffff8,%ebx
add    $0x8,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
mov    %ebx,%ecx
call   1090 <base32_encode@plt>
lea    0xd9d(%rip),%rdi
mov    %rbp,%rsi
jmp    1351 <main+0x1b1>
xor    %ebx,%ebx
lea    0x14(%rsp),%rsi
lea    0x20(%rsp),%r8
mov    $0x1,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %rax,%rax
je     1343 <main+0x1a3>
movq   $0x0,0x18(%rsp)
mov    0x2d47(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     12fb <main+0x15b>
xor    %ebx,%ebx
lea    0x8(%rsp),%r15
lea    0x18(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %eax,%ebx
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10b0 <strcat@plt>
movq   $0x0,0x8(%rsp)
mov    0x0(%rbp),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
jne    12d0 <main+0x130>
lea    0x0(,%rbx,8),%eax
cltq
imul   $0x66666667,%rax,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x21,%rbp
add    %eax,%ebp
and    $0xfffffff8,%ebp
add    $0x8,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1090 <base32_encode@plt>
lea    0xcc5(%rip),%rdi
mov    %r15,%rsi
jmp    1351 <main+0x1b1>
lea    0xcb9(%rip),%rdi
lea    0xcb5(%rip),%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [3 x i8] c"-h\00"
@STDIN_FILENO = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.2 = [149 x i8] c"If no arguments given it reads from stdin until it's at the end. Otherwise it reads the first argument passed to the program as a string to encode.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = call i8* @malloc(i32 1)
store i8* %18, i8** %6
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7
%19 = call i8* @malloc(i32 1)
store i8* %19, i8** %8
%20 = call i8* @malloc(i32 1)
store i8* %20, i8** %9
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 4000, i32* %22
store i32 0, i32* %12
store i32 0, i32* %13
%23 = call i32 @FD_ZERO(i32* %14)
%24 = load i32, i32* @STDIN_FILENO
%25 = call i32 @FD_SET(i32 %24, i32* %14)
%26 = load i32, i32* %4
%27 = icmp sgt i32 %26, 1
br i1 %27, label %28, label %58
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i8*, i8** %7
%33 = call i64 @strncmp(i8* %31, i8* %32, i32 3)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %58
35: ; preds = %28
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = call i32 @strlen(i8* %39)
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = mul i32 %41, 8
%43 = udiv i32 %42, 5
store i32 %43, i32* %13
%44 = load i32, i32* %13
%45 = load i32, i32* %13
%46 = urem i32 %45, 8
%47 = sub i32 8, %46
%48 = add i32 %44, %47
store i32 %48, i32* %13
%49 = load i32, i32* %13
%50 = call i8* @malloc(i32 %49)
store i8* %50, i8** %6
%51 = load i8*, i8** %6
%52 = load i8*, i8** %10
%53 = load i32, i32* %12
%54 = load i32, i32* %13
%55 = call i32 @base32_encode(i8* %51, i8* %52, i32 %53, i32 %54)
%56 = load i8*, i8** %6
%57 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %56)
br label %101
58: ; preds = %28, %2
%59 = call i64 @select(i32 1, i32* %14, i32* null, i32* null, %struct.timeval* %11)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %98
61: ; preds = %58
store i32 0, i32* %15
store i32 0, i32* %16
store i64 0, i64* %17
%62 = load i32, i32* @stdin
%63 = call i32 @getline(i8** %8, i64* %17, i32 %62)
store i32 %63, i32* %16
br label %64
64: ; preds = %77, %61
%65 = load i32, i32* %16
%66 = icmp ne i32 %65, -1
br i1 %66, label %67, label %80
67: ; preds = %64
%68 = load i32, i32* %16
%69 = icmp ne i32 %68, -1
br i1 %69, label %70, label %77
70: ; preds = %67
%71 = load i32, i32* %16
%72 = load i32, i32* %15
%73 = add nsw i32 %72, %71
store i32 %73, i32* %15
%74 = load i8*, i8** %9
%75 = load i8*, i8** %8
%76 = call i32 @strcat(i8* %74, i8* %75)
store i8* null, i8** %8
br label %77
77: ; preds = %70, %67
%78 = load i32, i32* @stdin
%79 = call i32 @getline(i8** %8, i64* %17, i32 %78)
store i32 %79, i32* %16
br label %64
80: ; preds = %64
%81 = load i32, i32* %15
%82 = mul nsw i32 %81, 8
%83 = sdiv i32 %82, 5
store i32 %83, i32* %13
%84 = load i32, i32* %13
%85 = load i32, i32* %13
%86 = urem i32 %85, 8
%87 = sub i32 8, %86
%88 = add i32 %84, %87
store i32 %88, i32* %13
%89 = load i32, i32* %13
%90 = call i8* @malloc(i32 %89)
store i8* %90, i8** %6
%91 = load i8*, i8** %6
%92 = load i8*, i8** %9
%93 = load i32, i32* %15
%94 = load i32, i32* %13
%95 = call i32 @base32_encode(i8* %91, i8* %92, i32 %93, i32 %94)
%96 = load i8*, i8** %6
%97 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %96)
br label %100
98: ; preds = %58
%99 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.2, i64 0, i64 0))
br label %100
100: ; preds = %98, %80
br label %101
101: ; preds = %100, %35
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [3 x i8] c"-h\00"
@STDIN_FILENO = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.2 = [149 x i8] c"If no arguments given it reads from stdin until it's at the end. Otherwise it reads the first argument passed to the program as a string to encode.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i64
%7 = bitcast i8** %3 to i8*
%8 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
store i8* %8, i8** %3
%9 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%10 = bitcast %struct.timeval* %4 to i8*
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 4000, i32* %12
%13 = bitcast i32* %5 to i8*
%14 = call i32 @FD_ZERO(i32* %5)
%15 = load i32, i32* @STDIN_FILENO
%16 = call i32 @FD_SET(i32 %15, i32* %5)
%17 = icmp sgt i32 %0, 1
br i1 %17, label %18, label %34
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %34, label %23
23: ; preds = %18
%24 = load i8*, i8** %19
%25 = call i32 @strlen(i8* %24)
%26 = shl i32 %25, 3
%27 = udiv i32 %26, 5
%28 = and i32 %27, 7
%29 = sub nuw nsw i32 8, %28
%30 = add nuw nsw i32 %29, %27
%31 = call i8* @malloc(i32 %30)
%32 = call i32 @base32_encode(i8* %31, i8* %24, i32 %25, i32 %30)
%33 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %63
34: ; preds = %18, %2
%35 = call i64 @select(i32 1, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %61, label %37
37: ; preds = %34
%38 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%39 = load i32, i32* @stdin
%40 = call i32 @getline(i8** %3, i64* %6, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %51, label %42
42: ; preds = %37, %42
%43 = phi i32 [ %49, %42 ], [ %40, %37 ]
%44 = phi i32 [ %47, %42 ], [ 0, %37 ]
%45 = load i8*, i8** %3
%46 = call i32 @strcat(i8* %9, i8* %45)
store i8* null, i8** %3
%47 = add nsw i32 %43, %44
%48 = load i32, i32* @stdin
%49 = call i32 @getline(i8** %3, i64* %6, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %42
51: ; preds = %42, %37
%52 = phi i32 [ 0, %37 ], [ %47, %42 ]
%53 = shl nsw i32 %52, 3
%54 = sdiv i32 %53, 5
%55 = and i32 %54, 7
%56 = sub nuw nsw i32 8, %55
%57 = add nsw i32 %56, %54
%58 = call i8* @malloc(i32 %57)
%59 = call i32 @base32_encode(i8* %58, i8* %9, i32 %52, i32 %57)
%60 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %58)
br label %63
61: ; preds = %34
%62 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %51, %61, %23
ret i32 0
}