#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_EXCL;
int S_IRUSR;
int /*<<< orphan*/ S_IRWXU;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int creat(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int fd, n;
  printf("Please select a way of creating files:\n");
  printf("1.creat 2.open\n");
  scanf("%d", &n);
  if (1 == n) {
    if (-1 == (fd = creat("hello.txt", S_IRWXU))) {
      perror("creat");
      exit(1);
    } else {
      printf("Create file success\n");
    }
  } else if (2 == n) {
    if (-1 == (fd = open("hello.txt", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR))) {
      perror("open");
      exit(1);
    } else {
      printf("create file success \n");
    }
  } else {
    printf("your choice may be wrong \n");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
lea    0xe96(%rip),%rdi
call   1030 <printf@plt>
lea    0xe9a(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <scanf@plt>
mov    $0x1,%eax
cmp    -0xc(%rbp),%eax
jne    1203 <main+0x93>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1080 <creat@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11f2 <main+0x82>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
jmp    128b <main+0x11b>
mov    $0x2,%eax
cmp    -0xc(%rbp),%eax
jne    127a <main+0x10a>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2da7(%rip),%rax
or     (%rax),%esi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
mov    0x2da5(%rip),%rax
or     (%rax),%edx
lea    0xdff(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1269 <main+0xf9>
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdf5(%rip),%rdi
call   1030 <printf@plt>
jmp    1286 <main+0x116>
lea    0xdfa(%rip),%rdi
call   1030 <printf@plt>
jmp    128b <main+0x11b>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6c(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0xc(%rsp),%eax
cmp    $0x2,%eax
jne    11e7 <main+0x77>
mov    0x2e4a(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e00(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe31(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1223 <main+0xb3>
mov    %eax,%ebx
lea    0xe7e(%rip),%rdi
jmp    120f <main+0x9f>
cmp    $0x1,%eax
jne    1242 <main+0xd2>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1080 <creat@plt>
cmp    $0xffffffff,%eax
je     122c <main+0xbc>
mov    %eax,%ebx
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe1a(%rip),%rdi
jmp    1233 <main+0xc3>
lea    0xdda(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe00(%rip),%rdi
call   1030 <puts@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"Please select a way of creating files:\0A\00"
@.str.1 = [16 x i8] c"1.creat 2.open\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [10 x i8] c"hello.txt\00"
@S_IRWXU = global i32 0
@.str.4 = [6 x i8] c"creat\00"
@.str.5 = [21 x i8] c"Create file success\0A\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.6 = [5 x i8] c"open\00"
@.str.7 = [22 x i8] c"create file success \0A\00"
@.str.8 = [27 x i8] c"your choice may be wrong \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%7 = load i32, i32* %3
%8 = icmp eq i32 1, %7
br i1 %8, label %9, label %19
9: ; preds = %0
%10 = load i32, i32* @S_IRWXU
%11 = call i32 @creat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %16
br label %40
19: ; preds = %0
%20 = load i32, i32* %3
%21 = icmp eq i32 2, %20
br i1 %21, label %22, label %37
22: ; preds = %19
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* @O_EXCL
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %28)
store i32 %29, i32* %2
%30 = icmp eq i32 -1, %29
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %22
%35 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %36
36: ; preds = %34
br label %39
37: ; preds = %19
%38 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
br label %39
39: ; preds = %37, %36
br label %40
40: ; preds = %39, %18
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [10 x i8] c"hello.txt\00"
@S_IRWXU = global i32 0
@.str.4 = [6 x i8] c"creat\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.6 = [5 x i8] c"open\00"
@str = [39 x i8] c"Please select a way of creating files:\00"
@str.9 = [15 x i8] c"1.creat 2.open\00"
@str.10 = [26 x i8] c"your choice may be wrong \00"
@str.11 = [21 x i8] c"create file success \00"
@str.12 = [20 x i8] c"Create file success\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
switch i32 %6, label %26 [
i32 1, label %7
i32 2, label %14
]
7: ; preds = %0
%8 = load i32, i32* @S_IRWXU
%9 = call i32 @creat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %28
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_EXCL
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %0
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.10, i64 0, i64 0))
unreachable
28: ; preds = %14, %7
%29 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0), %7 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.11, i64 0, i64 0), %14 ]
%30 = phi i32 [ %9, %7 ], [ %21, %14 ]
%31 = call i32 @puts(i8* dereferenceable(1) %29)
%32 = call i32 @close(i32 %30)
ret i32 0
}