#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdin;
int strlen(char *);
scalar_t__ toupper(int);
int main(void) {
  char *s = malloc(sizeof(char) * (128 + 1));
  FILE *f;
  int i;
  sleep(2);
  if (s == NULL) {
    printf("FATAL, no mem.\n");
    return 1;
  }
  fgets(s, 128, stdin);
  for (i = 0; i < strlen(s); i++) {
    s[i] = (char)toupper((int)s[i]);
  }
  sleep(1);
  f = fopen("/tmp/something.txt", "w");
  if (f == NULL) {
    printf("%s", s);
  } else {
    fprintf(f, "%s", s);
    fclose(f);
  }
  free(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x81,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x2,%edi
call   10c0 <sleep@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11ec <main+0x4c>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c2 <main+0x122>
mov    -0x10(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1080 <fgets@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    1252 <main+0xb2>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <toupper@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120a <main+0x6a>
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    0xdad(%rip),%rdi
lea    0xdb9(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1295 <main+0xf5>
mov    -0x10(%rbp),%rsi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12b2 <main+0x112>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd81(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x81,%edi
call   10b0 <malloc@plt>
mov    %rax,%r15
mov    $0x2,%edi
call   10d0 <sleep@plt>
test   %r15,%r15
je     125b <main+0xab>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    $0x80,%esi
call   10a0 <fgets@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
jle    1221 <main+0x71>
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%r15,%rbx,1),%edi
call   1040 <toupper@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1070 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1200 <main+0x50>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xdce(%rip),%rdi
lea    0xdda(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     126e <main+0xbe>
mov    %rax,%r14
mov    %r15,%rdi
mov    %rax,%rsi
call   1090 <fputs@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
jmp    127f <main+0xcf>
lea    0xdb6(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
jmp    1289 <main+0xd9>
lea    0xda0(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"FATAL, no mem.\0A\00"
@stdin = global i32 0
@.str.1 = [19 x i8] c"/tmp/something.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i8* @malloc(i32 129)
store i8* %5, i8** %2
%6 = call i32 @sleep(i32 2)
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %53
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %12, i32 128, i32 %13)
store i32 0, i32* %4
br label %15
15: ; preds = %33, %11
%16 = load i32, i32* %4
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %36
20: ; preds = %15
%21 = load i8*, i8** %2
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = call i64 @toupper(i32 %26)
%28 = trunc i64 %27 to i8
%29 = load i8*, i8** %2
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %20
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %15
36: ; preds = %15
%37 = call i32 @sleep(i32 1)
%38 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %38, i32** %3
%39 = load i32*, i32** %3
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i8*, i8** %2
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %50
44: ; preds = %36
%45 = load i32*, i32** %3
%46 = load i8*, i8** %2
%47 = call i32 @fprintf(i32* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %3
%49 = call i32 @fclose(i32* %48)
br label %50
50: ; preds = %44, %41
%51 = load i8*, i8** %2
%52 = call i32 @free(i8* %51)
store i32 0, i32* %1
br label %53
53: ; preds = %50, %9
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [19 x i8] c"/tmp/something.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"%s\00"
@str = [15 x i8] c"FATAL, no mem.\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(129) i8* @malloc(i32 129)
%2 = tail call i32 @sleep(i32 2)
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %33
6: ; preds = %0
%7 = load i32, i32* @stdin
%8 = tail call i32 @fgets(i8* %1, i32 128, i32 %7)
%9 = tail call i32 @strlen(i8* %1)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %6, %11
%12 = phi i64 [ %18, %11 ], [ 0, %6 ]
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = tail call i64 @toupper(i32 %15)
%17 = trunc i64 %16 to i8
store i8 %17, i8* %13
%18 = add nuw nsw i64 %12, 1
%19 = tail call i32 @strlen(i8* %1)
%20 = sext i32 %19 to i64
%21 = icmp slt i64 %18, %20
br i1 %21, label %11, label %22
22: ; preds = %11, %6
%23 = tail call i32 @sleep(i32 1)
%24 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %31
28: ; preds = %22
%29 = tail call i32 @fputs(i8* %1, i32* %24)
%30 = tail call i32 @fclose(i32* %24)
br label %31
31: ; preds = %28, %26
%32 = tail call i32 @free(i8* %1)
br label %33
33: ; preds = %31, %4
%34 = phi i32 [ 1, %4 ], [ 0, %31 ]
ret i32 %34
}