#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <file>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  FILE *fp = fopen(argv[1], "r");
  if (fp == NULL) {
    fprintf(stderr, "Error opening file: %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  char buf[4];
  char *rval;
  rval = fgets(buf, 4, fp);
  printf("buf = %s, rval = %p\n", buf, rval);
  printf("buf[3] = %d\n", buf[3]);
  int c = fread(buf, sizeof(char), 4, fp);
  printf("buf = %s, c = %d\n", buf, c);
  printf("buf[3] = %d\n", buf[3]);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11aa <main+0x4a>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe59(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f8 <main+0x98>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe2f(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    -0x1c(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x4,%esi
call   1050 <fgets@plt>
mov    %rax,-0x28(%rbp)
lea    -0x1c(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x19(%rbp),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1c(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x1c(%rbp),%rsi
mov    -0x2c(%rbp),%edx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x19(%rbp),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    121a <main+0xba>
mov    0x8(%r14),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     122f <main+0xcf>
mov    %rax,%rbx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
mov    $0x4,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
lea    0xe7f(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0xf(%rsp),%esi
lea    0xe7b(%rip),%r15
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xe62(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0xf(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdd3(%rip),%rsi
jmp    1243 <main+0xe3>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xdd1(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Error opening file: %s\0A\00"
@.str.3 = [21 x i8] c"buf = %s, rval = %p\0A\00"
@.str.4 = [13 x i8] c"buf[3] = %d\0A\00"
@.str.5 = [18 x i8] c"buf = %s, c = %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [4 x i8]
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i32*, i32** %6
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %35
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %20
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%37 = load i32*, i32** %6
%38 = call i8* @fgets(i8* %36, i32 4, i32* %37)
store i8* %38, i8** %8
%39 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%40 = load i8*, i8** %8
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %39, i8* %40)
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 3
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%47 = load i32*, i32** %6
%48 = call i32 @fread(i8* %46, i32 1, i32 4, i32* %47)
store i32 %48, i32* %9
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %49, i32 %50)
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 3
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %54)
%56 = load i32, i32* @EXIT_SUCCESS
ret i32 %56
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Error opening file: %s\0A\00"
@.str.3 = [21 x i8] c"buf = %s, rval = %p\0A\00"
@.str.4 = [13 x i8] c"buf[3] = %d\0A\00"
@.str.5 = [18 x i8] c"buf = %s, c = %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = icmp eq i32 %0, 2
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %12
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %11
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%24 = call i8* @fgets(i8* %23, i32 4, i32* %14)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %23, i8* %24)
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = call i32 @fread(i8* %23, i32 1, i32 4, i32* %14)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %23, i32 %30)
%32 = load i8, i8* %26
%33 = sext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @EXIT_SUCCESS
ret i32 %35
}