#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*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int main(void) {
  char *fname = "test.txt";
  int fd;
  fd = creat(fname, 0666);
  if (fd < 0)
    perror("creat()");
  else {
    printf("Success!\n Filename: %s, fd: %d\n", fname, fd);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1186 <main+0x46>
lea    0xe8d(%rip),%rdi
call   1050 <perror@plt>
jmp    11a1 <main+0x61>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe7d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xeb8(%rip),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
test   %eax,%eax
js     1178 <main+0x38>
mov    %eax,%ebx
lea    0xeb2(%rip),%rdi
lea    0xe9a(%rip),%rsi
mov    %eax,%edx
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
jmp    1174 <main+0x34>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [8 x i8] c"creat()\00"
@.str.2 = [32 x i8] c"Success!\0A Filename: %s, fd: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @creat(i8* %4, i32 438)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %16
10: ; preds = %0
%11 = load i8*, i8** %2
%12 = load i32, i32* %3
%13 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %11, i32 %12)
%14 = load i32, i32* %3
%15 = call i32 @close(i32 %14)
br label %16
16: ; preds = %10, %8
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [8 x i8] c"creat()\00"
@.str.2 = [32 x i8] c"Success!\0A Filename: %s, fd: %d\0A\00"
define i32 @main() {
%1 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %8
5: ; preds = %0
%6 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%7 = tail call i32 @close(i32 %1)
br label %8
8: ; preds = %5, %3
ret i32 0
}