#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
int write(int, char *, int);
int main() {
  int fd = open("hello.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd > 0) {
    char out[100] = "Hello, world!\n";
    int out_len = strlen(out);
    int written_bytes = write(fd, out, out_len);
    if (written_bytes == out_len) {
      printf("Success!\n");
      return 0;
    }
    close(fd);
  }
  perror("Error: ");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
lea    0xe5c(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    121a <main+0xaa>
lea    -0x70(%rbp),%rdi
lea    0xe5a(%rip),%rsi
mov    $0x64,%edx
call   1070 <memcpy@plt>
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x74(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x74(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%eax
cmp    -0x74(%rbp),%eax
jne    1212 <main+0xa2>
lea    0xe09(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    122d <main+0xbd>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e59(%rip),%rax
mov    0x2e82(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
lea    0xe76(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
jle    1228 <main+0xc8>
mov    %eax,%ebx
movaps 0xecb(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xeaf(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe93(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe77(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe5b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe3f(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
jne    1221 <main+0xc1>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xddb(%rip),%rdi
call   1080 <perror@plt>
jmp    1216 <main+0xb6>

LLVM IR (-O0):
@.str = [10 x i8] c"hello.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@__const.main.out = [100 x i8] c"Hello, world!\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [10 x i8] c"Success!\0A\00"
@.str.2 = [8 x i8] c"Error: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10, i32 420)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %0
%15 = bitcast [100 x i8]* %3 to i8*
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %4
%18 = load i32, i32* %2
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* %4
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = load i32, i32* %4
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %32
27: ; preds = %14
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %0
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %32
32: ; preds = %30, %25
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [10 x i8] c"hello.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@__const.main.out = [100 x i8] c"Hello, world!\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.2 = [8 x i8] c"Error: \00"
@str = [9 x i8] c"Success!\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6, i32 420)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %0
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
%12 = call i32 @write(i32 %7, i8* %10, i32 %11)
%13 = icmp eq i32 %12, %11
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %20
16: ; preds = %9
%17 = call i32 @close(i32 %7)
br label %18
18: ; preds = %16, %0
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %14, %18
ret i32 0
}