#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*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int ftruncate(int, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
size_t write(int, char *, size_t);
void fatal_set(char *name, int fd, char *state) {
  if (lseek(fd, 0, SEEK_SET) == -1) {
    fprintf(stderr, "%s: seek failed: %s\n", name, strerror(errno));
    exit(2);
  }
  if (ftruncate(fd, 0) == -1) {
    fprintf(stderr, "%s: truncate failed: %s\n", name, strerror(errno));
    exit(2);
  }
  size_t len = strlen(state);
  if (write(fd, state, len) != len) {
    fprintf(stderr, "%s: write failed: %s\n", name, strerror(errno));
    exit(2);
  }
}


x86 assembly (clang, -O0):
<fatal_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11e1 <fatal_set+0x71>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe2e(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1236 <fatal_set+0xc6>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdee(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x20(%rbp),%rax
je     129f <fatal_set+0x12f>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9e(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatal_set>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    %esi,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
je     11cc <fatal_set+0x5c>
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11ec <fatal_set+0x7c>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    120c <fatal_set+0x9c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e0d(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe16(%rip),%rsi
jmp    122a <fatal_set+0xba>
mov    0x2ded(%rip),%rax
mov    (%rax),%ebx
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe0b(%rip),%rsi
jmp    122a <fatal_set+0xba>
mov    0x2dcd(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe04(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: seek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"%s: truncate failed: %s\0A\00"
@.str.2 = [22 x i8] c"%s: write failed: %s\0A\00"
define void @fatal_set(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i32, i32* %5
%9 = load i32, i32* @SEEK_SET
%10 = call i32 @lseek(i32 %8, i32 0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16)
%18 = call i32 @exit(i32 2)
unreachable
19: ; preds = %3
%20 = load i32, i32* %5
%21 = call i32 @ftruncate(i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %27)
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %19
%31 = load i8*, i8** %6
%32 = call i64 @strlen(i8* %31)
store i64 %32, i64* %7
%33 = load i32, i32* %5
%34 = load i8*, i8** %6
%35 = load i64, i64* %7
%36 = call i64 @write(i32 %33, i8* %34, i64 %35)
%37 = load i64, i64* %7
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %46
39: ; preds = %30
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %4
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %41, i8* %43)
%45 = call i32 @exit(i32 2)
unreachable
46: ; preds = %30
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: seek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"%s: truncate failed: %s\0A\00"
@.str.2 = [22 x i8] c"%s: write failed: %s\0A\00"
define void @fatal_set(i8* %0, i32 %1, i8* nocapture %2) {
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @lseek(i32 %1, i32 0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %13
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %3
%14 = tail call i32 @ftruncate(i32 %1, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %19)
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %13
%23 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%24 = tail call i64 @write(i32 %1, i8* %2, i64 %23)
%25 = icmp eq i64 %24, %23
br i1 %25, label %32, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %29)
%31 = tail call i32 @exit(i32 2)
unreachable
32: ; preds = %22
ret void
}