#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*/ word;
typedef int ssize_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int close(int);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
unsigned long strlen(char const *);
int write(int, ...);
int main(int argc, char *argv[]) {
  int fd;
  const char *filename1 = "test_input.txt";
  const char *buf = "0123456789";
  ssize_t nr;
  fd = open(filename1, O_WRONLY | O_CREAT | O_TRUNC,
            S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
  if (fd == -1) {
    perror("open");
  }
  nr = write(fd, buf, strlen(buf));
  if (nr == -1) {
    perror("write");
  }
  int r = close(fd);
  if (r == -1)
    perror("close");
  fd = open("/tmp/short.txt", O_WRONLY | O_CREAT | O_TRUNC,
            S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
  if (fd == -1) {
    perror("open");
  }
  unsigned long word = 1720;
  size_t count;
  nr = 0;
  count = sizeof(word);
  nr = write(fd, &word, word);
  if (nr == -1) {
    perror("write");
  } else if (nr != count) {
    printf("Failed to write data, length %d, written %d\n", (int)count,
           (int)nr);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    0x2df5(%rip),%rax
or     (%rax),%edx
mov    0x2e14(%rip),%rax
or     (%rax),%edx
mov    0x2df3(%rip),%rax
or     (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f6 <main+0x96>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1237 <main+0xd7>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1258 <main+0xf8>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
mov    0x2d90(%rip),%rax
or     (%rax),%esi
mov    0x2d7f(%rip),%rax
or     (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
mov    0x2d2d(%rip),%rax
or     (%rax),%edx
mov    0x2d4c(%rip),%rax
or     (%rax),%edx
mov    0x2d2b(%rip),%rax
or     (%rax),%edx
mov    0x2d1a(%rip),%rax
or     (%rax),%edx
lea    0xd84(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12c5 <main+0x165>
lea    0xd5a(%rip),%rdi
call   1080 <perror@plt>
movq   $0x6b8,-0x38(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x8,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rdx
lea    -0x38(%rbp),%rsi
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    130c <main+0x1ac>
lea    0xd1d(%rip),%rdi
call   1080 <perror@plt>
jmp    1334 <main+0x1d4>
movslq -0x2c(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     132f <main+0x1cf>
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x2c(%rbp),%edx
lea    0xd10(%rip),%rdi
call   1050 <printf@plt>
jmp    1334 <main+0x1d4>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e3e(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e7c(%rip),%rax
or     (%rax),%esi
mov    0x2e53(%rip),%r13
mov    0x2e2c(%rip),%rbp
mov    0x0(%rbp),%edx
or     0x0(%r13),%edx
mov    0x2e46(%rip),%r14
or     (%r14),%edx
mov    0x2e24(%rip),%r15
or     (%r15),%edx
mov    0x2e12(%rip),%r12
or     (%r12),%edx
lea    0xe4f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1275 <main+0x125>
lea    0xe47(%rip),%rsi
mov    $0xa,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1286 <main+0x136>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     1297 <main+0x147>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
mov    0x0(%rbp),%edx
or     0x0(%r13),%edx
or     (%r14),%edx
or     (%r15),%edx
or     (%r12),%edx
lea    0xe09(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12a8 <main+0x158>
movq   $0x6b8,(%rsp)
mov    %rsp,%rsi
mov    $0x6b8,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x8,%eax
je     1264 <main+0x114>
cmp    $0xffffffff,%eax
je     12b9 <main+0x169>
lea    0xde2(%rip),%rdi
mov    $0x8,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   1070 <perror@plt>
jmp    11c1 <main+0x71>
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
jmp    11df <main+0x8f>
lea    0xd87(%rip),%rdi
call   1070 <perror@plt>
jmp    11ef <main+0x9f>
lea    0xd6b(%rip),%rdi
call   1070 <perror@plt>
jmp    122e <main+0xde>
lea    0xd5f(%rip),%rdi
call   1070 <perror@plt>
jmp    1264 <main+0x114>

LLVM IR (-O0):
@.str = [15 x i8] c"test_input.txt\00"
@.str.1 = [11 x i8] c"0123456789\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [6 x i8] c"close\00"
@.str.5 = [15 x i8] c"/tmp/short.txt\00"
@.str.6 = [45 x i8] c"Failed to write data, length %d, written %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %8
%13 = load i8*, i8** %7
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IWUSR
%20 = load i32, i32* @S_IRUSR
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IWGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IROTH
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %13, i32 %18, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %2
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %2
%34 = load i32, i32* %6
%35 = load i8*, i8** %8
%36 = load i8*, i8** %8
%37 = call i64 @strlen(i8* %36)
%38 = call i32 (i32, ...) @write(i32 %34, i8* %35, i64 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %33
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = load i32, i32* @O_WRONLY
%52 = load i32, i32* @O_CREAT
%53 = or i32 %51, %52
%54 = load i32, i32* @O_TRUNC
%55 = or i32 %53, %54
%56 = load i32, i32* @S_IWUSR
%57 = load i32, i32* @S_IRUSR
%58 = or i32 %56, %57
%59 = load i32, i32* @S_IWGRP
%60 = or i32 %58, %59
%61 = load i32, i32* @S_IRGRP
%62 = or i32 %60, %61
%63 = load i32, i32* @S_IROTH
%64 = or i32 %62, %63
%65 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %55, i32 %64)
store i32 %65, i32* %6
%66 = load i32, i32* %6
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %50
%69 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %70
70: ; preds = %68, %50
store i64 1720, i64* %11
store i32 0, i32* %9
store i64 8, i64* %12
%71 = load i32, i32* %6
%72 = load i64, i64* %11
%73 = call i32 (i32, ...) @write(i32 %71, i64* %11, i64 %72)
store i32 %73, i32* %9
%74 = load i32, i32* %9
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %70
%77 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %89
78: ; preds = %70
%79 = load i32, i32* %9
%80 = sext i32 %79 to i64
%81 = load i64, i64* %12
%82 = icmp ne i64 %80, %81
br i1 %82, label %83, label %88
83: ; preds = %78
%84 = load i64, i64* %12
%85 = trunc i64 %84 to i32
%86 = load i32, i32* %9
%87 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %85, i32 %86)
br label %88
88: ; preds = %83, %78
br label %89
89: ; preds = %88, %76
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"test_input.txt\00"
@.str.1 = [11 x i8] c"0123456789\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [6 x i8] c"close\00"
@.str.5 = [15 x i8] c"/tmp/short.txt\00"
@.str.6 = [45 x i8] c"Failed to write data, length %d, written %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IWUSR
%10 = load i32, i32* @S_IRUSR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IWGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %2
%23 = tail call i32 (i32, ...) @write(i32 %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 10)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = tail call i32 @close(i32 %18)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %34, %33
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IWUSR
%39 = load i32, i32* @S_IRUSR
%40 = or i32 %39, %38
%41 = load i32, i32* @S_IWGRP
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IRGRP
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IROTH
%46 = or i32 %44, %45
%47 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %37, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %32
%50 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %32
%52 = bitcast i64* %3 to i8*
store i64 1720, i64* %3
%53 = call i32 (i32, ...) @write(i32 %47, i64* %3, i64 1720)
switch i32 %53, label %56 [
i32 -1, label %54
i32 8, label %58
]
54: ; preds = %51
%55 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %58
56: ; preds = %51
%57 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 8, i32 %53)
br label %58
58: ; preds = %51, %56, %54
ret i32 0
}