#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ stderr;
int read_char(const char *path, char *val) {
  FILE *f;
  int ret;
  f = fopen(path, "r");
  if (!f) {
    fprintf(stderr, "failed to open '%s': %m\n", path);
    return -1;
  }
  ret = fscanf(f, "%c", val);
  fclose(f);
  if (ret != 1) {
    fprintf(stderr, "%s: failed to parse a char\n", path);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1194 <read_char+0x54>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe7f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <read_char+0xaa>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe78(%rip),%rsi
call   1040 <fscanf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x1,-0x24(%rbp)
je     11e3 <read_char+0xa3>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <read_char+0xaa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xeac(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     118d <read_char+0x4d>
mov    %rax,%rbx
lea    0xeb3(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    $0x1,%ebp
je     11aa <read_char+0x6a>
lea    0xe93(%rip),%rsi
jmp    1194 <read_char+0x54>
lea    0xe6e(%rip),%rsi
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"failed to open '%s': %m\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [28 x i8] c"%s: failed to parse a char\0A\00"
define i32 @read_char(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %29
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i8*, i8** %5
%19 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 %19, i32* %7
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %4
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %26)
store i32 -1, i32* %3
br label %29
28: ; preds = %16
store i32 0, i32* %3
br label %29
29: ; preds = %28, %24, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"failed to open '%s': %m\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [28 x i8] c"%s: failed to parse a char\0A\00"
define i32 @read_char(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%7 = tail call i32 @fclose(i32* %3)
%8 = icmp eq i32 %6, 1
br i1 %8, label %13, label %9
9: ; preds = %5, %2
%10 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), %5 ]
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* %10, i8* %0)
br label %13
13: ; preds = %9, %5
%14 = phi i32 [ 0, %5 ], [ -1, %9 ]
ret i32 %14
}