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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ full_name(char const *, char *, int);
int strlen(char *);
int conf_read(const char *fname, char *buf, int sz) {
  char fnbuf[512];
  FILE *f = fopen(full_name(fname, fnbuf, sizeof(fnbuf)), "r");
  if (f == NULL) {
    return errno;
  }
  fgets(buf, sz, f);
  buf[strlen(buf) - 1] = '\0';
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<conf_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <full_name@plt>
mov    %eax,%edi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    11af <conf_read+0x5f>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <conf_read+0xac>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x228(%rbp),%rdx
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x230(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x228(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conf_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rsp,%rsi
mov    $0x200,%edx
call   1050 <full_name@plt>
lea    0xe8b(%rip),%rsi
mov    %eax,%edi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ad <conf_read+0x5d>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11b6 <conf_read+0x66>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @conf_read(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%12 = call i32 @full_name(i8* %10, i8* %11, i32 512)
%13 = call i32* @fopen(i32 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* @errno
store i32 %17, i32* %4
br label %31
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = load i32*, i32** %9
%22 = call i32 @fgets(i8* %19, i32 %20, i32* %21)
%23 = load i8*, i8** %6
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %23, i64 %27
store i8 0, i8* %28
%29 = load i32*, i32** %9
%30 = call i32 @fclose(i32* %29)
store i32 0, i32* %4
br label %31
31: ; preds = %18, %16
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @conf_read(i8* %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = call i32 @full_name(i8* %0, i8* %5, i32 512)
%7 = call i32* @fopen(i32 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = load i32, i32* @errno
br label %18
11: ; preds = %3
%12 = call i32 @fgets(i8* %1, i32 %2, i32* %7)
%13 = call i32 @strlen(i8* %1)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
%17 = call i32 @fclose(i32* %7)
br label %18
18: ; preds = %11, %9
%19 = phi i32 [ %10, %9 ], [ 0, %11 ]
ret i32 %19
}