#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_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int main() {
  int fd = 0;
  int ret;
  fd = open("/tmp/tt", O_RDWR);
  if (fd == -1) {
    perror("fopen failed");
    return -1;
  }
  ret = write(fd, "hello", 5);
  if (ret == -1) {
    perror("fread error");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1190 <main+0x50>
lea    0xe89(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <main+0x98>
mov    -0x8(%rbp),%edi
lea    0xe7b(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c9 <main+0x89>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <main+0x98>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1180 <main+0x40>
mov    %eax,%ebx
lea    0xeb1(%rip),%rsi
mov    %eax,%edi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1189 <main+0x49>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe81(%rip),%rdi
jmp    1190 <main+0x50>
lea    0xe8b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/tmp/tt\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"fopen failed\00"
@.str.2 = [6 x i8] c"hello\00"
@.str.3 = [12 x i8] c"fread error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %20
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i32 @write(i32 %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %20
17: ; preds = %10
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
store i32 0, i32* %1
br label %20
20: ; preds = %17, %15, %8
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@.str = [8 x i8] c"/tmp/tt\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"fopen failed\00"
@.str.2 = [6 x i8] c"hello\00"
@.str.3 = [12 x i8] c"fread error\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %13
6: ; preds = %0
%7 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %13
11: ; preds = %6
%12 = tail call i32 @close(i32 %2)
br label %13
13: ; preds = %11, %9, %4
%14 = phi i32 [ -1, %4 ], [ -1, %9 ], [ 0, %11 ]
ret i32 %14
}