#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*/ O_RDONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int openat(int, char const *, int /*<<< orphan*/);
const char *get_file_content(int dir, const char *file) {
  static char buf[128];
  int fd = openat(dir, file, O_RDONLY);
  if (fd < 0) {
    return NULL;
  }
  FILE *fp = fdopen(fd, "r");
  int ok = fscanf(fp, "%127s", buf);
  fclose(fp);
  return ok ? buf : NULL;
}


x86 assembly (clang, -O0):
<get_file_content>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
call   1040 <openat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117e <get_file_content+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    11cb <get_file_content+0x8b>
mov    -0x1c(%rbp),%edi
lea    0xe78(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe66(%rip),%rsi
lea    0x2ead(%rip),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%edx
xor    %eax,%eax
lea    0x2e90(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_content>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e9d(%rip),%rax
mov    (%rax),%edx
call   1040 <openat@plt>
test   %eax,%eax
js     1194 <get_file_content+0x54>
lea    0xea3(%rip),%rsi
mov    %eax,%edi
call   1060 <fdopen@plt>
mov    %rax,%rbx
lea    0xe94(%rip),%rsi
lea    0x2edb(%rip),%r14
mov    %rax,%rdi
mov    %r14,%rdx
call   1050 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
test   %ebp,%ebp
cmovne %r14,%rax
jmp    1196 <get_file_content+0x56>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@get_file_content.buf = internal global [128 x i8] zeroinitializer
@O_RDONLY = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"%127s\00"
define i8* @get_file_content(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @openat(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %27
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_file_content.buf, i64 0, i64 0))
store i32 %20, i32* %8
%21 = load i32*, i32** %7
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_file_content.buf, i64 0, i64 0), i8* null
store i8* %26, i8** %3
br label %27
27: ; preds = %16, %15
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
@get_file_content.buf = internal global [128 x i8] zeroinitializer
@O_RDONLY = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"%127s\00"
define i8* @get_file_content(i32 %0, i8* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @openat(i32 %0, i8* %1, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = tail call i32* @fdopen(i32 %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_file_content.buf, i64 0, i64 0))
%9 = tail call i32 @fclose(i32* %7)
%10 = icmp eq i32 %8, 0
%11 = select i1 %10, i8* null, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @get_file_content.buf, i64 0, i64 0)
br label %12
12: ; preds = %2, %6
%13 = phi i8* [ %11, %6 ], [ null, %2 ]
ret i8* %13
}