#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 */

/* Variables and functions */
int EACCES;
int ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDERR_FILENO;
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int try_open_file(char *path) {
  int file_descriptor = open(path, O_RDONLY);
  if (file_descriptor < 0) {
    int errnum = errno;
    int total_size = 51 + 40;
    char error_message[total_size + 1];
    error_message[total_size] = '\0';
    if (errnum == ENOENT) {
      snprintf(error_message, total_size,
               "cannot open '%s' for reading: No such file or directory\n",
               path);
      int i;
      for (i = 0; i < total_size; i++) {
        write(STDERR_FILENO, &error_message[i], 1);
        if (error_message[i] == '\n') {
          break;
        }
      }
    } else if (errnum == EACCES) {
      snprintf(error_message, total_size,
               "cannot open '%s' for reading: Permission denied\n", path);
      int i;
      for (i = 0; i < total_size; i++) {
        write(STDERR_FILENO, &error_message[i], 1);
        if (error_message[i] == '\n') {
          break;
        }
      }
    }
    return -1;
  } else {
    return file_descriptor;
  }
}


x86 assembly (clang, -O0):
<try_open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12d1 <try_open_file+0x1a1>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x5b,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x40(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x30(%rbp)
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%eax
jne    122d <try_open_file+0xfd>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xe3b(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1228 <try_open_file+0xf8>
mov    -0x40(%rbp),%rsi
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x40(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1215 <try_open_file+0xe5>
jmp    1228 <try_open_file+0xf8>
jmp    121a <try_open_file+0xea>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d1 <try_open_file+0xa1>
jmp    12be <try_open_file+0x18e>
mov    -0x18(%rbp),%eax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%eax
jne    12b9 <try_open_file+0x189>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xde8(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12b4 <try_open_file+0x184>
mov    -0x40(%rbp),%rsi
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
movslq -0x38(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12a1 <try_open_file+0x171>
jmp    12b4 <try_open_file+0x184>
jmp    12a6 <try_open_file+0x176>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    125d <try_open_file+0x12d>
jmp    12b9 <try_open_file+0x189>
jmp    12be <try_open_file+0x18e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
jmp    12d7 <try_open_file+0x1a7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_open_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
jns    122d <try_open_file+0xfd>
mov    %rsp,%r15
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
lea    -0x60(%rcx),%r12
mov    %r12,%rsp
movb   $0x0,-0x5(%rcx)
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%eax
jne    11c7 <try_open_file+0x97>
lea    0xe80(%rip),%rdx
mov    %r12,%rdi
mov    $0x5b,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
xor    %ebx,%ebx
mov    0x2e2f(%rip),%r14
nopl   0x0(%rax)
mov    (%r14),%edi
lea    (%r12,%rbx,1),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmpb   $0xa,(%r12,%rbx,1)
je     1225 <try_open_file+0xf5>
lea    0x1(%rbx),%rax
cmp    $0x5a,%rbx
mov    %rax,%rbx
jne    11a0 <try_open_file+0x70>
jmp    1225 <try_open_file+0xf5>
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
jne    1225 <try_open_file+0xf5>
lea    0xe60(%rip),%rdx
mov    %r12,%rdi
mov    $0x5b,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
xor    %ebx,%ebx
mov    0x2dd6(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%edi
lea    (%r12,%rbx,1),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmpb   $0xa,(%r12,%rbx,1)
je     1225 <try_open_file+0xf5>
lea    0x1(%rbx),%rax
cmp    $0x5a,%rbx
mov    %rax,%rbx
jne    1200 <try_open_file+0xd0>
mov    %r15,%rsp
mov    $0xffffffff,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@.str = [57 x i8] c"cannot open '%s' for reading: No such file or directory\0A\00"
@STDERR_FILENO = global i32 0
@EACCES = global i32 0
@.str.1 = [49 x i8] c"cannot open '%s' for reading: Permission denied\0A\00"
define i32 @try_open_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %88
16: ; preds = %1
%17 = load i32, i32* @errno
store i32 %17, i32* %5
store i32 91, i32* %6
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %7
%22 = alloca i8, i64 %20
store i64 %20, i64* %8
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* %5
%27 = load i32, i32* @ENOENT
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %55
29: ; preds = %16
%30 = load i32, i32* %6
%31 = load i8*, i8** %3
%32 = call i32 @snprintf(i8* %22, i32 %30, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %31)
store i32 0, i32* %9
br label %33
33: ; preds = %51, %29
%34 = load i32, i32* %9
%35 = load i32, i32* %6
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %54
37: ; preds = %33
%38 = load i32, i32* @STDERR_FILENO
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %22, i64 %40
%42 = call i32 @write(i32 %38, i8* %41, i32 1)
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %22, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %50
49: ; preds = %37
br label %54
50: ; preds = %37
br label %51
51: ; preds = %50
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %33
54: ; preds = %49, %33
br label %86
55: ; preds = %16
%56 = load i32, i32* %5
%57 = load i32, i32* @EACCES
%58 = icmp eq i32 %56, %57
br i1 %58, label %59, label %85
59: ; preds = %55
%60 = load i32, i32* %6
%61 = load i8*, i8** %3
%62 = call i32 @snprintf(i8* %22, i32 %60, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %61)
store i32 0, i32* %10
br label %63
63: ; preds = %81, %59
%64 = load i32, i32* %10
%65 = load i32, i32* %6
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %84
67: ; preds = %63
%68 = load i32, i32* @STDERR_FILENO
%69 = load i32, i32* %10
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %22, i64 %70
%72 = call i32 @write(i32 %68, i8* %71, i32 1)
%73 = load i32, i32* %10
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %22, i64 %74
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 10
br i1 %78, label %79, label %80
79: ; preds = %67
br label %84
80: ; preds = %67
br label %81
81: ; preds = %80
%82 = load i32, i32* %10
%83 = add nsw i32 %82, 1
store i32 %83, i32* %10
br label %63
84: ; preds = %79, %63
br label %85
85: ; preds = %84, %55
br label %86
86: ; preds = %85, %54
store i32 -1, i32* %2
%87 = load i8*, i8** %7
br label %90
88: ; preds = %1
%89 = load i32, i32* %4
store i32 %89, i32* %2
br label %90
90: ; preds = %88, %86
%91 = load i32, i32* %2
ret i32 %91
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@.str = [57 x i8] c"cannot open '%s' for reading: No such file or directory\0A\00"
@STDERR_FILENO = global i32 0
@EACCES = global i32 0
@.str.1 = [49 x i8] c"cannot open '%s' for reading: Permission denied\0A\00"
define i32 @try_open_file(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %41
5: ; preds = %1
%6 = load i32, i32* @errno
%7 = tail call i8* @llvm.stacksave()
%8 = alloca [92 x i8]
%9 = getelementptr inbounds [92 x i8], [92 x i8]* %8, i64 0, i64 0
%10 = getelementptr inbounds [92 x i8], [92 x i8]* %8, i64 0, i64 91
store i8 0, i8* %10
%11 = load i32, i32* @ENOENT
%12 = icmp eq i32 %6, %11
br i1 %12, label %13, label %25
13: ; preds = %5
%14 = call i32 @snprintf(i8* %9, i32 91, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %15, %13
%16 = phi i64 [ 0, %13 ], [ %22, %15 ]
%17 = load i32, i32* @STDERR_FILENO
%18 = getelementptr inbounds [92 x i8], [92 x i8]* %8, i64 0, i64 %16
%19 = call i32 @write(i32 %17, i8* %18, i32 1)
%20 = load i8, i8* %18
%21 = icmp eq i8 %20, 10
%22 = add nuw nsw i64 %16, 1
%23 = icmp eq i64 %22, 91
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %40, label %15
25: ; preds = %5
%26 = load i32, i32* @EACCES
%27 = icmp eq i32 %6, %26
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = call i32 @snprintf(i8* %9, i32 91, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %30, %28
%31 = phi i64 [ 0, %28 ], [ %37, %30 ]
%32 = load i32, i32* @STDERR_FILENO
%33 = getelementptr inbounds [92 x i8], [92 x i8]* %8, i64 0, i64 %31
%34 = call i32 @write(i32 %32, i8* %33, i32 1)
%35 = load i8, i8* %33
%36 = icmp eq i8 %35, 10
%37 = add nuw nsw i64 %31, 1
%38 = icmp eq i64 %37, 91
%39 = select i1 %36, i1 true, i1 %38
br i1 %39, label %40, label %30
40: ; preds = %30, %15, %25
br label %41
41: ; preds = %1, %40
%42 = phi i32 [ -1, %40 ], [ %3, %1 ]
ret i32 %42
}