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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
long strlen(char *);
long int read_buffer(int opt, void *ptr, size_t *len) {
  switch (opt) {
  case 0: {
    return 0;
  }
  case 1: {
    int *n = (int *)ptr;
    return *n;
  }
  case 2: {
    long int *n = (long int *)ptr;
    return *n;
  }
  case 3: {
    char *s = (char *)ptr;
    return strlen(s);
  }
  case 4: {
    char *b = (char *)ptr;
    char *s = (char *)malloc(*len + 1);
    memcpy(s, b, *len);
    s[*len] = '\0';
    return *len + 1;
  }
  case 5: {
    char *b = (char *)ptr;
    FILE *h = fopen("dump", "w");
    int i;
    for (i = 0; i < *len; ++i)
      fputc(b[i], h);
    fclose(h);
    return *len;
  }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %rax,-0x68(%rbp)
sub    $0x5,%rax
ja     12b7 <read_buffer+0x157>
mov    -0x68(%rbp),%rax
lea    0xe71(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movq   $0x0,-0x8(%rbp)
jmp    12bf <read_buffer+0x15f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12bf <read_buffer+0x15f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12bf <read_buffer+0x15f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x8(%rbp)
jmp    12bf <read_buffer+0x15f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    12bf <read_buffer+0x15f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    0xdc7(%rip),%rdi
lea    0xdc5(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
movslq -0x5c(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jae    129e <read_buffer+0x13e>
mov    -0x50(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x58(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1268 <read_buffer+0x108>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12bf <read_buffer+0x15f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_buffer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
add    $0xffffffff,%edi
cmp    $0x4,%edi
ja     118e <read_buffer+0x2e>
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe83(%rip),%rax
movslq (%rax,%rdi,4),%rcx
add    %rax,%rcx
jmp    *%rcx
movslq (%r15),%rax
jmp    1221 <read_buffer+0xc1>
xor    %eax,%eax
jmp    1221 <read_buffer+0xc1>
mov    (%r15),%rax
jmp    1221 <read_buffer+0xc1>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <strlen@plt>
mov    (%r14),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    (%r14),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <memcpy@plt>
mov    (%r14),%rax
movb   $0x0,(%rbx,%rax,1)
mov    (%r14),%rax
add    $0x1,%rax
jmp    1221 <read_buffer+0xc1>
lea    0xe30(%rip),%rdi
lea    0xe2e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r12
cmpq   $0x0,(%r14)
je     1216 <read_buffer+0xb6>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
add    $0x1,%rbx
cmp    %rbx,(%r14)
ja     1200 <read_buffer+0xa0>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    (%r14),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"dump\00"
@.str.1 = [2 x i8] c"w\00"
define i64 @read_buffer(i32 %0, i8* %1, i64* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i64*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64* %2, i64** %7
%16 = load i32, i32* %5
switch i32 %16, label %77 [
i32 0, label %17
i32 1, label %18
i32 2, label %24
i32 3, label %29
i32 4, label %33
i32 5, label %52
]
17: ; preds = %3
store i64 0, i64* %4
br label %78
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = bitcast i8* %19 to i32*
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = load i32, i32* %21
%23 = sext i32 %22 to i64
store i64 %23, i64* %4
br label %78
24: ; preds = %3
%25 = load i8*, i8** %6
%26 = bitcast i8* %25 to i64*
store i64* %26, i64** %9
%27 = load i64*, i64** %9
%28 = load i64, i64* %27
store i64 %28, i64* %4
br label %78
29: ; preds = %3
%30 = load i8*, i8** %6
store i8* %30, i8** %10
%31 = load i8*, i8** %10
%32 = call i64 @strlen(i8* %31)
store i64 %32, i64* %4
br label %78
33: ; preds = %3
%34 = load i8*, i8** %6
store i8* %34, i8** %11
%35 = load i64*, i64** %7
%36 = load i64, i64* %35
%37 = add i64 %36, 1
%38 = call i64 @malloc(i64 %37)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %12
%40 = load i8*, i8** %12
%41 = load i8*, i8** %11
%42 = load i64*, i64** %7
%43 = load i64, i64* %42
%44 = call i32 @memcpy(i8* %40, i8* %41, i64 %43)
%45 = load i8*, i8** %12
%46 = load i64*, i64** %7
%47 = load i64, i64* %46
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i64*, i64** %7
%50 = load i64, i64* %49
%51 = add i64 %50, 1
store i64 %51, i64* %4
br label %78
52: ; preds = %3
%53 = load i8*, i8** %6
store i8* %53, i8** %13
%54 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %54, i32** %14
store i32 0, i32* %15
br label %55
55: ; preds = %69, %52
%56 = load i32, i32* %15
%57 = sext i32 %56 to i64
%58 = load i64*, i64** %7
%59 = load i64, i64* %58
%60 = icmp ult i64 %57, %59
br i1 %60, label %61, label %72
61: ; preds = %55
%62 = load i8*, i8** %13
%63 = load i32, i32* %15
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = load i8, i8* %65
%67 = load i32*, i32** %14
%68 = call i32 @fputc(i8 signext %66, i32* %67)
br label %69
69: ; preds = %61
%70 = load i32, i32* %15
%71 = add nsw i32 %70, 1
store i32 %71, i32* %15
br label %55
72: ; preds = %55
%73 = load i32*, i32** %14
%74 = call i32 @fclose(i32* %73)
%75 = load i64*, i64** %7
%76 = load i64, i64* %75
store i64 %76, i64* %4
br label %78
77: ; preds = %3
store i64 0, i64* %4
br label %78
78: ; preds = %77, %72, %33, %29, %24, %18, %17
%79 = load i64, i64* %4
ret i64 %79
}

LLVM IR (-O2):
@.str = [5 x i8] c"dump\00"
@.str.1 = [2 x i8] c"w\00"
define i64 @read_buffer(i32 %0, i8* %1, i64* nocapture %2) {
switch i32 %0, label %39 [
i32 5, label %24
i32 1, label %4
i32 2, label %8
i32 3, label %11
i32 4, label %13
]
4: ; preds = %3
%5 = bitcast i8* %1 to i32*
%6 = load i32, i32* %5
%7 = sext i32 %6 to i64
br label %39
8: ; preds = %3
%9 = bitcast i8* %1 to i64*
%10 = load i64, i64* %9
br label %39
11: ; preds = %3
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
br label %39
13: ; preds = %3
%14 = load i64, i64* %2
%15 = add i64 %14, 1
%16 = tail call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
%18 = load i64, i64* %2
%19 = tail call i32 @memcpy(i8* %17, i8* %1, i64 %18)
%20 = load i64, i64* %2
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8 0, i8* %21
%22 = load i64, i64* %2
%23 = add i64 %22, 1
br label %39
24: ; preds = %3
%25 = tail call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = load i64, i64* %2
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %24, %28
%29 = phi i64 [ %33, %28 ], [ 0, %24 ]
%30 = getelementptr inbounds i8, i8* %1, i64 %29
%31 = load i8, i8* %30
%32 = tail call i32 @fputc(i8 signext %31, i32* %25)
%33 = add nuw i64 %29, 1
%34 = load i64, i64* %2
%35 = icmp ugt i64 %34, %33
br i1 %35, label %28, label %36
36: ; preds = %28, %24
%37 = tail call i32 @fclose(i32* %25)
%38 = load i64, i64* %2
br label %39
39: ; preds = %3, %36, %13, %11, %8, %4
%40 = phi i64 [ %38, %36 ], [ %23, %13 ], [ %12, %11 ], [ %10, %8 ], [ %7, %4 ], [ 0, %3 ]
ret i64 %40
}