#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 */
typedef scalar_t__ mode_t;

/* Variables and functions */
int /*<<< orphan*/ O_CREAT;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int);
scalar_t__ umask(int);
int main(void) {
  int fd;
  mode_t mask;
  mask = umask(S_IRUSR | S_IRGRP | S_IROTH);
  printf("the original mask is %x\n", (unsigned int)mask);
  if ((fd = open("test.txt", O_CREAT, 0777)) == -1) {
    perror("fail to create");
    exit(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
or     (%rax),%edi
mov    0x2e48(%rip),%rax
or     (%rax),%edi
call   1050 <umask@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
mov    $0x1ff,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11e1 <main+0x81>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e60(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
mov    0x2e56(%rip),%rax
or     (%rax),%edi
call   1050 <umask@plt>
lea    0xe78(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
mov    $0x1ff,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b9 <main+0x59>
mov    %eax,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [25 x i8] c"the original mask is %x\0A\00"
@.str.1 = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@.str.2 = [15 x i8] c"fail to create\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IRGRP
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IROTH
%8 = or i32 %6, %7
%9 = call i64 @umask(i32 %8)
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = trunc i64 %10 to i32
%12 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @O_CREAT
%14 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 511)
store i32 %14, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
ret i32 0
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [25 x i8] c"the original mask is %x\0A\00"
@.str.1 = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@.str.2 = [15 x i8] c"fail to create\00"
define i32 @main() {
%1 = load i32, i32* @S_IRUSR
%2 = load i32, i32* @S_IRGRP
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IROTH
%5 = or i32 %3, %4
%6 = tail call i64 @umask(i32 %5)
%7 = trunc i64 %6 to i32
%8 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_CREAT
%10 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 511)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = tail call i32 @close(i32 %10)
ret i32 0
}