#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 stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int convert(void) {
  FILE *fp;
  struct stat statbuf;
  char *p, *q;
  int n;
  int res = -1;
  if (stat("text.txt", &statbuf) == -1) {
    perror("fail to get stat");
  }
  fp = fopen("text.txt", "rb");
  if (fp == NULL) {
    perror("fail to open");
    return res;
  }
  p = (char *)malloc(sizeof(char) * (statbuf.st_size + 1));
  if (p == NULL) {
    perror("fail to malloc");
    goto memory_err;
  }
  n = fread(p, sizeof(char), statbuf.st_size, fp);
  if (n == -1) {
    perror("fail to read");
    goto read_err;
  }
  *(p + n) = '\0';
  q = p;
  while (*q != '\0') {
    if (('a' <= *q) && (*q <= 'z'))
      *q -= 32;
    printf("%c", *q);
    q++;
  }
  printf("\n");
  res = 0;
read_err:
  free(p);
memory_err:
  fclose(fp);
  return res;
}


x86 assembly (clang, -O0):
<convert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0x30(%rbp)
lea    0xe6a(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b4 <convert+0x34>
lea    0xe5a(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe45(%rip),%rdi
lea    0xe58(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ed <convert+0x6d>
lea    0xe40(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12fa <convert+0x17a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1220 <convert+0xa0>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
jmp    12eb <convert+0x16b>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1253 <convert+0xd3>
lea    0xdf0(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e2 <convert+0x162>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12cd <convert+0x14d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x61,%eax
cmp    %ecx,%eax
jg     12a7 <convert+0x127>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7a,%eax
jg     12a7 <convert+0x127>
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
sub    $0x20,%ecx
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1267 <convert+0xe7>
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe72(%rip),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     125b <convert+0xdb>
lea    0xe5a(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     126c <convert+0xec>
mov    %rax,%r14
mov    (%rsp),%ebp
lea    0x1(%rbp),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     127f <convert+0xff>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0xffffffff,%eax
je     1292 <convert+0x112>
cltq
movb   $0x0,(%r15,%rax,1)
mov    (%r15),%al
test   %al,%al
je     1232 <convert+0xb2>
mov    %r15,%rbx
add    $0x1,%rbx
jmp    1223 <convert+0xa3>
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1040 <putchar@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1232 <convert+0xb2>
lea    -0x61(%rax),%ecx
cmp    $0x19,%cl
ja     1210 <convert+0x90>
add    $0xe0,%al
mov    %al,-0x1(%rbx)
jmp    1210 <convert+0x90>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %ebp,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda7(%rip),%rdi
call   10a0 <perror@plt>
jmp    119f <convert+0x1f>
lea    0xdaa(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    124e <convert+0xce>
lea    0xda4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1246 <convert+0xc6>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    123e <convert+0xbe>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"text.txt\00"
@.str.1 = [17 x i8] c"fail to get stat\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [13 x i8] c"fail to open\00"
@.str.4 = [15 x i8] c"fail to malloc\00"
@.str.5 = [13 x i8] c"fail to read\00"
@.str.6 = [3 x i8] c"%c\00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @convert() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 -1, i32* %7
%8 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* %7
store i32 %18, i32* %1
br label %85
19: ; preds = %12
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %19
%31 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %81
32: ; preds = %19
%33 = load i8*, i8** %4
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32*, i32** %2
%37 = call i32 @fread(i8* %33, i32 1, i32 %35, i32* %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %78
42: ; preds = %32
%43 = load i8*, i8** %4
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %4
store i8* %47, i8** %5
br label %48
48: ; preds = %69, %42
%49 = load i8*, i8** %5
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %76
53: ; preds = %48
%54 = load i8*, i8** %5
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp sle i32 97, %56
br i1 %57, label %58, label %69
58: ; preds = %53
%59 = load i8*, i8** %5
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp sle i32 %61, 122
br i1 %62, label %63, label %69
63: ; preds = %58
%64 = load i8*, i8** %5
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = sub nsw i32 %66, 32
%68 = trunc i32 %67 to i8
store i8 %68, i8* %64
br label %69
69: ; preds = %63, %58, %53
%70 = load i8*, i8** %5
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %72)
%74 = load i8*, i8** %5
%75 = getelementptr inbounds i8, i8* %74, i32 1
store i8* %75, i8** %5
br label %48
76: ; preds = %48
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %7
br label %78
78: ; preds = %76, %40
%79 = load i8*, i8** %4
%80 = call i32 @free(i8* %79)
br label %81
81: ; preds = %78, %30
%82 = load i32*, i32** %2
%83 = call i32 @fclose(i32* %82)
%84 = load i32, i32* %7
store i32 %84, i32* %1
br label %85
85: ; preds = %81, %16
%86 = load i32, i32* %1
ret i32 %86
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"text.txt\00"
@.str.1 = [17 x i8] c"fail to get stat\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [13 x i8] c"fail to open\00"
@.str.4 = [15 x i8] c"fail to malloc\00"
@.str.5 = [13 x i8] c"fail to read\00"
define i32 @convert() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %53
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %50
21: ; preds = %12
%22 = tail call i32 @fread(i8* %17, i32 1, i32 %14, i32* %8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %47
26: ; preds = %21
%27 = sext i32 %22 to i64
%28 = getelementptr inbounds i8, i8* %17, i64 %27
store i8 0, i8* %28
%29 = load i8, i8* %17
%30 = icmp eq i8 %29, 0
br i1 %30, label %45, label %31
31: ; preds = %26, %38
%32 = phi i8 [ %43, %38 ], [ %29, %26 ]
%33 = phi i8* [ %42, %38 ], [ %17, %26 ]
%34 = add i8 %32, -97
%35 = icmp ult i8 %34, 26
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = add nsw i8 %32, -32
store i8 %37, i8* %33
br label %38
38: ; preds = %36, %31
%39 = phi i8 [ %37, %36 ], [ %32, %31 ]
%40 = sext i8 %39 to i32
%41 = tail call i32 @putchar(i32 %40)
%42 = getelementptr inbounds i8, i8* %33, i64 1
%43 = load i8, i8* %42
%44 = icmp eq i8 %43, 0
br i1 %44, label %45, label %31
45: ; preds = %38, %26
%46 = tail call i32 @putchar(i32 10)
br label %47
47: ; preds = %45, %24
%48 = phi i32 [ -1, %24 ], [ 0, %45 ]
%49 = tail call i32 @free(i8* %17)
br label %50
50: ; preds = %47, %19
%51 = phi i32 [ -1, %19 ], [ %48, %47 ]
%52 = tail call i32 @fclose(i32* %8)
br label %53
53: ; preds = %50, %10
%54 = phi i32 [ -1, %10 ], [ %51, %50 ]
ret i32 %54
}