#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 *, ...);
int ftruncate(int, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
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, int value) {
  char buf[10] = {0};
  if (snprintf(buf, 10 - 1, "%u", value) >= 10) {
    fprintf(stderr, "%s: too many increments\n", name);
    exit(2);
  }
  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(buf);
  if (write(fd, buf, 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    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x1a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <memset@plt>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%ecx
mov    $0x9,%esi
lea    0xe3b(%rip),%rdx
call   1050 <snprintf@plt>
cmp    $0xa,%eax
jl     11f8 <fatal_set+0x68>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1258 <fatal_set+0xc8>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12af <fatal_set+0x11f>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    -0x1a(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x28(%rbp),%rax
je     131a <fatal_set+0x18a>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd41(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatal_set>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ecx
mov    %esi,%ebp
mov    %rdi,%r14
movw   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe58(%rip),%rdx
lea    0x8(%rsp),%rdi
mov    $0x9,%esi
call   1050 <snprintf@plt>
cmp    $0xa,%eax
jge    120e <fatal_set+0x8e>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     1232 <fatal_set+0xb2>
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1252 <fatal_set+0xd2>
lea    0x8(%rsp),%r15
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    1274 <fatal_set+0xf4>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%ebp
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdcc(%rip),%rsi
jmp    1270 <fatal_set+0xf0>
mov    0x2d87(%rip),%rax
mov    (%rax),%ebp
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc1(%rip),%rsi
mov    %ebp,%edi
jmp    1294 <fatal_set+0x114>
mov    0x2d65(%rip),%rax
mov    (%rax),%ebx
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb8(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

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

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