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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, char *, int);
int open(char *, int /*<<< orphan*/);
int read_all(int, unsigned char *, int /*<<< orphan*/);
scalar_t__ sigbufsize(char *, int /*<<< orphan*/ *);
int write_all(int, unsigned char *, int /*<<< orphan*/);
int writesig(char *path) {
  unsigned char *buf = NULL;
  int fd = -1, n;
  off_t blocksize;
  if (sigbufsize(path, &blocksize) < 0)
    goto error;
  if (!(buf = malloc(blocksize)))
    goto nomem;
  if ((fd = open(path, O_RDWR)) < 0)
    goto error;
  if ((n = read_all(fd, buf, blocksize)) < 0)
    goto error;
  memcpy(buf, "\001\002\003SCRUBBED!", sizeof("\001\002\003SCRUBBED!"));
  if (lseek(fd, 0, SEEK_SET) < 0)
    goto error;
  if ((n = write_all(fd, buf, blocksize)) < 0)
    goto error;
  if (n == 0) {
    errno = EINVAL;
    goto error;
  }
  if (close(fd) < 0)
    goto error;
  free(buf);
  return 0;
nomem:
  errno = ENOMEM;
error:
  if (buf)
    free(buf);
  if (fd != -1)
    (void)close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<writesig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1090 <sigbufsize@plt>
cmp    $0x0,%rax
jge    11c7 <writesig+0x37>
jmp    12db <writesig+0x14b>
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e2 <writesig+0x52>
jmp    12c9 <writesig+0x139>
mov    -0x10(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1205 <writesig+0x75>
jmp    12db <writesig+0x14b>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   10b0 <read_all@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1225 <writesig+0x95>
jmp    12db <writesig+0x14b>
mov    -0x18(%rbp),%rdi
lea    0xdd0(%rip),%rsi
mov    $0xd,%edx
call   1060 <memcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    125c <writesig+0xcc>
jmp    12db <writesig+0x14b>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <write_all@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    127c <writesig+0xec>
jmp    12db <writesig+0x14b>
cmpl   $0x0,-0x20(%rbp)
jne    129d <writesig+0x10d>
mov    0x2d33(%rip),%rax
mov    (%rax),%ecx
mov    0x2d3a(%rip),%rax
mov    %ecx,(%rax)
jmp    12db <writesig+0x14b>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    12b4 <writesig+0x124>
jmp    12db <writesig+0x14b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1308 <writesig+0x178>
mov    0x2d08(%rip),%rax
mov    (%rax),%ecx
mov    0x2cf7(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     12ef <writesig+0x15f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
je     1301 <writesig+0x171>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writesig>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
lea    0xc(%rsp),%rsi
call   1090 <sigbufsize@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     1296 <writesig+0x106>
mov    0xc(%rsp),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1252 <writesig+0xc2>
mov    %rax,%rbx
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1266 <writesig+0xd6>
mov    0xc(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <read_all@plt>
test   %eax,%eax
js     1287 <writesig+0xf7>
lea    0xdff(%rip),%rsi
mov    %rbx,%rdi
mov    $0xd,%edx
call   1060 <memcpy@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     1287 <writesig+0xf7>
mov    0xc(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <write_all@plt>
test   %eax,%eax
js     1287 <writesig+0xf7>
je     1275 <writesig+0xe5>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
js     1287 <writesig+0xf7>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    1296 <writesig+0x106>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    0x2d6e(%rip),%rcx
mov    %eax,(%rcx)
jmp    1296 <writesig+0x106>
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebp
jne    128f <writesig+0xff>
jmp    1296 <writesig+0x106>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    0x2d4b(%rip),%rcx
mov    %eax,(%rcx)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [13 x i8] c"\01\02\03SCRUBBED!\00"
@SEEK_SET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @writesig(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i32 -1, i32* %5
%8 = load i8*, i8** %3
%9 = call i64 @sigbufsize(i8* %8, i32* %7)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
br label %60
12: ; preds = %1
%13 = load i32, i32* %7
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %12
br label %58
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
br label %60
23: ; preds = %17
%24 = load i32, i32* %5
%25 = load i8*, i8** %4
%26 = load i32, i32* %7
%27 = call i32 @read_all(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
br label %60
30: ; preds = %23
%31 = load i8*, i8** %4
%32 = call i32 @memcpy(i8* %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 13)
%33 = load i32, i32* %5
%34 = load i32, i32* @SEEK_SET
%35 = call i64 @lseek(i32 %33, i32 0, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %60
38: ; preds = %30
%39 = load i32, i32* %5
%40 = load i8*, i8** %4
%41 = load i32, i32* %7
%42 = call i32 @write_all(i32 %39, i8* %40, i32 %41)
store i32 %42, i32* %6
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38
br label %60
45: ; preds = %38
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = load i32, i32* @EINVAL
store i32 %49, i32* @errno
br label %60
50: ; preds = %45
%51 = load i32, i32* %5
%52 = call i64 @close(i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
br label %60
55: ; preds = %50
%56 = load i8*, i8** %4
%57 = call i32 @free(i8* %56)
store i32 0, i32* %2
br label %73
58: ; preds = %16
%59 = load i32, i32* @ENOMEM
store i32 %59, i32* @errno
br label %60
60: ; preds = %58, %54, %48, %44, %37, %29, %22, %11
%61 = load i8*, i8** %4
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i8*, i8** %4
%65 = call i32 @free(i8* %64)
br label %66
66: ; preds = %63, %60
%67 = load i32, i32* %5
%68 = icmp ne i32 %67, -1
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i32, i32* %5
%71 = call i64 @close(i32 %70)
br label %72
72: ; preds = %69, %66
store i32 -1, i32* %2
br label %73
73: ; preds = %72, %55
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [13 x i8] c"\01\02\03SCRUBBED!\00"
@SEEK_SET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @writesig(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i64 @sigbufsize(i8* %0, i32* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %45, label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %36, label %10
10: ; preds = %6
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %40, label %14
14: ; preds = %10
%15 = load i32, i32* %2
%16 = call i32 @read_all(i32 %12, i8* %8, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %38, label %18
18: ; preds = %14
%19 = call i32 @memcpy(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 13)
%20 = load i32, i32* @SEEK_SET
%21 = call i64 @lseek(i32 %12, i32 0, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %18
%24 = load i32, i32* %2
%25 = call i32 @write_all(i32 %12, i8* %8, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %38, label %27
27: ; preds = %23
%28 = icmp eq i32 %25, 0
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = load i32, i32* @EINVAL
store i32 %30, i32* @errno
br label %38
31: ; preds = %27
%32 = call i64 @close(i32 %12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = call i32 @free(i8* %8)
br label %45
36: ; preds = %6
%37 = load i32, i32* @ENOMEM
store i32 %37, i32* @errno
br label %45
38: ; preds = %31, %29, %23, %18, %14
%39 = call i32 @free(i8* %8)
br label %43
40: ; preds = %10
%41 = call i32 @free(i8* %8)
%42 = icmp eq i32 %12, -1
br i1 %42, label %45, label %43
43: ; preds = %38, %40
%44 = call i64 @close(i32 %12)
br label %45
45: ; preds = %36, %1, %40, %43, %34
%46 = phi i32 [ 0, %34 ], [ -1, %43 ], [ -1, %40 ], [ -1, %1 ], [ -1, %36 ]
ret i32 %46
}