#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*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, unsigned int);
unsigned long strtoul(char *, char **, int);
scalar_t__ umask(scalar_t__);
int /*<<< orphan*/ warn(char *, char *);
int sh_umask(int argc, char *argv[]) {
  if (argc > 1) {
    char *end;
    unsigned long value = strtoul(argv[1], &end, 8);
    if (value > 0777 || *end) {
      errno = EINVAL;
      warn("umask: '%s'", argv[1]);
      return 1;
    }
    umask((mode_t)value);
  } else {
    mode_t mask = umask(0);
    umask(mask);
    printf("%.4o\n", (unsigned int)mask);
  }
  return 0;
}


x86 assembly (clang, -O0):
<sh_umask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11d1 <sh_umask+0x91>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <strtoul@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1ff,-0x20(%rbp)
ja     1191 <sh_umask+0x51>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c3 <sh_umask+0x83>
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3f(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe4e(%rip),%rdi
call   1030 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    1200 <sh_umask+0xc0>
mov    -0x20(%rbp),%rdi
call   1050 <umask@plt>
jmp    11f9 <sh_umask+0xb9>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <umask@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <umask@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sh_umask>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     1198 <sh_umask+0x58>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    %rsp,%rsi
mov    $0x8,%edx
call   1060 <strtoul@plt>
cmp    $0x1ff,%rax
ja     116e <sh_umask+0x2e>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     11c6 <sh_umask+0x86>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
mov    %eax,(%rcx)
mov    0x8(%rbx),%rsi
lea    0xe75(%rip),%rdi
call   1030 <warn@plt>
mov    $0x1,%r14d
jmp    11bb <sh_umask+0x7b>
xor    %r14d,%r14d
xor    %edi,%edi
call   1050 <umask@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <umask@plt>
lea    0xe58(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rax,%rdi
call   1050 <umask@plt>
xor    %r14d,%r14d
jmp    11bb <sh_umask+0x7b>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [12 x i8] c"umask: '%s'\00"
@.str.1 = [6 x i8] c"%.4o\0A\00"
define i32 @sh_umask(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %32
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strtoul(i8* %14, i8** %6, i32 8)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp ugt i64 %16, 511
br i1 %17, label %23, label %18
18: ; preds = %11
%19 = load i8*, i8** %6
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18, %11
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 1, i32* %3
br label %40
29: ; preds = %18
%30 = load i64, i64* %7
%31 = call i64 @umask(i64 %30)
br label %39
32: ; preds = %2
%33 = call i64 @umask(i64 0)
store i64 %33, i64* %8
%34 = load i64, i64* %8
%35 = call i64 @umask(i64 %34)
%36 = load i64, i64* %8
%37 = trunc i64 %36 to i32
%38 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %32, %29
store i32 0, i32* %3
br label %40
40: ; preds = %39, %23
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [12 x i8] c"umask: '%s'\00"
@.str.1 = [6 x i8] c"%.4o\0A\00"
define i32 @sh_umask(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %21
5: ; preds = %2
%6 = bitcast i8** %3 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i64 @strtoul(i8* %8, i8** %3, i32 8)
%10 = icmp ugt i64 %9, 511
br i1 %10, label %17, label %11
11: ; preds = %5
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i64 @umask(i64 %9)
br label %26
17: ; preds = %5, %11
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
%19 = load i8*, i8** %7
%20 = tail call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %26
21: ; preds = %2
%22 = tail call i64 @umask(i64 0)
%23 = tail call i64 @umask(i64 %22)
%24 = trunc i64 %22 to i32
%25 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %21, %15, %17
%27 = phi i32 [ 1, %17 ], [ 0, %15 ], [ 0, %21 ]
ret i32 %27
}