#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*/ str;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  FILE *fd;
  char str[1024];
  printf("Hello world from app 2\n");
  if ((fd = fopen("/tmp/file2", "r")) == NULL) {
    printf("Failed to open file\n");
    goto end;
  }
  if (fgets(str, sizeof(str), fd) == NULL) {
    printf("Failed to read from file\n");
    goto end;
  }
  printf("Read from /tmp/file2: %s\n", str);
end:
  while (1)
    sleep(100000);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeaa(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119b <main+0x5b>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e2 <main+0xa2>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11cd <main+0x8d>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e2 <main+0xa2>
lea    -0x420(%rbp),%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e7 <main+0xa7>
mov    $0x186a0,%edi
call   1060 <sleep@plt>
jmp    11e7 <main+0xa7>

x86 assembly (clang, -O2):
<main>:
sub    $0x408,%rsp
lea    0xec9(%rip),%rdi
call   1030 <puts@plt>
lea    0xe96(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a3 <main+0x53>
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b1 <main+0x61>
lea    0xe76(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c0 <main+0x70>
lea    0xead(%rip),%rdi
call   1030 <puts@plt>
jmp    11c0 <main+0x70>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
nopl   (%rax)
mov    $0x186a0,%edi
call   1070 <sleep@plt>
jmp    11c0 <main+0x70>

LLVM IR (-O0):
@.str = [24 x i8] c"Hello world from app 2\0A\00"
@.str.1 = [11 x i8] c"/tmp/file2\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [21 x i8] c"Failed to open file\0A\00"
@.str.4 = [26 x i8] c"Failed to read from file\0A\00"
@.str.5 = [26 x i8] c"Read from /tmp/file2: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 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* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %23
13: ; preds = %2
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%15 = load i32*, i32** %6
%16 = call i32* @fgets(i8* %14, i32 1024, i32* %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %23
20: ; preds = %13
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %20, %18, %11
br label %24
24: ; preds = %23, %24
%25 = call i32 @sleep(i32 100000)
br label %24
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"/tmp/file2\00"
@.str.2 = [2 x i8] c"r\00"
@.str.5 = [26 x i8] c"Read from /tmp/file2: %s\0A\00"
@str = [23 x i8] c"Hello world from app 2\00"
@str.6 = [25 x i8] c"Failed to read from file\00"
@str.7 = [20 x i8] c"Failed to open file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
br label %17
10: ; preds = %2
%11 = call i32* @fgets(i8* %4, i32 1024, i32* %6)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
br label %17
15: ; preds = %10
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %4)
br label %17
17: ; preds = %15, %13, %8
br label %18
18: ; preds = %17, %18
%19 = call i32 @sleep(i32 100000)
br label %18
}