#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_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ die(char *, char *);
scalar_t__ errno;
long open(char *, int, int);
long strtol(char *, char **, int);
int getfd(char *arg) {
  char *ep;
  errno = 0;
  long fd = strtol(arg, &ep, 10);
  if (errno == 0 && ep[0] == '/' && ep[1] == '\0')
    return fd;
  fd = open(arg, O_WRONLY | O_CREAT | O_APPEND, 0644);
  if (fd < 0)
    die("Opening file '%s' for writing:", arg);
  return fd;
}


x86 assembly (clang, -O0):
<getfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e71(%rip),%rax
cmpq   $0x0,(%rax)
jne    119e <getfd+0x6e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    119e <getfd+0x6e>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    119e <getfd+0x6e>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11ed <getfd+0xbd>
mov    -0x10(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11e6 <getfd+0xb6>
mov    -0x10(%rbp),%rsi
lea    0xe1f(%rip),%rdi
call   1050 <die@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getfd>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e95(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
cmpq   $0x0,(%r15)
jne    1172 <getfd+0x42>
mov    %rax,%rbx
mov    0x8(%rsp),%rax
cmpb   $0x2f,(%rax)
jne    1172 <getfd+0x42>
cmpb   $0x0,0x1(%rax)
je     11b1 <getfd+0x81>
mov    0x2e4f(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11b1 <getfd+0x81>
lea    0xe57(%rip),%rdi
mov    %r14,%rsi
call   1050 <die@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [31 x i8] c"Opening file '%s' for writing:\00"
define i32 @getfd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* @errno
%6 = load i8*, i8** %3
%7 = call i64 @strtol(i8* %6, i8** %4, i32 10)
store i64 %7, i64* %5
%8 = load i64, i64* @errno
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 47
br i1 %15, label %16, label %25
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i64, i64* %5
%24 = trunc i64 %23 to i32
store i32 %24, i32* %2
br label %41
25: ; preds = %16, %10, %1
%26 = load i8*, i8** %3
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_APPEND
%31 = or i32 %29, %30
%32 = call i64 @open(i8* %26, i32 %31, i32 420)
store i64 %32, i64* %5
%33 = load i64, i64* %5
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = load i8*, i8** %3
%37 = call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %25
%39 = load i64, i64* %5
%40 = trunc i64 %39 to i32
store i32 %40, i32* %2
br label %41
41: ; preds = %38, %22
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@errno = global i64 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [31 x i8] c"Opening file '%s' for writing:\00"
define i32 @getfd(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @errno
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 47
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %8, i64 1
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %11, %7, %1
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = load i32, i32* @O_APPEND
%20 = or i32 %18, %19
%21 = tail call i64 @open(i8* %0, i32 %20, i32 420)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = tail call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %15, %23, %11
%26 = phi i64 [ %4, %11 ], [ %21, %23 ], [ %21, %15 ]
%27 = trunc i64 %26 to i32
ret i32 %27
}