#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ creat(char *, int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int umask(int);
int main(void) {
  int fd = 0, om = 0;
  om = umask(0);
  printf("mask before set to 0 is %o\n", om);
  if ((fd = creat("foo", (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
                          S_IWOTH)) < 0))
    err_sys("creat error for foo");
  close(fd);
  om = umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  printf("mask before set to 0066 is %o\n", om);
  if ((fd = creat("bar", (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
                          S_IWOTH)) < 0))
    err_sys("creat error for bar");
  close(fd);
  om = umask(0);
  printf("last mask is %o\n", om);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
xor    %edi,%edi
call   1060 <umask@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe6f(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    0x2de6(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   1070 <creat@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x8(%rbp)
test   $0x1,%al
jne    11f7 <main+0x97>
jmp    1203 <main+0xa3>
lea    0xe1e(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x2dcd(%rip),%rax
or     (%rax),%edi
mov    0x2da4(%rip),%rax
or     (%rax),%edi
mov    0x2d83(%rip),%rax
or     (%rax),%edi
call   1060 <umask@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xdef(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
mov    0x2d71(%rip),%rax
or     (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
mov    0x2d57(%rip),%rax
or     (%rax),%esi
mov    0x2d36(%rip),%rax
or     (%rax),%esi
lea    0xddc(%rip),%rdi
call   1070 <creat@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x8(%rbp)
test   $0x1,%al
jne    12a7 <main+0x147>
jmp    12b3 <main+0x153>
lea    0xda1(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xd94(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe87(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2e39(%rip),%rax
mov    0x2e52(%rip),%r15
mov    (%r15),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%r12
or     (%r12),%esi
mov    0x2e43(%rip),%r13
or     0x0(%r13),%esi
mov    0x2e18(%rip),%rbp
or     0x0(%rbp),%esi
mov    0x2df6(%rip),%r14
or     (%r14),%esi
lea    0xe68(%rip),%rdi
call   1070 <creat@plt>
mov    %rax,%rbx
shr    $0x3f,%rbx
test   %rax,%rax
jns    11e1 <main+0x81>
lea    0xe40(%rip),%rdi
call   1030 <err_sys@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x0(%r13),%edi
or     (%r12),%edi
or     0x0(%rbp),%edi
or     (%r14),%edi
call   1060 <umask@plt>
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    (%r15),%esi
mov    0x2dad(%rip),%rax
or     (%rax),%esi
or     (%r12),%esi
or     0x0(%r13),%esi
or     0x0(%rbp),%esi
or     (%r14),%esi
lea    0xe35(%rip),%rdi
call   1070 <creat@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1243 <main+0xe3>
lea    0xe11(%rip),%rdi
call   1030 <err_sys@plt>
shr    $0x3f,%rbx
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe07(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"mask before set to 0 is %o\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.2 = [20 x i8] c"creat error for foo\00"
@.str.3 = [31 x i8] c"mask before set to 0066 is %o\0A\00"
@.str.4 = [4 x i8] c"bar\00"
@.str.5 = [20 x i8] c"creat error for bar\00"
@.str.6 = [17 x i8] c"last mask is %o\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
%4 = call i32 @umask(i32 0)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IWGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWOTH
%17 = or i32 %15, %16
%18 = call i64 @creat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp slt i64 %18, 0
%20 = zext i1 %19 to i32
store i32 %20, i32* %2
br i1 %19, label %21, label %23
21: ; preds = %0
%22 = call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %0
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @S_IRGRP
%27 = load i32, i32* @S_IWGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IROTH
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IWOTH
%32 = or i32 %30, %31
%33 = call i32 @umask(i32 %32)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IRGRP
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IWGRP
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IROTH
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IWOTH
%46 = or i32 %44, %45
%47 = call i64 @creat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = icmp slt i64 %47, 0
%49 = zext i1 %48 to i32
store i32 %49, i32* %2
br i1 %48, label %50, label %52
50: ; preds = %23
%51 = call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %52
52: ; preds = %50, %23
%53 = load i32, i32* %2
%54 = call i32 @close(i32 %53)
%55 = call i32 @umask(i32 0)
store i32 %55, i32* %3
%56 = load i32, i32* %3
%57 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %56)
%58 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [28 x i8] c"mask before set to 0 is %o\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.2 = [20 x i8] c"creat error for foo\00"
@.str.3 = [31 x i8] c"mask before set to 0066 is %o\0A\00"
@.str.4 = [4 x i8] c"bar\00"
@.str.5 = [20 x i8] c"creat error for bar\00"
@.str.6 = [17 x i8] c"last mask is %o\0A\00"
define i32 @main() {
%1 = tail call i32 @umask(i32 0)
%2 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @S_IRUSR
%4 = load i32, i32* @S_IWUSR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRGRP
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IWGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IWOTH
%13 = or i32 %11, %12
%14 = tail call i64 @creat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp slt i64 %14, 0
%16 = lshr i64 %14, 63
%17 = trunc i64 %16 to i32
br i1 %15, label %18, label %20
18: ; preds = %0
%19 = tail call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %0
%21 = tail call i32 @close(i32 %17)
%22 = load i32, i32* @S_IRGRP
%23 = load i32, i32* @S_IWGRP
%24 = or i32 %23, %22
%25 = load i32, i32* @S_IROTH
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IWOTH
%28 = or i32 %26, %27
%29 = tail call i32 @umask(i32 %28)
%30 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = load i32, i32* @S_IRGRP
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IWGRP
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IROTH
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IWOTH
%41 = or i32 %39, %40
%42 = tail call i64 @creat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %20
%45 = tail call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %44, %20
%47 = lshr i64 %42, 63
%48 = trunc i64 %47 to i32
%49 = tail call i32 @close(i32 %48)
%50 = tail call i32 @umask(i32 0)
%51 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %50)
%52 = tail call i32 @exit(i32 0)
unreachable
}