#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, ...);
char *strerror(int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void control_write(const char *dirname, const char *filename, size_t val) {
  char tmp[4096];
  int fd;
  sprintf(tmp, "%s/%s", dirname, filename);
  fd = open(tmp, O_RDWR);
  if (fd < 0) {
    printf("Couldn't open control file %s\n", tmp);
    exit(1);
  }
  sprintf(tmp, "%zu", val);
  if (write(fd, tmp, strlen(tmp)) < 0) {
    printf("Couldn't write control file %s: errcode = %d: %s\n", tmp, errno,
           strerror(errno));
    close(fd);
    exit(1);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<control_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    11fb <control_write+0x7b>
lea    -0x1020(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x1024(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12b6 <control_write+0x136>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1038(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1040(%rbp),%rsi
mov    -0x1038(%rbp),%edx
mov    %rax,%rcx
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1024(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1024(%rbp),%edi
call   1060 <close@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<control_write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rdx,%r14
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe65(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1202 <control_write+0x82>
mov    %eax,%ebp
lea    0xe5f(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     121d <control_write+0x9d>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfd(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xdf5(%rip),%rdi
mov    %rsp,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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