#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 O_CREAT;
int O_WRONLY;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
size_t write(int, char *, int);
int main() {
  int fd;
  size_t size;
  char string[] = "Hello, world!";
  (void)umask(0);
  if ((fd = open("myfile", O_WRONLY | O_CREAT, 0666)) < 0) {
    printf("Can\'t open file\n");
    exit(-1);
  }
  size = write(fd, string, 14);
  if (size != 14) {
    printf("Can\'t write all string\n");
    exit(-1);
  }
  if (close(fd) < 0) {
    printf("Can\'t close file\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x1e(%rbp)
mov    0xe88(%rip),%eax
mov    %eax,-0x16(%rbp)
mov    0xe82(%rip),%ax
mov    %ax,-0x12(%rbp)
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
lea    0xe60(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11da <main+0x7a>
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xe,%edx
call   1030 <write@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xe,-0x10(%rbp)
je     1210 <main+0xb0>
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    122e <main+0xce>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movabs $0x21646c726f7720,%rax
mov    %rax,0x6(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,(%rsp)
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2e40(%rip),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6c(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11e7 <main+0x87>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xe,%edx
call   1040 <write@plt>
cmp    $0xe,%rax
jne    11f0 <main+0x90>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    11df <main+0x7f>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe4f(%rip),%rdi
jmp    11f7 <main+0x97>
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.string = [14 x i8] c"Hello, world!\00"
@.str = [7 x i8] c"myfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [17 x i8] c"Can't open file\0A\00"
@.str.2 = [24 x i8] c"Can't write all string\0A\00"
@.str.3 = [18 x i8] c"Can't close file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca [14 x i8]
store i32 0, i32* %1
%5 = bitcast [14 x i8]* %4 to i8*
%6 = call i32 @umask(i32 0)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %9, i32 438)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %2
%17 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%18 = call i64 @write(i32 %16, i8* %17, i32 14)
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp ne i64 %19, 14
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %15
%25 = load i32, i32* %2
%26 = call i64 @close(i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
ret i32 0
}

LLVM IR (-O2):
@__const.main.string = [14 x i8] c"Hello, world!\00"
@.str = [7 x i8] c"myfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@str = [17 x i8] c"Can't close file\00"
@str.4 = [23 x i8] c"Can't write all string\00"
@str.5 = [16 x i8] c"Can't open file\00"
define i32 @main() {
%1 = alloca [14 x i8]
%2 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @umask(i32 0)
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i64 @write(i32 %7, i8* %2, i32 14)
%14 = icmp eq i64 %13, 14
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = tail call i64 @close(i32 %7)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
ret i32 0
}