#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*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strnlen(char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int fd;
  if ((fd = creat("testf", 0666)) == -1)
    perror("cannot create!");
  else
    fprintf(stdout, "created!");
  close(fd);
  if ((fd = open("testf", O_WRONLY, 0)) == -1)
    perror("cannot open");
  else
    fprintf(stdout, "opened");
  char *txt = "hello world";
  write(fd, txt, strnlen(txt, 100));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x1b6,%esi
call   1090 <creat@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11ad <main+0x3d>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
jmp    11c2 <main+0x52>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11fe <main+0x8e>
lea    0xe2a(%rip),%rdi
call   1080 <perror@plt>
jmp    1213 <main+0xa3>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe17(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x64,%esi
call   1040 <strnlen@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe85(%rip),%rdi
mov    $0x1b6,%esi
call   1090 <creat@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1210 <main+0xa0>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     121e <main+0xae>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe4b(%rip),%r14
mov    %r14,%rdi
mov    $0x64,%esi
call   1040 <strnlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
jmp    11a5 <main+0x35>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
jmp    11df <main+0x6f>

LLVM IR (-O0):
@.str = [6 x i8] c"testf\00"
@.str.1 = [15 x i8] c"cannot create!\00"
@stdout = global i32 0
@.str.2 = [9 x i8] c"created!\00"
@O_WRONLY = global i32 0
@.str.3 = [12 x i8] c"cannot open\00"
@.str.4 = [7 x i8] c"opened\00"
@.str.5 = [12 x i8] c"hello world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 438)
store i32 %4, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %11
8: ; preds = %0
%9 = load i32, i32* @stdout
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %8, %6
%12 = load i32, i32* %2
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %14, i32 0)
store i32 %15, i32* %2
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %22
19: ; preds = %11
%20 = load i32, i32* @stdout
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %19, %17
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8** %3
%23 = load i32, i32* %2
%24 = load i8*, i8** %3
%25 = load i8*, i8** %3
%26 = call i32 @strnlen(i8* %25, i32 100)
%27 = call i32 @write(i32 %23, i8* %24, i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"testf\00"
@.str.1 = [15 x i8] c"cannot create!\00"
@stdout = global i32 0
@.str.2 = [9 x i8] c"created!\00"
@O_WRONLY = global i32 0
@.str.3 = [12 x i8] c"cannot open\00"
@.str.4 = [7 x i8] c"opened\00"
@.str.5 = [12 x i8] c"hello world\00"
define i32 @main() {
%1 = tail call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 438)
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %8
5: ; preds = %0
%6 = load i32, i32* @stdout
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %5, %3
%9 = tail call i32 @close(i32 %1)
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %18
15: ; preds = %8
%16 = load i32, i32* @stdout
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %15, %13
%19 = tail call i32 @strnlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 100)
%20 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = tail call i32 @close(i32 %11)
ret i32 0
}