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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
size_t atoi(char *);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
char *strerror(int);
size_t control_read(const char *dirname, const char *filename) {
  char tmp[4096];
  int fd;
  sprintf(tmp, "%s/%s", dirname, filename);
  fd = open(tmp, O_RDONLY);
  if (fd < 0) {
    printf("Couldn't open control file %s\n", tmp);
    exit(1);
  }
  if (read(fd, tmp, sizeof(tmp)) < 0) {
    printf("Couldn't read control file %s: errcode = %d: %s\n", tmp, errno,
           strerror(errno));
    close(fd);
    exit(1);
  }
  close(fd);
  return atoi(tmp);
}


x86 assembly (clang, -O0):
<control_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe57(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    11f5 <control_read+0x75>
lea    -0x1010(%rbp),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    1274 <control_read+0xf4>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1020(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1018(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1020(%rbp),%rsi
mov    -0x1018(%rbp),%edx
mov    %rax,%rcx
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1014(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1014(%rbp),%edi
call   1040 <close@plt>
lea    -0x1010(%rbp),%rdi
call   1070 <atoi@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<control_read>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe6a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e5 <control_read+0x65>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1200 <control_read+0x80>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rsp,%rdi
call   1070 <atoi@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xe0e(%rip),%rdi
mov    %rsp,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Couldn't open control file %s\0A\00"
@.str.2 = [49 x i8] c"Couldn't read control file %s: errcode = %d: %s\0A\00"
@errno = global i32 0
define i64 @control_read(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [4096 x i8]
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = load i8*, i8** %4
%10 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9)
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %6
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%23 = call i64 @read(i32 %21, i8* %22, i32 4096)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%27 = load i32, i32* @errno
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32 %27, i8* %29)
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%38 = call i64 @atoi(i8* %37)
ret i64 %38
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Couldn't open control file %s\0A\00"
@.str.2 = [49 x i8] c"Couldn't read control file %s: errcode = %d: %s\0A\00"
@errno = global i32 0
define i64 @control_read(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = call i64 @read(i32 %7, i8* %4, i32 4096)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 %16, i8* %17)
%19 = call i32 @close(i32 %7)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = tail call i32 @close(i32 %7)
%23 = call i64 @atoi(i8* %4)
ret i64 %23
}