#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ getpid();
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int write_pid(char *filename) {
  char pid[20];
  int pidfile;
  sprintf(pid, "%i\n", (int)getpid());
  pidfile = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
  if (pidfile >= 0) {
    write(pidfile, pid, strlen(pid));
    close(pidfile);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xe78(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     11fc <write_pid+0x9c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1203 <write_pid+0xa3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe87(%rip),%rsi
mov    %rsp,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    0x2e6e(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11d4 <write_pid+0x74>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%i\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @write_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [20 x i8]
%5 = alloca i32
store i8* %0, i8** %3
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = call i64 (...) @getpid()
%8 = trunc i64 %7 to i32
%9 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %1
%20 = load i32, i32* %5
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %20, i8* %21, i32 %23)
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
store i32 1, i32* %2
br label %28
27: ; preds = %1
store i32 0, i32* %2
br label %28
28: ; preds = %27, %19
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [4 x i8] c"%i\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @write_pid(i8* nocapture %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = tail call i64 (...) @getpid()
%5 = trunc i64 %4 to i32
%6 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %0, i32 %11, i32 420)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = call i32 @strlen(i8* %3)
%16 = call i32 @write(i32 %12, i8* %3, i32 %15)
%17 = call i32 @close(i32 %12)
br label %18
18: ; preds = %1, %14
%19 = phi i32 [ 1, %14 ], [ 0, %1 ]
ret i32 %19
}