#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*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *readstring(char *filename) {
  char *result = malloc(sizeof(char) * 20);
  FILE *tmp_file = fopen(filename, "r");
  if (!tmp_file) {
    fprintf(stderr, "%s: failed to open for reading\n", filename);
    return NULL;
  }
  if (!fscanf(tmp_file, "%s", result))
    fprintf(stderr, "Failed to resad 20 char string from %s", filename);
  fclose(tmp_file);
  return result;
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11af <readstring+0x5f>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe65(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <readstring+0xa6>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe88(%rip),%rsi
call   1040 <fscanf@plt>
cmp    $0x0,%eax
jne    11e5 <readstring+0x95>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe42(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xe94(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b4 <readstring+0x64>
mov    %rax,%rbx
lea    0xec3(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1040 <fscanf@plt>
test   %eax,%eax
jne    11aa <readstring+0x5a>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11cf <readstring+0x7f>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"%s: failed to open for reading\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [39 x i8] c"Failed to resad 20 char string from %s\00"
define i8* @readstring(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %3
%6 = call i8* @malloc(i32 20)
store i8* %6, i8** %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i8* null, i8** %2
br label %28
15: ; preds = %1
%16 = load i32*, i32** %5
%17 = load i8*, i8** %4
%18 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %3
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %20, %15
%25 = load i32*, i32** %5
%26 = call i32 @fclose(i32* %25)
%27 = load i8*, i8** %4
store i8* %27, i8** %2
br label %28
28: ; preds = %24, %11
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"%s: failed to open for reading\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [39 x i8] c"Failed to resad 20 char string from %s\00"
define i8* @readstring(i8* %0) {
%2 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
8: ; preds = %1
%9 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %14
14: ; preds = %11, %8
%15 = tail call i32 @fclose(i32* %3)
br label %16
16: ; preds = %14, %5
%17 = phi i8* [ %2, %14 ], [ null, %5 ]
ret i8* %17
}