#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int main(void) {
  int fd = -1;
  char filename[] = "test.txt";
  fd = open(filename, O_RDWR);
  if (-1 == fd) {
    printf("Open file %s failure!,fd:%d\n", filename, fd);
  } else {
    printf("Open file %s success!,fd:%d\n", filename, fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    0xec3(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xec1(%rip),%al
mov    %al,-0x9(%rbp)
lea    -0x11(%rbp),%rdi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1185 <main+0x65>
lea    -0x11(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
jmp    1198 <main+0x78>
lea    -0x11(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xe93(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1160 <main+0x40>
lea    0xecf(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %eax,%edx
jmp    1171 <main+0x51>
lea    0xea2(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    $0xffffffff,%edx
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str = [29 x i8] c"Open file %s failure!,fd:%d\0A\00"
@.str.1 = [29 x i8] c"Open file %s success!,fd:%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [9 x i8]
store i32 0, i32* %1
store i32 -1, i32* %2
%4 = bitcast [9 x i8]* %3 to i8*
%5 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* %2
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
br label %18
14: ; preds = %0
%15 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%16 = load i32, i32* %2
%17 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %16)
br label %18
18: ; preds = %14, %10
ret i32 0
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str = [29 x i8] c"Open file %s failure!,fd:%d\0A\00"
@.str.1 = [29 x i8] c"Open file %s success!,fd:%d\0A\00"
define i32 @main() {
%1 = alloca [9 x i8]
%2 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* %2, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %2, i32 -1)
br label %10
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 %4)
br label %10
10: ; preds = %8, %6
ret i32 0
}